0

Is it possible to use parameters to allow users to pass a git sha to a multi branch pipeline while defaulting to the head of the branch? Also I would only need to this function for the master branch.

I'm using ...
Jenkinsfile in code
Jenkins Declarative Pipeline

David Williams
  • 401
  • 3
  • 15

2 Answers2

1

I was able to do this with declarative pipelines with the following...

pipeline {
  options {
    skipDefaultCheckout()
  }
...
steps {
    script {
      if (GIT_REVISION=='HEAD') {
        checkout scm
      } else {
        checkout([$class: 'GitSCM',
            branches: [[name: "${params.GIT_REVISION}"]],
            doGenerateSubmoduleConfigurations: false,
            extensions: [],
            submoduleCfg: [],
            userRemoteConfigs: [[credentialsId: 'XXXXXXX', url: 'git@github.com:xxxxx/xxxxx.git']]
        ])
      }
      ...
    }
  }
}  
David Williams
  • 401
  • 3
  • 15
0

Yes, this is possible, but I guess you have to use scripted pipelines instead of declarative ones.

  • If the current branch is the master, you configure a parameter for this build (as this isn't super intuitive, I wrote a blog article a while ago). params.INPUT_REVISION for example would then store the given revision and you can set default to HEAD or fallback to it, if the parameter is not yet specified (e.g. for the first run).

  • You supply this revision to the checkout(scm) step as a parameter so that it doesn't checkout the current master branch, but the specified revision.

StephenKing
  • 36,187
  • 11
  • 83
  • 112