Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cloudchristoph/0a8bf5d533900b2a738690bfb7593008 to your computer and use it in GitHub Desktop.
Save cloudchristoph/0a8bf5d533900b2a738690bfb7593008 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