How to read parameters sent by an upstream job in a downstream job?

Viewed 4544

I have an upstream pipeline job that triggers a downstream pipeline job, the upstream job sends a few parameters to the downstream job. How should I read these parameters in the downstream job? Both jobs are non-parameterized.

The upstream job sends parameters like so.

build job: 'downstream-job', 
        parameters: [[$class: 'StringParameterValue', name: 'Environment', value: "dev"]]

This answer says uses the variable directly but this doesn't seem to work. When I try using the variable Environment in the downstream job, I get an error saying.

groovy.lang.MissingPropertyException: No such property: Environment for class: groovy.lang.Binding
2 Answers

From the documentation here, it has to be params.Environment.

I think that the problem is with downstream job, because it's non-parameterized. And if downstream job is non-parameterized, it just ignores your parameters. From this page:

Parameterized Trigger Plugin Parameters (e.g. when using properties files as source) are only passed if they are defined on the downstream job. This is the behavior intended by the SECURITY-170, see Jenkins Security Advisory 2016-05-11

Also from description of Parameterized Trigger Plugin:

* YOU MUST DEFINE THE PARAMETER IN DOWNSTREAM JOBS VIA "This project is parameterized". For example, if job1 passes ABC=123 to job2 then in job2 mark the job as "This project is parameterized" and "Add Parameter" named "ABC". *

Related