Setting up branch for git SCM checkout based on custom logic

I have the following use case

  1. I have a jenkins pipeline that can trigger either by Bitbucket Push Pull Request Plugin or via Manual Trigger
  2. I use Git SCM plugin to checkout the code to build. The problem here is under different conditions my branch to be checkout will be different
    a . Manual Trigger → Branch input by user
    b. Bitbucket Pull Request create/update → Source Branch of PR (taken from env variables)
    c. Bitbucket Pull Request merge → Destionation Branch of PR (taken from env variables)

Is there a way i can set a config for the SCM to choose branch on the basis of this logic. Trying a regex in branch section didn’t help

I need this feature in order to put my Jenkinsfile as a part of the repo itself. Otherwise currently my jenkinsfile resides in a separate repo which the SCM plugin checks out and inside the pipeline script i checkout the actual repo based on the above conditions.

Well, what you can do is store the “Branch” you’d like to checkout in a variable first, and then call the Git SCM.

like:

def BRANCH = 'development'

checkout([$class: 'GitSCM', branches: [[name: BRANCH ]], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: '']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'myJenkinsCredentials', url: 'ssh://git@bitbucket.myhost.com' ]] ])

The BRANCH variable can also be an “option” or “parameter”

1 Like