且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Jenkins groovy构建步骤触发groovy脚本中的另一项工作,删除原始作业的参数

更新时间:2023-12-05 14:26:22

在尝试了很多选项后,我决定加载默认参数对于我要触发的工作,并将它们添加到我正在准备的参数数组中,如下面的示例中所示。



我使用这里来获得我的工作的初始默认配置。



我仍然需要添加一些逻辑来选择参数和空值,但我对当前的结果感到满意。



我希望这有助于。


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.