Skip to content

Instantly share code, notes, and snippets.

@danlister
Last active May 5, 2021 08:41
Show Gist options
  • Save danlister/891905cca25a45432a66ffb90e17dd8b to your computer and use it in GitHub Desktop.
Save danlister/891905cca25a45432a66ffb90e17dd8b to your computer and use it in GitHub Desktop.
Multi-Stage Pipeline (Terraform and .NET Core App)
trigger: none
pr:
branches:
include:
- master
variables:
- name: COMPONENT_NAME
value: app
- name: PROJECT_PATH
value: MyApplication.App.csproj
stages:
- stage: Build
variables:
- group: product
- group: environment-prd
- group: terraform-prd
- group: region-uksouth
jobs:
- job: Plan
displayName: 'Plan & Package Terraform'
pool:
vmImage: ubuntu-latest
steps:
- task: AzureCLI@1
displayName: 'Deploy Terraform Resources'
inputs:
azureSubscription: sub-production
scriptPath: '$(Build.SourcesDirectory)/common/deploy-terraform-resources.sh'
- task: TerraformInstaller@0
displayName: 'Install Terraform'
inputs:
terraformVersion: '0.14.6'
- task: TerraformTaskV1@0
displayName: 'Initialise Terraform'
inputs:
provider: 'azurerm'
command: 'init'
workingDirectory: '$(Build.SourcesDirectory)/$(COMPONENT_NAME)/deploy'
backendServiceArm: sub-production
backendAzureRmResourceGroupName: '$(TERRAFORM_RESOURCE_GROUP)'
backendAzureRmStorageAccountName: '$(TERRAFORM_STORAGE_ACCOUNT)'
backendAzureRmContainerName: '$(COMPONENT_NAME)'
backendAzureRmKey: 'terraform.tfstate'
- task: CmdLine@2
displayName: 'Validate Terraform'
inputs:
script: 'terraform validate'
workingDirectory: '$(Build.SourcesDirectory)/$(COMPONENT_NAME)/deploy'
- task: TerraformTaskV1@0
displayName: 'Plan Terraform'
inputs:
provider: 'azurerm'
command: 'plan'
workingDirectory: '$(Build.SourcesDirectory)/$(COMPONENT_NAME)/deploy'
environmentServiceNameAzureRM: sub-production
- task: CopyFiles@2
displayName: 'Copy Files'
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
inputs:
SourceFolder: '$(Build.SourcesDirectory)'
TargetFolder: '$(Build.ArtifactStagingDirectory)'
Contents: |
common/deploy-terraform-resources.sh
$(COMPONENT_NAME)/deploy/**/*.tf
!$(COMPONENT_NAME)/deploy/.terraform
- task: PublishBuildArtifacts@1
displayName: 'Publish Artifact'
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
ArtifactName: 'deploy'
- job: Build
displayName: 'Build & Package'
dependsOn: Plan
pool:
vmImage: ubuntu-latest
steps:
- task: DotNetCoreCLI@2
displayName: 'Build App'
inputs:
command: 'build'
arguments: '--configuration Release'
projects: '$(Build.SourcesDirectory)/$(COMPONENT_NAME)/src/$(PROJECT_PATH)'
- task: DotNetCoreCLI@2
displayName: 'Publish App'
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
inputs:
command: 'publish'
projects: '$(Build.SourcesDirectory)/$(COMPONENT_NAME)/src/$(PROJECT_PATH)'
arguments: '--configuration Release --output $(Build.ArtifactStagingDirectory)'
publishWebProjects: false
zipAfterPublish: false
modifyOutputPath: false
- task: PublishBuildArtifacts@1
displayName: 'Publish Artifact'
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
ArtifactName: 'app'
- stage: Prod
dependsOn: Build
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
variables:
- group: product
- group: environment-prd
- group: terraform-prd
- group: region-uksouth
jobs:
- deployment: Prod_Deployment
displayName: 'Apply Terraform'
pool:
vmImage: ubuntu-latest
environment: myapplication-prd
strategy:
runOnce:
deploy:
steps:
- download: current
artifact: deploy
- task: AzureCLI@1
displayName: 'Deploy Terraform Resources'
inputs:
azureSubscription: sub-production
scriptPath: '$(Pipeline.Workspace)/deploy/common/deploy-terraform-resources.sh'
- task: TerraformInstaller@0
displayName: 'Install Terraform'
inputs:
terraformVersion: '0.14.6'
- task: TerraformTaskV1@0
displayName: 'Initialise Terraform'
inputs:
provider: 'azurerm'
command: 'init'
workingDirectory: '$(Pipeline.Workspace)/deploy/$(COMPONENT_NAME)/deploy'
backendServiceArm: sub-production
backendAzureRmResourceGroupName: '$(TERRAFORM_RESOURCE_GROUP)'
backendAzureRmStorageAccountName: '$(TERRAFORM_STORAGE_ACCOUNT)'
backendAzureRmContainerName: '$(COMPONENT_NAME)'
backendAzureRmKey: 'terraform.tfstate'
- task: TerraformTaskV1@0
displayName: 'Apply Terraform'
inputs:
provider: 'azurerm'
command: 'apply'
workingDirectory: '$(Pipeline.Workspace)/deploy/$(COMPONENT_NAME)/deploy'
commandOptions: '-auto-approve'
environmentServiceNameAzureRM: sub-production
- deployment: Prod_Deployment_App
displayName: 'Deploy App'
dependsOn: Prod_Deployment
pool:
vmImage: ubuntu-latest
environment: myapplication-prd
strategy:
runOnce:
deploy:
steps:
- download: current
artifact: app
- task: AzureRmWebAppDeployment@4
displayName: 'Deploy App'
inputs:
ConnectionType: 'AzureRM'
azureSubscription: sub-production
appType: 'webApp'
WebAppName: 'as-$(PRODUCT)-$(COMPONENT_NAME)-prd-$(REGION_SHORT)'
packageForLinux: '$(Pipeline.Workspace)/app'
enableCustomDeployment: true
DeploymentType: 'zipDeploy'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment