MBeck
MBeck

Reputation: 183

Jenkins workflow job: Use parameter as branch specifier

I want to migrate our old free style dev builds, in which we use the branch name as a build parameter, to workflow builds. This works fine so far, the only thing we are really missing is the ability to use the parameter, e.g. "branch_name", as the branch specifier for the Workflow script from SCM section. On a free style build this works fine. Any ideas how this could be achieved? We don't want a dev to change the configuration all the time before starting a build.

Upvotes: 18

Views: 16471

Answers (4)

Mikalai Parafeniuk
Mikalai Parafeniuk

Reputation: 1356

Try to disable "Lightweight checkout" checkbox.

Lightweight checkout screenshot

This behavior is noted when clicking the help question mark for the "Lightweight checkout" option:

Also build parameters will not be substituted into SCM configuration in this mode.

Found that in latest comments of JENKINS-28447

Upvotes: 46

Jesse Glick
Jesse Glick

Reputation: 25461

Sounds like JENKINS-28447:

When selecting the "Groovy CPS DSL from SCM" option for a worflow job, the SCM plugins do not appear to resolve build parameters or environment variables. I am using the git plugin and when I use it from other jobs I can specify a build parameter, like "BuildBranch", and use that when specifying what branch should be built

The workaround would be to use an inline bootstrap script that calls load after checkout, as described in the tutorial.

Upvotes: 2

Craig Rodrigues
Craig Rodrigues

Reputation: 821

I have a workflow DSL script described here: https://groups.google.com/forum/#!msg/jenkinsci-users/jSKwSKbaXq8/dG2mn6iyDQAJ

In that script, I have a build parameter called FREEBSD_SRC_URL , which is passed down to the workflow. Based on different parameters in that URL, a different branch can be checked out.

If you are using git, you can still use the same technique for passing a build parameter down to the script, but you would need to do things slightly differently. For example, you could define a parameter BRANCH_NAME in your job and do something like this in your workflow script:

String checkout_url = "https://github.com/jenkinsci/jenkins"
String branch_name = "master"

if (getBinding().hasVariable("CHECKOUT_URL")) {
    // override default URL from build parameter
    checkout_url = CHECKOUT_URL
}
if (getBinding().hasVariable("BRANCH_NAME")) {
    // override default branch from build parameter
    branch_name = BRANCH_NAME
}

node {
    // Do the git checkout
    git branch: "${branch_name}", url: "${checkout_url}"
}

Upvotes: 1

Jesse Glick
Jesse Glick

Reputation: 25461

Alternately, go further by creating a multibranch workflow project, so that each branch is built automatically with its own history.

Upvotes: 0

Related Questions