Skip to content

Commit

Permalink
ci: replace parameter with variable in tf plan and apply logic
Browse files Browse the repository at this point in the history
  • Loading branch information
lalver1 committed Jul 25, 2024
1 parent bbd4fc6 commit 5866eee
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 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)
18 changes: 12 additions & 6 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,14 @@ steps:
workingDirectory: "$(System.DefaultWorkingDirectory)/terraform"
# service connection
environmentServiceNameAzureRM: "${{ parameters.service_connection }}"
- bash: |
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: $[variables['Build.SourceBranchName']]
IS_TAG: $[startsWith(variables['Build.SourceBranch'], 'refs/tags/')]
- task: TerraformTaskV3@3
displayName: Terraform plan
inputs:
Expand All @@ -53,8 +59,8 @@ steps:
condition: |
and(
ne(variables['Build.SourceBranchName'], 'main'),
ne('${{ parameters.tag_type }}', 'test'),
ne('${{ parameters.tag_type }}', 'prod')
ne(variables['tag_type'], 'test'),
ne(variables['tag_type'], 'prod')
)
- task: TerraformTaskV3@3
displayName: Terraform apply
Expand All @@ -70,6 +76,6 @@ steps:
condition: |
or(
eq(variables['Build.SourceBranchName'], 'main'),
eq('${{ parameters.tag_type }}', 'test'),
eq('${{ parameters.tag_type }}', 'prod')
eq(variables['tag_type'], 'test'),
eq(variables['tag_type'], 'prod')
)

0 comments on commit 5866eee

Please sign in to comment.