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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With