Some re-usable Azure Pipelines snippets I use acrossed my Azure Pipelines.
Name | Template |
---|---|
Set Custom Variable | steps/set-custom-variable.yml |
Combined Docker login, build, push and logout Tasks | steps/docker-build-push.yml |
Lock Azure Container Registory Image (makes immutable) | steps/lock-acr-image.yml |
Deploy Container to Azure App Service | steps/deploy-app-service.yml |
- Official Docs - Azure DevOps > Template types & usage
- Example pipeline - julie-ng/azure-nodejs-demo/azure-pipelines.yml
In your azure-pipelines.yml
file for your project, add a reference to this repository:
# File: azure-pipelines.yml
resources:
repositories:
- repository: templates # for reference
type: github
name: julie-ng/azure-pipelines-templates
ref: refs/heads/master
Note: @templates
suffix always refers to repository name from setup as described above.
Sometimes it's helpful to set a custom variable at runtime
based on output. For example, you need project version or the git commit of the build. This task encapsulates the clunky ##vso[task.setvariable…]
syntax for you.
For example, here we set the git-sha
variable to output of git rev-parse --short HEAD
command:
# File: azure-pipelines.yml
steps:
- template: steps/set-custom-variable.yml@templates
parameters:
variableName: git-sha
command: 'git rev-parse --short HEAD'
The result, for example 8cd076e
may be useful for logging and auditing your project by tagging images.
This template combines the following docker tasks:
- registry login
- build and tag image
- push image with tag(s)
- registry logout
The example below takes the $(git-sha)
variable and uses it as a Docker tag. This example pushes a single image with tagged both by its git sha and the version 1.0.0
steps:
- template: steps/docker-build-push.yml@templates
parameters:
registryConnectionName: 'acr-or-docker-hub-connection'
imageName: 'my-app'
tagsAsMultilineString: |
$(git-sha)
'1.0.0'
Note the parameter is called tagsAsMultilineString
which is due to limitations of Azure DevOps. A YAML object would make more sense to me. But the underlying Azure DevOps task only takes multi-line strings 🤷♀️
This template locks an image in the Azure Container Registry preventing it from being deleted.
Example use:
steps:
- template: steps/lock-acr-image.yml@templates
parameters:
azureArmConnection: 'azure-arm-connection'
acrRegistryName: 'myregistry'
imageName: 'my-app'
imageTag: 'v1.0'
condition: succeeded() # optional
Deploys a container to Azure App Service, Web App for Containers. Works for both Docker Hub and Azure Container Registry images.
Example use:
steps:
- template: steps/deploy-app-service.yml@templates
parameters:
ARMConnectionName: 'arm-service-connection'
dockerImage: 'image-name:v1.0'
appName: 'myapp-dev'