4
votes

Would be grateful for a decent full code example of how to pass parameters (parameterized build) from JobA to JobB in Jenkins Pipeline plugin?

I am using a script like below and can not figure from the docs how to access parameters from JobA in say a build step shell script in JobB:

build job: 'JobA', parameters: [[$class: 'StringParameterValue', name: 'CVS_TAG', value: 'test']]

build job: 'JobB', parameters: [[$class: 'StringParameterValue', name: 'CVS_TAG', value: 'test']]

echo env.CVS_TAG  

Above gives an error:

groovy.lang.MissingPropertyException: No such property: CVS_TAG for class: groovy.lang.Binding

And can not access $CVS_TAG in a build step shell script in JobB.

Thanks

Per you responses I have also tried this unsuccessfully:

build job: 'JobA', parameters: [[$class: 'StringParameterValue', name: 'test_param', value: 'working']]

env.test_param=test_param

echo ${test_param}

The error is always:

groovy.lang.MissingPropertyException: No such property: test_param for class: groovy.lang.Binding at groovy.lang.Binding.getVariable(Binding.java:63)

1
This answer might give you a clue: stackoverflow.com/questions/37079913/…izzekil
Did you enable "This project is parameterized" in Build JobB? Also note that you can access the Parameters in this way ${CVS_TAG}.mrkernelpanic
Have a look into the answer stackoverflow.com/questions/37675194/…CSchulz
Thanks for the response but I am not able to get this working? Any working script examples using build job?gcloud
Any ideas what I am doing wrong? This seems like the most important feature when using a pipeline and there is very little in the way of doc's / example of how to use it?gcloud

1 Answers

3
votes

Upstream JobA:

//do something
env.CVS_TAG = 'test'
build job: 'JobB'

Downstream JobB:

import hudson.EnvVars
import org.jenkinsci.plugins.workflow.cps.EnvActionImpl
import hudson.model.Cause

def upstreamEnv = new EnvVars()
node {
    //if the current build is running by another we begin to getting variables
    def upstreamCause = currentBuild.rawBuild.getCause(Cause$UpstreamCause)
    if (upstreamCause) {
        def upstreamJobName = upstreamCause.properties.upstreamProject
        def upstreamBuild = Jenkins.instance
                                .getItemByFullName(upstreamJobName)
                                .getLastBuild()
        upstreamEnv = upstreamBuild.getAction(EnvActionImpl).getEnvironment()
    }
    def CVS_TAG = upstreamEnv.CVS_TAG
    echo CVS_TAG
}