简体   繁体   English

我如何从 groovy 脚本调用另一个 jenkins 构建作业

[英]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 .我想通过 groovy 调用另一个 Jenkins 构建作业,我该怎么做。 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:如果您使用 Pipeline Plugin 并且想要构建参数化作业,您可以执行以下操作:

Supose you have a "sonar-review" boolean parameter:假设您有一个“sonar-review”布尔参数:

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 https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Trigger+Plugin

You're talking about 's build step, right?你说的是build步骤,对吧?

This takes the job name as parameter, not a URL.这将作业名称作为参数,而不是 URL。 So to trigger a job called test , use因此,要触发名为test的作业,请使用

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如果你真的只想通过 URL 解雇一份工作,你可以通过发布到

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

seeparameterized-builds for details.有关详细信息,请参阅参数化构建

As far as through groovy there are several ways to do this.就通过 groovy 而言,有几种方法可以做到这一点。 Since you asked specifically with groovy see the Groovy plugin which provides an example on how to trigger another build.由于您专门询问了 groovy,请参阅Groovy 插件,它提供了有关如何触发另一个构建的示例。

You can also use the jobs-dsl-plugin as well.您也可以使用jobs-dsl-plugin 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.因此,我建议使用multibranch 管道插件,它允许您将管道作为Jenkinsfile 中的代码编写,并且构建将自动触发存储库中的任何分支。 Just simply create a multibranch pipeline job and set your SCM to your repository.只需简单地创建一个多分支管道作业并将您的 SCM 设置为您的存储库。 Then any branch with a Jenkinsfile in the root of the project will automatically build.然后任何在项目根目录中带有 Jenkinsfile 的分支都会自动构建。

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.例如,下面我正在触发一个名为jobName的作业,它将在启动jobName之前等待 10 秒,即使触发的作业失败,我的上游作业也不会失败,并且我的上游作业在标记自己之前不会等待触发的作业完成完全的。 Again, see the snippet generator for details.同样,请参阅代码段生成器以获取详细信息。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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