简体   繁体   中英

How can i invoke another jenkins build job from groovy script

I want to invoke another Jenkins build job through groovy , How I can do that . I tried with including like below but no luck

build 'job url' 

but got the error

ERROR: No parameterized job named

If you are using Pipeline Plugin and you want to build a parametrized job you can do:

Supose you have a "sonar-review" boolean parameter:

build job: 'cargo-pipeline-ci-declarative', parameters: [booleanParam(name: 'sonar-review', value: false)]

In case you don't required to build a job without parameters:

You can just do:

build "cargo-pipeline-ci-declarative"

Be sure you have installed:

https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Trigger+Plugin

You're talking about 's build step, right?

This takes the job name as parameter, not a URL. So to trigger a job called test , use

build 'test'

It seems searching for a parameterized job, maybe because you gave multiple arguments? Please be more precise with your example code and what the job name is (is it in another folder)?

If you truly just want to fire a job by the URL you can do so using by posting to

http://server/job/myjob/buildWithParameters?PARAMETER=Value 

seeparameterized-builds for details.

As far as through groovy there are several ways to do this. Since you asked specifically with groovy see the Groovy plugin which provides an example on how to trigger another build.

You can also use the jobs-dsl-plugin as well. But that is being phased out by the pipeline workflow.

As such I would recommend to use the multibranch pipeline plugin which allows you to write your pipeline as code in a Jenkinsfile and builds will automatically trigger for any branch in your repository. 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.

A benefit to using the multibranch pipeline plugin is that it provides the snippet generator which will generate the code for you. This is helpful in learning the syntax and how to write code yourself.

Finally, to address your actual issue. You don't provide a url but the name of the job you want to trigger.

You also need to consider the defaults. For example, below I'm triggering a job called jobName which will wait 10 seconds before starting jobName , my upstream job will not fail even if the triggered job does, and my upstream job will not wait for the triggered job to finish before marking itself complete. Again, see the snippet generator for details.

build job: 'jobName', propagate: false, quietPeriod: 10, wait: false

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