Branch specifier regex in Jenkins scripted pipeline

北城以北 提交于 2020-01-06 10:17:29

问题


Suppose i want to define pipeline for different branches under same scripted pipeline, how to define the regex for certain pattern of branches. Say for example :-

if(env.BRANCH_NAME ==~ /release.*/){
	 stage("Deploy"){
		echo 'Deployed release to QA'
	 }

Here i want to define that regex in such a way for any branch of the pattern

*release*

(meaning any branch with release string in it). How to achieve that?

And similarly how to achieve something like :-

if the branch is anything but develop, master, release(pattern).


回答1:


If you're using groovy you may use the following

if ((env.BRANCH_NAME =~ '.*release.*').matches()) {
    stage("Deploy"){
        echo 'Deployed release to QA'
    }
}

And if you want to match any branch name but develop, master or release, you may use the following regex

if ((env.BRANCH_NAME =~ '^((?!develop|master|release).)*$').matches()) {
    stage("Deploy"){
        echo 'Deployed release to QA'
    }
}



回答2:


You can use this regex for matching branch name like develop, release, hotfix.

if (branch_name =~ 'develop|hotfix.*|release.*') {

  stage("Deploy") {
        echo 'Deployed release to QA'
    }
}


来源:https://stackoverflow.com/questions/55151441/branch-specifier-regex-in-jenkins-scripted-pipeline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!