Azure DevOps yaml pipeline cheatsheet
A couple remarks
- Use a single pipeline for CI-CD, as there seems to be no way to connect BuildId (i.e., container version/tag to deploy) from CI pipeline to CD pipeline.
- The environment referenced in the deployment jobs are created if doesn’t exist
- The environment is where the approvals are configured, directly in Azure DevOps
- The pipeline is linked to a git repo in Azure DevOps as well.
- The pipeline is triggered when the master branch is pushed.
- The pipeline does:
- build and push container image to ACR
- deploy to
dev
environment - deploy to
int
environment
- The Service Connection (in this case AzDevOps.ServiceConnection), needs to be created.
Sample two-environments pipeline
The example is for a netcore app running in a container.
# Docker
# Build and push an image to Azure Container Registry
# https://docs.microsoft.com/azure/devops/pipelines/languages/docker
trigger:
- master
resources:
- repo: self
variables:
# Container registry service connection established during pipeline creation
dockerRegistryServiceConnection: 'ra076bb1-83c4-4bff-9c7d-d2e1b7918498'
imageRepository: 'bustroker.yamlpipelines.webapi'
containerRegistry: 'bustrokeryamlpipelines.azurecr.io'
dockerfilePath: '$(Build.SourcesDirectory)/Bustroker.YamlPipelines.WebApi/Dockerfile'
tag: '$(Build.BuildId)'
# Agent VM image name
vmImageName: 'ubuntu-latest'
stages:
- stage: BuildStage
displayName: Build
jobs:
- job: Build
displayName: Build and push
pool:
vmImage: $(vmImageName)
steps:
- task: Docker@2
displayName: Build and push an image to container registry
inputs:
command: buildAndPush
repository: $(imageRepository)
dockerfile: $(dockerfilePath)
containerRegistry: $(dockerRegistryServiceConnection)
tags: |
$(tag)
- stage: DevDeployStage
dependsOn: BuildStage
displayName: Dev
jobs:
- deployment: DevDeploymentJob
displayName: Deploy to dev
environment: dev
strategy:
runOnce:
deploy:
steps:
- task: AzureWebAppContainer@1
inputs:
azureSubscription: 'AzDevOps.ServiceConnection'
appName: 'bustroker-yamlpipelines-webapi-dev'
containers: '$(containerRegistry)/$(imageRepository):$(tag)'
appSettings: '-ASPNETCORE_ENVIRONMENT dev'
- stage: IntDeployStage
dependsOn: DevDeployStage
displayName: Int
jobs:
- deployment: IntDeploymentJob
displayName: Deploy to int
environment: int
pool:
vmImage: $(vmImageName)
strategy:
runOnce:
deploy:
- task: AzureWebAppContainer@1
inputs:
azureSubscription: 'AzDevOps.ServiceConnection'
appName: 'bustroker-yamlpipelines-webapi-int'
containers: '$(containerRegistry)/$(imageRepository):$(tag)'
appSettings: '-ASPNETCORE_ENVIRONMENT int'