Last active
February 10, 2022 13:53
-
-
Save techdecline/94dfe748a0561e58e1c82896d4d17ca0 to your computer and use it in GitHub Desktop.
AzDevOps Terraform Pipeline Template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| trigger: | |
| - master | |
| - feature/* | |
| pr: | |
| branches: | |
| include: | |
| - '*' | |
| pool: | |
| vmImageName: 'ubuntu-latest' | |
| variables: | |
| - group: tfstate | |
| - name: tf_version | |
| value: '1.1.5' | |
| stages: | |
| - stage: TerraformPlan | |
| displayName: TerraformPlan | |
| jobs: | |
| - job: TerraformValidate | |
| displayName: TerraformValidate | |
| steps: | |
| - task: ms-devlabs.custom-terraform-tasks.custom-terraform-installer-task.TerraformInstaller@0 | |
| displayName: 'Install Terraform' | |
| inputs: | |
| terraformVersion: $(tf_version) | |
| - task: AzureCLI@2 | |
| inputs: | |
| azureSubscription: "AzDevOps-MSDN" | |
| scriptType: 'pscore' | |
| scriptLocation: 'inlineScript' | |
| addSpnToEnvironment: true | |
| inlineScript: | | |
| $env:ARM_CLIENT_ID=$env:servicePrincipalId | |
| $env:ARM_CLIENT_SECRET=$env:servicePrincipalKey | |
| $env:ARM_TENANT_ID=$env:tenantId | |
| terraform init | |
| displayName: "Terraform Init" | |
| - task: AzureCLI@2 | |
| inputs: | |
| azureSubscription: "AzDevOps-MSDN" | |
| scriptType: 'pscore' | |
| scriptLocation: 'inlineScript' | |
| addSpnToEnvironment: true | |
| inlineScript: | | |
| terraform validate | |
| displayName: "Terraform Validate" | |
| - task: AzureCLI@2 | |
| inputs: | |
| azureSubscription: "AzDevOps-MSDN" | |
| scriptType: 'pscore' | |
| scriptLocation: 'inlineScript' | |
| addSpnToEnvironment: true | |
| inlineScript: | | |
| $env:ARM_CLIENT_ID=$env:servicePrincipalId | |
| $env:ARM_CLIENT_SECRET=$env:servicePrincipalKey | |
| $env:ARM_TENANT_ID=$env:tenantId | |
| terraform plan -out="plan.tfplan" | |
| displayName: "Terraform Plan" | |
| - task: ArchiveFiles@2 | |
| condition: eq(variables['Build.SourceBranchName'], 'master') | |
| inputs: | |
| rootFolderOrFile: 'plan.tfplan' | |
| includeRootFolder: false | |
| archiveType: 'tar' | |
| tarCompression: 'gz' | |
| archiveFile: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).tgz' | |
| replaceExistingArchive: true | |
| displayName: 'Create Plan Artifact' | |
| - task: PublishBuildArtifacts@1 | |
| condition: eq(variables['Build.SourceBranchName'], 'master') | |
| inputs: | |
| PathtoPublish: '$(Build.ArtifactStagingDirectory)' | |
| ArtifactName: '$(Build.BuildId)-tfplan' | |
| publishLocation: 'Container' | |
| displayName: 'Publish Plan Artifact' | |
| - stage: TerraformApply | |
| dependsOn: [TerraformPlan] | |
| condition: and(succeeded('TerraformPlan'), eq(variables['Build.SourceBranchName'], 'master')) | |
| displayName: TerraformApply | |
| jobs: | |
| - job: TerraformApply | |
| displayName: TerraformApply | |
| steps: | |
| - task: ms-devlabs.custom-terraform-tasks.custom-terraform-installer-task.TerraformInstaller@0 | |
| displayName: 'Install Terraform' | |
| inputs: | |
| terraformVersion: $(tf_version) | |
| - task: AzureCLI@2 | |
| inputs: | |
| azureSubscription: "AzDevOps-MSDN" | |
| scriptType: 'pscore' | |
| scriptLocation: 'inlineScript' | |
| addSpnToEnvironment: true | |
| inlineScript: | | |
| $env:ARM_CLIENT_ID=$env:servicePrincipalId | |
| $env:ARM_CLIENT_SECRET=$env:servicePrincipalKey | |
| $env:ARM_TENANT_ID=$env:tenantId | |
| terraform init | |
| displayName: "Terraform Init" | |
| - task: DownloadBuildArtifacts@0 | |
| inputs: | |
| artifactName: '$(Build.BuildId)-tfplan' | |
| displayName: 'Download Plan Artifact' | |
| - task: ExtractFiles@1 | |
| inputs: | |
| archiveFilePatterns: '$(System.ArtifactsDirectory)/$(Build.BuildId)-tfplan/$(Build.BuildId).tgz' | |
| destinationFolder: '$(System.DefaultWorkingDirectory)/' | |
| cleanDestinationFolder: false | |
| displayName: 'Extract Terraform Plan Artifact' | |
| - task: AzureCLI@2 | |
| inputs: | |
| azureSubscription: "AzDevOps-MSDN" | |
| scriptType: 'pscore' | |
| scriptLocation: 'inlineScript' | |
| addSpnToEnvironment: true | |
| inlineScript: | | |
| $env:ARM_CLIENT_ID=$env:servicePrincipalId | |
| $env:ARM_CLIENT_SECRET=$env:servicePrincipalKey | |
| $env:ARM_TENANT_ID=$env:tenantId | |
| terraform apply -input=false "plan.tfplan" | |
| displayName: "Terraform Apply" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment