gcloud
gcloud

Reputation: 41

Jenkins Pipeline Plugin pass build Job parameters between jobs started in a pipeline

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)

Upvotes: 4

Views: 7660

Answers (1)

Anton Shishkin
Anton Shishkin

Reputation: 1796

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
}

Upvotes: 3

Related Questions