Multibranch Pipeline: How to parameterize?

7,183 views
Skip to first unread message

Sverre Moe

unread,
Jun 8, 2016, 6:42:01 AM6/8/16
to Jenkins Users
Jenkins Pipeline can be parameterized, but not multibranch pipelines. There is no option for "This build is parameterized" in the configuration of a multibranch pipeline. Why is that?

I have a multibranch project for each repository with multiple branches.

ProjectA
    master
    develop
    releaseX
ProjectB
    master
    develop
    releaseX

In this case ProjectB has a dependency on ProjectA on the same branch.

The Jenkinsfile is sparse, while the common main build logic is stored in workflowLibs.git

When a upstream project builds a dependent downstream project I need to provide them with build parameters.
def BUILD_PROJECT = "projectB"+"/"+env.BUILD_BRANCH
build job
: BUILD_PROJECT, parameters: [[$class: 'BooleanParameterValue', name: 'UPSTREAM_TRIGGERED', value: true]]
I have some logic in the main build script for when the build is triggered by an upstream dependency.

Not sure how I can do this since multibranch pipeline cannot be parameterized.

I have even tried withEnv, but it did not work
withEnv(["UPSTREAM_TRIGGERED=true"]) {
    def BUILD_PROJECT = "projectB"+"/"+env.BUILD_BRANCH
    build BUILD_PROJECT
}

Sverre Moe

unread,
Jun 13, 2016, 4:20:55 AM6/13/16
to Jenkins Users
Either I am doing it wrong, or Multibranch pipeline has no way of sending parameters to downstream builds.
Considering that each branch within a multibranch pipeline seems to be a single Pipeline project and standard such Pipeline projects can be parameterized.

Sverre Moe

unread,
Jun 22, 2016, 2:27:13 AM6/22/16
to Jenkins Users
Each branch pipeline has their own config.xml. A standard pipeline job does have support for parameterization. I think I read somewhere in Jenkins JIRA that such properties should be defined in the Jenkinsfile and set for the branch pipeline project on the first run. I am a bit unsure how to set them. Cannot find a single example of this.


I know how to set them programatically in the Script console for "regular" projects.
    StringBuilder builder = new StringBuilder()
    builder.append("PROP1=true")
    builder.append("\n")
    builder.append("PROP2=true")
    final def propertiesContent = builder.toString()

    def info = new EnvInjectJobPropertyInfo(null, propertiesContent, null, null, null, false)
    def property = new EnvInjectJobProperty()
    property.setOn(true)
    property.setKeepJenkinsSystemVariables(true)
    property.setKeepBuildVariables(true)
    property.setInfo(info)

    project.addProperty(property)
    project.save()

Sverre Moe

unread,
Jun 22, 2016, 2:45:18 AM6/22/16
to Jenkins Users
Found some clue in https://issues.jenkins-ci.org/browse/JENKINS-32780
Using the Snippet code generator I was able to generate a build parameter
properties [[$class: 'ParametersDefinitionProperty', parameterDefinitions: [[$class: 'BooleanParameterDefinition', defaultValue: false, description: 'Force publish RPM artifacts to repository', name: 'FORCE_PUBLISH']]]]

If I want all branches to have this property, could I put it in my global script? I am trying to keep my Jenkinsfiles as DRY as possible.

Using the Snippet generator to "Prepare an environment for the run"
This is needed because of SECURITY-170, otherwised properties are not propagated to downstream builds.
It does not generate anything useful. Previously the result was empty whatever I put within "Properties Content". Now I only get
properties [<object of type org.jenkinsci.plugins.envinject.EnvInjectJobProperty>]
This is what the snippet generator gives me for any of the choices, script, file or content.

Sverre Moe

unread,
Jun 22, 2016, 3:49:32 AM6/22/16
to Jenkins Users
It seems there is no point with Envinject-plugin with Pipeline
There is an issue for removing it in the configuration screen.

Then this is no longer an issue for me. Using the properties context in the script I was able to add parameters to my multibranch pipeline.

Advait Chabukswar

unread,
Aug 4, 2016, 12:25:13 AM8/4/16
to Jenkins Users
Sverre, can you elabore a bit more on how you did this?

So, I am automating the creation and working of Multibranch Pipeline. I am able to create jobs through Job DSL and it works fine.
But I have some variables(or parameters) that I want to pass to the individual branches (and I want it to be automated on job creation). If I put it in the Jenkinsfile, it won't be scalable.

I do it for regular pipeline jobs by using the EnvInject plugin, can't use that here though.
Do you have any solutions for this?

Sverre Moe

unread,
Aug 4, 2016, 3:26:42 AM8/4/16
to Jenkins Users
I am using Pipeline Global Library along with Jenkinsfile. All build logic is in a centralized build script within Jenkins workflowLibs.git

Setting properties to be used on branches:
properties([[$class: 'ParametersDefinitionProperty', parameterDefinitions: [[$class: 'BooleanParameterDefinition', defaultValue: false, description: 'Parameter Text', name: 'MY_PARAM']]]])
The properties set during execution of a build will be available next time a build is started.

I used the following piece of code to check for this property in my build script
def myBoolParam = false
if (getBinding().hasVariable("MY_PARAM")) {
    myBoolParam
= Boolean.parseBoolean(MY_PARAM)
}

Reply all
Reply to author
Forward
0 new messages