Jenkins - passing variables between jobs?

前端 未结 10 1624
你的背包
你的背包 2020-11-28 06:53

I have two jobs in jenkins, both of which need the same parameter.

How can I run the first job with a parameter so that when it triggers the second job, the same pa

相关标签:
10条回答
  • 2020-11-28 07:11

    I faced the same issue when I had to pass a pom version to a downstream Rundeck job.

    What I did, was using parameters injection via a properties file as such:

    1) Creating properties in properties file via shell :

    Build actions:

    • Execute a shell script
    • Inject environment variables

    E.g : properties definition

    2) Passing defined properties to the downstream job : Post Build Actions :

    • Trigger parameterized build on other project
    • Add parameters : Current build parameters
    • Add parameters : predefined parameters

    E.g : properties sending

    3) It was then possible to use $POM_VERSION as such in the downstream Rundeck job.

    /!\ Jenkins Version : 1.636

    /!\ For some reason when creating the triggered build, it was necessary to add the option 'Current build parameters' to pass the properties.

    0 讨论(0)
  • 2020-11-28 07:12

    The accepted answer here does not work for my use case. I needed to be able to dynamically create parameters in one job and pass them into another. As Mark McKenna mentions there is seemingly no way to export a variable from a shell build step to the post build actions.

    I achieved a workaround using the Parameterized Trigger Plugin by writing the values to a file and using that file as the parameters to import via 'Add post-build action' -> 'Trigger parameterized build...' then selecting 'Add Parameters' -> 'Parameters from properties file'.

    0 讨论(0)
  • 2020-11-28 07:14

    Just add my answer in addition to Nigel Kirby's as I can't comment yet:

    In order to pass a dynamically created parameter, you can also export the variable in 'Execute Shell' tile and then pass it through 'Trigger parameterized build on other projects' => 'Predefined parameters" => give 'YOUR_VAR=$YOUR_VAR'. My team use this feature to pass npm package version from build job to deployment jobs

    UPDATE: above only works for Jenkins injected parameters, parameter created from shell still need to use same method. eg. echo YOUR_VAR=${YOUR_VAR} > variable.properties and pass that file downstream

    0 讨论(0)
  • 2020-11-28 07:28

    (for fellow googlers)

    If you are building a serious pipeline with the Build Flow Plugin, you can pass parameters between jobs with the DSL like this :

    Supposing an available string parameter "CVS_TAG", in order to pass it to other jobs :

    build("pipeline_begin", CVS_TAG: params['CVS_TAG'])
    parallel (
       // will be scheduled in parallel.
       { build("pipeline_static_analysis", CVS_TAG: params['CVS_TAG']) },
       { build("pipeline_nonreg", CVS_TAG: params['CVS_TAG']) }
    )
    // will be triggered after previous jobs complete
    build("pipeline_end", CVS_TAG: params['CVS_TAG'])
    

    Hint for displaying available variables / params :

    // output values
    out.println '------------------------------------'
    out.println 'Triggered Parameters Map:'
    out.println params
    out.println '------------------------------------'
    out.println 'Build Object Properties:'
    build.properties.each { out.println "$it.key -> $it.value" }
    out.println '------------------------------------'
    
    0 讨论(0)
  • 2020-11-28 07:29

    I think the answer above needs some update:

    I was trying to create a dynamic directory to store my upstream build artifacts so I wanted to pass my upstream job build number to downstream job I tried the above steps but couldn't make it work. Here is how it worked:

    1. I copied the artifacts from my current job using copy artifacts plugin.
    2. In post build action of upstream job I added the variable like "SOURCE_BUILD_NUMBER=${BUILD_NUMBER}" and configured it to trigger the downstream job.
    3. Everything worked except that my downstream job was not able to get $SOURCE_BUILD_NUMBER to create the directory.
    4. So I found out that to use this variable I have to define the same variable in down stream job as a parameter variable like in this picture below:

    This is because the new version of jenkins require's you to define the variable in the downstream job as well. I hope it's helpful.

    0 讨论(0)
  • 2020-11-28 07:29

    Reading through the answers, I don't see another option that I like so will offer it as well. I love the parameterization of jobs, but it doesn't always scale well. If you have jobs which are not directly downstream of the first job but farther down the pipeline, you don't really want to parameterize every job in the pipeline so as to be able to pass the parameters all the way through. Or if you have a large number of parameters used by a variety of other jobs (especially those not necessarily tied to one parent or master job), again parameterization doesn't work.

    In these cases, I favor outputting the values to a properties file and then injecting that in whatever job I need using the EnvInject plugin. This can be done dynamically, which is another way to solve the issue from another answer above where parameterized jobs were still used. This solution scales very well in many scenarios.

    0 讨论(0)
提交回复
热议问题