Skip to content

Commit

Permalink
Fix: terraform plan and apply conditions (#2232)
Browse files Browse the repository at this point in the history
  • Loading branch information
lalver1 authored Jul 26, 2024
2 parents 2f62863 + dd57ccc commit a10beb7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
10 changes: 1 addition & 9 deletions terraform/azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ stages:
- name: IS_TAG
value: $[startsWith(variables['Build.SourceBranch'], 'refs/tags/')]
steps:
- bash: |
python terraform/pipeline/workspace.py
TAG_TYPE=$(python terraform/pipeline/tag.py)
echo "##vso[task.setvariable variable=tag_type;isOutput=true]$TAG_TYPE"
- bash: python terraform/pipeline/workspace.py
displayName: Set environment-related variables
# save the values
# https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#use-outputs-in-a-different-job
Expand All @@ -45,22 +41,18 @@ stages:
condition: eq(dependencies.environment.outputs['env_select.service_connection'], 'Development')
variables:
workspace: $[ dependencies.environment.outputs['env_select.workspace'] ]
tag_type: $[ dependencies.environment.outputs['env_select.tag_type'] ]
steps:
- template: pipeline/deploy.yml
parameters:
service_connection: Development
workspace: $(workspace)
tag_type: $(tag_type)
- job: prod
dependsOn: environment
condition: eq(dependencies.environment.outputs['env_select.service_connection'], 'Production')
variables:
workspace: $[ dependencies.environment.outputs['env_select.workspace'] ]
tag_type: $[ dependencies.environment.outputs['env_select.tag_type'] ]
steps:
- template: pipeline/deploy.yml
parameters:
service_connection: Production
workspace: $(workspace)
tag_type: $(tag_type)
36 changes: 22 additions & 14 deletions terraform/pipeline/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ parameters:
type: string
- name: workspace
type: string
- name: tag_type
type: string

steps:
# https://github.com/microsoft/azure-pipelines-terraform/tree/main/Tasks/TerraformInstaller#readme
Expand Down Expand Up @@ -38,6 +36,18 @@ steps:
workingDirectory: "$(System.DefaultWorkingDirectory)/terraform"
# service connection
environmentServiceNameAzureRM: "${{ parameters.service_connection }}"
- bash: |
echo "REASON is $REASON, INDIVIDUAL_SOURCE is $INDIVIDUAL_SOURCE, SOURCE_BRANCH is $SOURCE_BRANCH"
TAG_TYPE=$(python terraform/pipeline/tag.py)
echo "##vso[task.setvariable variable=tag_type]$TAG_TYPE"
displayName: Set tag-type variable
env:
REASON: $(Build.Reason)
INDIVIDUAL_SOURCE: $(Build.SourceBranchName)
SOURCE_BRANCH: $(Build.SourceBranch)
- bash: |
echo $(tag_type)
displayName: Display tag-type variable
- task: TerraformTaskV3@3
displayName: Terraform plan
inputs:
Expand All @@ -51,12 +61,11 @@ steps:
environmentServiceNameAzureRM: "${{ parameters.service_connection }}"
# the plan is done as part of the apply (below), so don't bother doing it twice
condition: |
${{ and(
ne(variables['Build.SourceBranchName'], 'main'),
ne(parameters.tag_type, 'test'),
ne(parameters.tag_type, 'prod')
)
}}
and(
ne(variables['Build.SourceBranchName'], 'main'),
ne(variables['tag_type'], 'test'),
ne(variables['tag_type'], 'prod')
)
- task: TerraformTaskV3@3
displayName: Terraform apply
inputs:
Expand All @@ -69,9 +78,8 @@ steps:
environmentServiceNameAzureRM: "${{ parameters.service_connection }}"
# only run on main branch OR if it's a tag for test or prod
condition: |
${{ or(
eq(variables['Build.SourceBranchName'], 'main'),
eq(parameters.tag_type, 'test'),
eq(parameters.tag_type, 'prod')
)
}}
or(
eq(variables['Build.SourceBranchName'], 'main'),
eq(variables['tag_type'], 'test'),
eq(variables['tag_type'], 'prod')
)
4 changes: 3 additions & 1 deletion terraform/pipeline/tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
REASON = os.environ["REASON"]
# use variable corresponding to tag triggers
SOURCE = os.environ["INDIVIDUAL_SOURCE"]
IS_TAG = os.environ["IS_TAG"].lower() == "true"

SOURCE_BRANCH = os.environ["SOURCE_BRANCH"]
IS_TAG = SOURCE_BRANCH.startswith("refs/tags/") is True

if REASON == "IndividualCI" and IS_TAG:
if re.fullmatch(r"20\d\d.\d\d.\d+-rc\d+", SOURCE):
Expand Down

0 comments on commit a10beb7

Please sign in to comment.