Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jenkins

groovy

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?

like image 726
Eldad Assis Avatar asked Jun 24 '13 15:06

Eldad Assis


People also ask

How do you trigger Jenkins job from another Jenkins job with parameters?

You can follow the below steps to trigger a Jenkins pipeline in another Jenkins pipeline. Select a job that triggers a remote one and then go to Job Configuration > Build section > Add Build Step > Trigger builds on remote/local projects option.

How do I pass a parameter from one Jenkins job to another?

You can use Parameterized Trigger Plugin which will let you pass parameters from one task to another. You need also add this parameter you passed from upstream in downstream.

How can I invoke another Jenkins build job from Groovy script?

Just simply create a multibranch pipeline job and set your SCM to your repository. Then any branch with a Jenkinsfile in the root of the project will automatically build.

How do I integrate two jobs in Jenkins?

Configuring custom relationships between Jobs Now Let's go to 'Job 01' and go to post-build section. In build other projects, add 'Job 02' and 'Job 03'. Then add join trigger from post build steps and enter 'Job 04'. This will ensure that 'Job 04' is executed only when both 'Job 02' and 'Job 03' are completed.


1 Answers

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.

like image 55
Eldad Assis Avatar answered Sep 20 '22 06:09

Eldad Assis