I have a set of jobs that needs to be triggered only when branches are named by certain pattern. (Eg: ctl-sw-v01.01.01-vbeta, ctl-sw-v01.11.01-vbeta, ctl-sw-v11.01.01-vbeta, ctl-sw-v01.01.21-vbeta). To make this generic, I have a pattern developed using regex '~ /^ctl-sw-vd+.d+.d+(-vbeta)?$/'
. But I am finding it confusing as to how to specify this in the variables section in the yml file.
I was using as below:
trigger:
tags:
include:
- '*'
branches:
include:
- ctl-sw-v01.11.01-vbeta
pool:
vmImage: ubuntu-latest
variables:
isBranch: $[startsWith(variables['Build.SourceBranch'], 'refs/head/~ /^ctl-sw-vd+.d+.d+(-vbeta)?$/')]
jobs:
- job: A
condition: and(succeeded(), eq(variables.isBranch, 'true'))
steps:
- script: |
echo "hello"
- job: B
steps:
- script: |
echo "howdy"
The job A is being skipped continuously, I checked my regex, it produces a match.
What am I doing wrong here?
2
Answers
I don’t think you can use regular expressions in the startsWith expression.
A solution would be to create a separate step (in a job before job A) where you evaluate the variable isBranch with a script. This creates more code, but the benefit is that you can independently test the regex in the future which might save maintenance cost in the long run.
Moreover,
condition: and(succeeded(), eq(variables.isBranch, 'true'))
booleans are written simply asTrue
, not'true'
.As Bast said, it is not supported to use regular expressions as part of condition expressions in Azure DevOps.
In addition,
The branches should begins with
refs/heads/
, but you are usingrefs/head/
.To avoid mistake like this, I suggest you to use
Build.SourceBranchName
instead ofBuild.SourceBranch
.Build.SourceBranchName
ignores the file structure of the branch and just search for the branch name.Here is the example: