Skip to content

Instantly share code, notes, and snippets.

@rudolphjacksonm
Last active February 25, 2024 13:15
Show Gist options
  • Save rudolphjacksonm/132b240db0aa708033db59b87d3b2be2 to your computer and use it in GitHub Desktop.
Save rudolphjacksonm/132b240db0aa708033db59b87d3b2be2 to your computer and use it in GitHub Desktop.
trigger:
- none
variables:
- name: PROJECT_PATH
value: terraform
- name: tfComponent
value: cluster
- name: tfExecutionDir
value: '$(PROJECT_PATH)/src/$(tfComponent)'
- name: terraformVersion
value: 0.12.25
- name: serviceConnectionName
value: terraform-sp
- name: backendAzureRmResourceGroupName
value: terraformRG
- name: backendAzureRmStorageAccountName
value: terraformnsa
- name: backendAzureRmContainerName
value: tfstate
- name: backendAzureRmKey
value: 'terraform.tfstate'
- name: tfVarFileName
value: "../environment/dev.tfvars"
pool:
vmImage: 'ubuntu-latest'
stages:
- stage:
displayName: 'Terraform Plan'
jobs:
- job: TFPlan
steps:
- task: Cache@2
displayName: 'Register TF cache'
inputs:
key: terraform | $(Agent.OS) | "$(Build.BuildNumber)" | $(Build.SourceVersion) | $(tfComponent) | "$(tfVarFileName)"
path: $(tfExecutionDir)
- task: TerraformInstaller@0
displayName: 'Install Terraform'
inputs:
terraformVersion: '$(terraformVersion)'
- task: TerraformTaskV1@0
displayName: 'Terraform Init'
inputs:
provider: 'azurerm'
command: 'init'
workingDirectory: '$(tfExecutionDir)'
backendServiceArm: '$(serviceConnectionName)'
backendAzureRmResourceGroupName: '$(backendAzureRmResourceGroupName)'
backendAzureRmStorageAccountName: '$(backendAzureRmStorageAccountName)'
backendAzureRmContainerName: '$(backendAzureRmContainerName)'
backendAzureRmKey: '$(backendAzureRmKey)'
- task: TerraformTaskV1@0
displayName: 'Terraform Plan'
inputs:
provider: 'azurerm'
command: 'plan'
commandOptions: '-var-file=../zone/sandboxuks1.tfvars -out=${BUILD_BUILDNUMBER}.tfplan'
workingDirectory: '$(tfExecutionDir)'
environmentServiceNameAzureRM: '$(serviceConnectionName)'
- stage:
displayName: 'Terraform Apply'
jobs:
- deployment: DeployDev
displayName: 'Terraform Apply'
pool:
vmImage: 'ubuntu-latest'
environment: 'terraform'
strategy:
runOnce:
deploy:
steps:
- checkout: none
- task: Cache@2
displayName: 'Get Cache for TF Artifact'
inputs:
key: terraform | $(Agent.OS) | "$(Build.BuildNumber)" | $(Build.SourceVersion) | $(tfComponent) | "$(tfVarFileName)"
path: $(tfExecutionDir)
- task: TerraformInstaller@0
displayName: 'Install Terraform'
inputs:
terraformVersion: '$(terraformVersion)'
- task: TerraformTaskV1@0
displayName: 'Terraform Apply'
inputs:
provider: 'azurerm'
command: 'apply'
commandOptions: '${BUILD_BUILDNUMBER}.tfplan' # pass plan file to terraform apply
workingDirectory: '$(tfExecutionDir)'
environmentServiceNameAzureRM: '$(serviceConnectionName)'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment