Advanced Setup: Deploy to multiple targets
Learn how to set up your CI/CD pipeline to include more than one target environment.
The sample will enable you to work on two different branches, where each of the branches deploys to a different environment.
With the CI/CD flow, you can trigger deployments to multiple environments simultaneously.
If a deployment is already in progress to a named environment, it will not be possible to trigger another until the first is done.
Replace the
azure-release-pipeline.yamlwith the file calledazure-release-pipeline-more-targets.yaml. It's okay to renameazure-release-pipeline-more-targets.yaml.The file can be found in these locations within the sample files:
Bash:
/V2/bash/azuredevops/advancedPowerShell:
/V2/powershell/azuredevops/advanced
Ensure you don't have multiple YAML files that contain triggers (unless you designed your pipeline's workflow that way).
Fetch the aliases of the environments you want to target from the Cloud portal.
Insert the aliases into the following placeholders in
azure-release-pipeline-more-targets.yaml:##Your target environment alias here####Your other target environment alias here##
Fill in the
projectIdplaceholder if you haven't already:##Your project ID here##.Look at the triggers for the pipeline:
# Trigger when committing to main or flexible branch
trigger:
batch: true
branches:
include:
- main
- flexibleHere, 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:
The triggering branch is evaluated in the if [ "$(Build.SourceBranchName)" = "main" ]; then or elif [ "$(Build.SourceBranchName)" = "flexible" ]; then statement.
The code will write which alias is targeted and write a pipeline variable (targetEnvironment). This variable is used by steps later.
When updating the triggering branch names, it must be updated in the two mentioned places:
The trigger
The script
Replace the
main.ymlwith the one calledmain-more-targets.yml. It's okay to renamemain-more-targets.yml.The file can be found in these locations within the sample files:
Bash:
/V2/bash/github/advancedPowerShell:
/V2/powershell/github/advanced
Ensure you don't have multiple YAML files that contain triggers (unless you designed your pipeline's workflow that way).
Fetch the aliases of the environments you want to target from the Cloud portal.
Go to the repository in GitHub, and navigate to the Settings section.
Expand Secrets and Variables in the left-hand menu titled
Securityand click onActions.Go to the Variables tab.
Add a
repository variablecalledFLEXIBLE_ENVIRONMENT_ALIASand enter the environment alias you selected earlier.
If you followed the GitHub guide, you should already have a variable called TARGET_ENVIRONMENT_ALIAS.
Next, look at the triggers for the pipeline:
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. This is done with the following code:
The triggering branch is evaluated in the statement if [[ "${{ github.ref_name }}" == "main" ]]; then or elif [ "${{ github.ref_name }}" = "flexible" ]; then.
The code will write which alias is targeted and write a pipeline variable (targetEnvironmentAlias). This variable is used by jobs later.
When updating the triggering branch names, it must be updated in the two mentioned places:
The trigger
The script
Last updated
Was this helpful?