I have requirement to trigger pipeline when pull request is raised in ADO and when the triggered pipeline gets success, merge will be processed (using Branch policies). default value for environment parameter is NONPROD, but when pipeline auto triggers, I want to choose other environment based on target branch like NFT for develop and PROD for main.
I have tried with below code to set the variable and tried conditional parameter with predefined variable ['System.PullRequest.targetBranchName'], but looks like i am able to see expected target branch in output but condition is not executing.
variables:
- name: mode
value: 'apply'
- name: environment
${{ if eq( variables['System.PullRequest.targetBranchName'], 'main' ) }} :
value: 'PROD'
${{ if eq( variables['System.PullRequest.targetBranchName'], 'develop' ) }} :
value: 'NFT'
${{ else }}:
value: 'NONPROD'
Below is the stage where i am trying to check the values.
stages:
- stage: setprerequisites
displayName: "setprerequisites"
jobs:
- job: checkvariables
displayName: "check variables"
steps:
- script: |
echo "source working directory --> $(System.DefaultWorkingDirectory)"
echo "selected default environmentparameter --> ${{ parameters.environment }}"
echo "selected environment from variables --> $(environment)"
echo "target branch --> $(System.PullRequest.targetBranchName)"
echo "selected terraform mode to apply --> ${{ parameters.mode }}"
echo "terraform ${{ parameters.mode }} will be applied on $(environment) environment"
Below is the Output :
Thanks in advance
According to Predefined variables, variable System.PullRequest.targetBranchName
is not available in templates so the following won't work:
${{ if eq( variables['System.PullRequest.targetBranchName'], 'main' ) }}
You must use macro syntax $(var)
or a runtime expression instead $[variables.var]
.
Setting a pipeline output variable named ENVIRONMENT
and displaying it in the console:
trigger: none
stages:
- stage: setprerequisites
displayName: "setprerequisites"
jobs:
- job: checkvariables
displayName: "check variables"
steps:
- script: |
TARGET_BRANCH='$(System.PullRequest.targetBranchName)'
if [ $TARGET_BRANCH == "main" ]; then
echo "Found branch 'main', setting the environment to PROD"
echo "##vso[task.setvariable variable=ENVIRONMENT;isOutput=true]PROD"
elif [ $TARGET_BRANCH == "develop" ]; then
echo "Found branch 'develop', setting the environment to NFT"
echo "##vso[task.setvariable variable=ENVIRONMENT;isOutput=true]NFT"
else
echo "Couldn't find branches 'main' nor 'develop'. Setting the environment to NONPROD"
echo "##vso[task.setvariable variable=ENVIRONMENT;isOutput=true]NONPROD"
fi
displayName: "Set environment"
name: SetEnvironment
- script: |
echo "selected environment from variables --> $(SetEnvironment.environment)"
displayName: Check variables
Running the pipeline as part of a pull request (target branch: develop):
Running the pipeline manually:
Please note that $(SetEnvironment.environment)
is the correct syntax to use the output variable in other tasks within the same job.
To see how to use the variable in a different job/stage please refer to Levels of output variables.