Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pipeline jobs - pass parameters upstream?

TL;DR: Obviously in a Jenkins pipeline job you can easily pass parameters downstream. What I want to know is if you can pass them upstream.

Use case:

We have three jobs; job_one, job_two, and job_three. These are frequently run separately as only one stage is needed, but in increasingly more-frequent cases we'd like to be able to run all three back to back.

The first and second rely on parameters you can define ahead of time, but the third needs a parameter that is generated from the second job (a file name whose structure is unknown until job_two runs).

I have built umbrella, which calls something like the following for each job. In this case, PARAM1 is populated because umbrella runs as "Build with parameters".

build job: 'job_one', parameters: [[$class: 'StringParameterValue', name: 'PARAM1', value: "$PARAM1"]]

All fine and dandy, I can then use PARAM1 in job_one just fine.

The Problem:

For job_three I need the parameter filename. This is generated within job_two, and therefore from what I can tell is inaccessible because job_three has no idea what job_two is doing.

In an ideal world I would just have job_two pass the filename to the umbrella job, which would feed it back into job_three. Therefore, how can I pass the generated filename back up to the umbrella job?

I'm picturing a final script something like this;

node('on-demand-t2small'){

    stage ('Build 1') {

        build job: 'job_one', parameters: [[$class: 'StringParameterValue', name: 'PARAM1', value: "$PARMA1"]]
}
    stage ('Build 2') {

        build job: 'job_two', parameters: [[$class: 'StringParameterValue', name: 'PARAM2', value: "$PARMA2"]]

    //somehow get the filename parameter out of job_two here so that I can move it to job three...
} 
    stage ('Build 3') {

        build job: 'job_three', parameters: [[$class: 'StringParameterValue', name: 'filename', value: "$filename"]]
} }

Additional Notes:

I recognize that the first question will be "why not have job_two trigger job_three? I can't set the system up this way for two reasons;

  1. job_two needs to be able to run without triggering job_three, and three can't always require two's input to run.
  2. I debated having the umbrella kick off two and then have a clause in two that would trigger three ONLY IF it had been started by the umbrella, but as far as I can tell this will limit feedback in the umbrella job; you won't know if two failed because two failed, or because three (as a part of two) failed. If I'm wrong about this assumption please let me know.

I had thought about setting the parameter as an environment variable but I believe that's node-specific and I can't guarantee both jobs will run on the same node so that seemed to not be the solution.

Umbrella is a pipeline job written in groovy, the other three may be pipeline or freestyle jobs, if that matters.

I would appreciate detailed answers where possible, I'm still new to Groovy, Jenkins, and coding in general.

like image 220
Alex Avatar asked Jan 06 '17 19:01

Alex


1 Answers

It should be as simple as that:

stage ('Build 3') {
        res = build job: 'job_three', parameters: [[$class: 'StringParameterValue', name: 'filename', value: "$filename"]]
        echo "$res.buildVariables.filename"
}

Assuming that in job_three you do

env.filename = "col new file name"
like image 115
hakamairi Avatar answered Nov 14 '22 11:11

hakamairi