Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (29.3k 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 (50.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 

*/staging/*

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

Browse Categories

...