From 840cea519d66cca2f52aa7f6d4299aa357d69528 Mon Sep 17 00:00:00 2001 From: Angela Tran Date: Fri, 25 Aug 2023 23:38:36 +0000 Subject: [PATCH] feat: save plan file and use it when running apply command --- experiment/experiment-pipeline.yml | 33 ++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/experiment/experiment-pipeline.yml b/experiment/experiment-pipeline.yml index 01f620c1..8fc0b366 100644 --- a/experiment/experiment-pipeline.yml +++ b/experiment/experiment-pipeline.yml @@ -35,6 +35,9 @@ stages: - bash: | WORKSPACE=$(python experiment/experiment-workspace.py) echo "##vso[task.setvariable variable=workspace]$WORKSPACE" + + TAG_TYPE=$(python experiment/tag.py) + echo "##vso[task.setvariable variable=tag_type]$TAG_TYPE" displayName: Determine deployment environment env: REASON: $(Build.Reason) @@ -76,12 +79,36 @@ stages: command: plan # wait for lock to be released, in case being used by another pipeline run # https://discuss.hashicorp.com/t/terraform-plan-wait-for-lock-to-be-released/6870/2 - commandOptions: -input=false -lock-timeout=5m + commandOptions: -input=false -lock-timeout=5m -out=tfplan workingDirectory: "$(System.DefaultWorkingDirectory)/experiment" # service connection environmentServiceNameAzureRM: compiler-deployer # the plan is done as part of the apply (below), so don't bother doing it twice condition: notIn(variables['Build.SourceBranchName'], 'dev') + # need to copy and publish the tfplan to used by next stage if it's going to run + - task: CopyFiles@2 + displayName: 'Copy tfplan for use in TerraformApply' + inputs: + targetFolder: '$(Build.ArtifactStagingDirectory)' + condition: | + or( + in(variables['Build.SourceBranchName'], 'dev'), + or( + eq(variables['tag_type'], 'test'), + eq(variables['tag_type'], 'prod') + ) + ) + - publish: '$(Build.ArtifactStagingDirectory)' + displayName: 'Publish tfplan for use in TerraformApply' + artifact: tfplan + condition: | + or( + in(variables['Build.SourceBranchName'], 'dev'), + or( + eq(variables['tag_type'], 'test'), + eq(variables['tag_type'], 'prod') + ) + ) - stage: TerraformApply dependsOn: TerraformPlan jobs: @@ -102,6 +129,8 @@ stages: deploy: steps: - checkout: self + - download: current + artifact: tfplan - bash: | WORKSPACE=$(python experiment/experiment-workspace.py) echo "##vso[task.setvariable variable=workspace]$WORKSPACE" @@ -143,7 +172,7 @@ stages: provider: azurerm command: apply # (ditto the lock comment above) - commandOptions: -input=false -lock-timeout=5m + commandOptions: -input=false -lock-timeout=5m $(Pipeline.Workspace)/tfplan workingDirectory: "$(System.DefaultWorkingDirectory)/experiment" # service connection environmentServiceNameAzureRM: compiler-deployer