skip to Main Content

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


  1. 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 as True, not 'true'.

    Login or Signup to reply.
  2. 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 using refs/head/.

    To avoid mistake like this, I suggest you to use Build.SourceBranchName instead of Build.SourceBranch. Build.SourceBranchName ignores the file structure of the branch and just search for the branch name.

    Here is the example:

    variables['Build.SourceBranch'], 'refs/heads/main'
    
    variables['Build.SourceBranchName'], 'main'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search