简体   繁体   中英

Jenkins groovy build step trigger another job in groovy script removes original job's parameters

I'm using a groovy script to trigger other jobs, which is based on the example from the Groovy plugin page.

I get a list of jobs as a parameter, validate they exist and trigger them with a few parameters. See main trigger code:

    // Prepare parameters array
    def params = 
    [
        new StringParameterValue('PARAM1', 'val1'),
        new StringParameterValue('PARAM2', 'val2'),
    ]
    def future = job.scheduleBuild2(0, new Cause.UpstreamCause(build), new ParametersAction(params))
    println "Waiting for the completion of " + jobLink
    anotherBuild = future.get()

My triggered jobs run perfectly, but with one major problem. Their original parameters are lost and are replaced by the new ones PARAM1 and PARAM2 .

How to I trigger a job and add to its default parameters and not replace them?

I tried hard to find a solution for it and didn't find one...

EDIT: I was thinking of not setting parameters (and allowing job to use its defaults), but setting environment variables for the job's execution. Does anyone have an idea or example on how to do this?

After trying many options, I decided to load the default parameters for the job I'm about to trigger and add them to the parameter array I'm preparing as in the example below.

I used the example from here to get my job's initial default configuration.

I still has to add some logic to choices parameters and null values, but I'm happy with the current result.

I hope this helps.

These days I would use something like Build Flow plugin for this. That orchestration DSL/API reduces the code in question to:

 build('job', PARAM1: 'val1'
              PARAM2: 'val2')

You can pass the params as hash map, the ones that are missing will fall back to the default values.

One issue with this plugin: it may be abandoned soon. A brand new alternative(not released yet): https://github.com/jenkinsci/workflow-plugin .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM