Overview
Hi everyone, I am Luu Cao Cuong, Cloud Engineer at VTI Japan.
If you are working with Azure Devops and want to create a pipeline which can be triggered from multiple source branches, with each source branche the pipeline will set variables, run jobs, tasks accordingly? Then this is the post you are looking for.
Technical solutions
To archive this, there are 2 methods that I will introduce in this post, you can chose one, or combine the two for your design purpose.
Method 1: Set variables depends on the trigger branch.
In this method, we set values for environment variables depends on the trigger branch.
Syntax:
variables:
${{ if eq( variables['Build.SourceBranchName'], 'branch1' ) }}:
my_variable : 'my_value_for_branch_1'
${{ if eq( variables['Build.SourceBranchName'], 'branch2' ) }}:
my_variable : 'my_value_for_branch_2'
With this setting, if the pipeline is trigged by "branch1", the value of my_variable will be "my_variable_for_branch1". If the pipeline is trigged by "branch2", the value of my_variable will be "my_variable_for_branch2".
Following is a sample of pipeline yaml file with 3 trigger branches:
trigger:
branches:
include:
- dev
- stg
- master
variables:
vmImageName: 'ubuntu-latest'
${{ if eq( variables['Build.SourceBranchName'], 'master' ) }}:
test_variable1: 'prd value 1'
test_variable2: 'prd value 2'
${{ if eq( variables['Build.SourceBranchName'], 'stg' ) }}:
test_variable1: 'stg value 1'
test_variable2: 'stg value 2'
${{ if eq( variables['Build.SourceBranchName'], 'dev' ) }}:
test_variable1: 'dev value 1'
test_variable2: 'dev value 2'
stages:
- stage: Build
displayName: Build stage
jobs:
- job: Build_test
displayName: Build Test
pool:
vmImage: $(vmImageName)
steps:
- task: Bash@3
displayName: Test task
inputs:
targetType: 'inline'
script: |
echo variable 1 = $(test_variable1)
echo variable 2 = $(test_variable2)
If the branch is triggered by "dev" branch, the result will look like this:
Method 2: Run Stage, Job, or task with "condition"
You can set a condition to run a Stage/Job/Task. In this case, we check the trigger branch name in the "condition" statement.
In the following example, I only use condition for jobs, however, you are free to add "condition" in Stage/Task if needed.
A sample of pipeline yaml file with 3 trigger branches:
trigger:
branches:
include:
- dev
- stg
- master
variables:
vmImageName: 'ubuntu-latest'
prd_test_variable1: 'prd value 1'
prd_test_variable2: 'prd value 2'
stg_test_variable1: 'stg value 1'
stg_test_variable2: 'stg value 2'
dev_test_variable1: 'dev value 1'
dev_test_variable2: 'dev value 2'
stages:
- stage: Build
displayName: Build stage
jobs:
- job: Build_dev
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/dev'))
displayName: Build DEV
pool:
vmImage: $(vmImageName)
steps:
- task: Bash@3
displayName: Bash dev
inputs:
targetType: 'inline'
script: |
echo variable 1 = $(dev_test_variable1)
echo variable 2 = $(dev_test_variable2)
- job: Build_stg
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/stg'))
displayName: Build STG
pool:
vmImage: $(vmImageName)
steps:
- task: Bash@3
displayName: Bash stg
inputs:
targetType: 'inline'
script: |
echo variable 1 = $(stg_test_variable1)
echo variable 2 = $(stg_test_variable2)
- job: Build_prd
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
displayName: Build PRD
pool:
vmImage: $(vmImageName)
steps:
- task: Bash@3
displayName: Bash prd
inputs:
targetType: 'inline'
script: |
echo variable 1 = $(prd_test_variable1)
echo variable 2 = $(prd_test_variable2)
If the branch is triggered by "dev" branch, only "Build DEV" job will be run, the other two are not. The result will look like this:
That's it!
Thank you for reading, hope it helps!
Leave a Reply