Advanced Setup: Deploy to multiple targets
In this example, you will learn how to target deployments to more than one environment.
The sample will enable you to work on two different branches, where each branch will deploy to a different environment.
Replace the azure-release-pipeline.yaml
with the one called azure-release-pipeline-more-targets.yaml
. It's okay to rename azure-release-pipeline-more-targets.yaml
.
Its locations in the sample scripts are:
Bash:
/V2/bash/azuredevops/advanced
PowerShell:
/V2/powershell/azuredevops/advanced
Make sure you don't have multiple YAML files that contain triggers (unless you designed your pipeline's workflow that way).
Now you need the aliases of the environments you want to target.
Insert the aliases into the placeholders in azure-release-pipeline-more-targets.yaml
, the values you need to replace are:
##Your target environment alias here##
##Your other target environment alias here##
Remember to fix the projectId placeholder if you haven't already: ##Your project ID here##
Next, look at the triggers for the pipeline:
# Trigger when committing to main or flexible branch
trigger:
batch: true
branches:
include:
- main
- flexible
Here, you can change when a deployment is triggered based on which branch is pushed to.
The pipeline needs to resolve the target based on the triggering branch, which is done in the following code.
stages:
# resolve which environment to deploy to based on triggering branch
- stage: resolveTarget
displayName: Resolve Target Environment
jobs:
- job: setTargetEnvironment
steps:
- script: |
echo "Triggering branch: $(Build.SourceBranchName)"
if [ "$(Build.SourceBranchName)" = "main" ]; then
echo "Target is: $(targetEnvironmentAlias)"
echo "##vso[task.setvariable variable=targetEnvironment;isOutput=true]$(targetEnvironmentAlias)"
elif [ "$(Build.SourceBranchName)" = "flexible" ]; then
echo "Target is: $(flexibleTargetEnvironmentAlias)"
echo "##vso[task.setvariable variable=targetEnvironment;isOutput=true]$(flexibleTargetEnvironmentAlias)"
else
echo "no target environment defined for branch: $(Build.SourceBranchName)"
exit 1
fi
name: setTargetEnvironmentValue
The triggering branch is evaluated in the statement if [ "$(Build.SourceBranchName)" = "main" ]; then
or elif [ "$(Build.SourceBranchName)" = "flexible" ]; then
.
The code will write which alias is targeted and write a pipeline variable (targetEnvironment
). This variable is then used by later steps.
When updating the triggering branch names, it must be updated in the two mentioned places: In the trigger and in the script.
Last updated
Was this helpful?