Search code examples
jenkinsjenkins-pipelinejenkins-groovy

How to select jenkinsfile depending on branch name


I have a single large code base which is using for build and deployment apps for different countries. Each country has its own unique building/deployment pipeline. That's why I would like to specify dedicated jenkins script file for each country. For example, branch name contains _UK_, in this case I would like to use _UK_Jenkinsfile, if branch name containes _US_ I'll create a new _US_Jenkinsfile etc.

So, the question is - how to specify Jenkinsfile depending on branch name?

enter image description here


Solution

  • You can use filter branches inside jenkinsfile by using when filter:

    when {
      branch prod
    }
    
    when {
      expression { BRANCH_NAME ==~ /(production|staging)/ }
    }
    

    More examples are here