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/advancedPowerShell:
/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
      - 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.
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: setTargetEnvironmentValueThe 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.
Replace the main.yml with the one called main-more-targets.yml. It's okay to rename main-more-targets.yml.
Its locations in the sample scripts are:
Bash:
/V2/bash/github/advancedPowerShell:
/V2/powershell/github/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.
Now go to the repository in GitHub, and click on the Settings section.
Expand secrets and variables in the left-hand menu titled
Securityand click onActions.Now 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:
# Trigger when committing to main branch
on:
  push:
    branches:
      - main
      - flexible
  workflow_dispatch: # Allow manual triggering of the workflowHere you can change when a deployment is trigger 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.
jobs:
  # resolve which environment to deploy to based on triggering branch
  set-env:
    runs-on: ubuntu-latest
    outputs:
      targetEnvironmentAlias: ${{ steps.set.outputs.targetEnvironmentAlias }}
    steps:
      - name: Resolve Target Environment
        id: set
        run: |
          echo "Triggering branch: ${{ github.ref_name }}"
          if [[ "${{ github.ref_name }}" == "main" ]]; then
            echo "Target is: $leftmostMainline"
            echo "targetEnvironmentAlias=$leftmostMainline" >> $GITHUB_OUTPUT
          elif [ "${{ github.ref_name }}" = "flexible" ]; then
            echo "Target is: $flexible"
            echo "targetEnvironmentAlias=$flexible" >> $GITHUB_OUTPUT
          else
            echo "no target environment defined for branch: $(Build.SourceBranchName)"
            exit 1
          fi
        env:
          leftmostMainline: ${{ vars.TARGET_ENVIRONMENT_ALIAS}}
          flexible: ${{ vars.FLEXIBLE_ENVIRONMENT_ALIAS }}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 then used by later jobs.
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?