0 votes
1 view
in DevOps and Agile by (30k points)

I am trying to detect the branch pattern on a when statement inside a stage.

Like this:

stage('deploy to staging') {

agent label:'some-node'

when { branch "feature/*" }

steps {

    sh './deploy_pr.sh'



What if I want a more complicated pattern?

I am trying to detect something like feature/0.10.25 and the following pattern doesn't work:

when { branch 'feature/[0-9]+.[0-9]+.[0-9]+' }

It doesn't work. And it's a correct regexp, according to https://regexr.com/

1 Answer

0 votes
by (51.2k points)

With that branch option, Jenkins compares with ant style patterns so 

refer: https://ant.apache.org/manual/dirtasks.html

 Which clearly says that it doesn’t expect the regexp, but it can be done with 


To resolve this issue use when-expression option

 when { 

            expression { BRANCH_NAME ==~ /feature\/[0-9]+\.[0-9]+\.[0-9]+/ }


That uses the Groovy expression

Reference: https://www.regular-expressions.info/groovy.html

Welcome to Intellipaat Community. Get your technical queries answered by top developers !