检索远程触发的詹金斯作业的 ID [英] Retrieve id of remotely triggered jenkins job

查看:30
本文介绍了检索远程触发的詹金斯作业的 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过 http POST 请求从 jenkins 外部触发参数化 Jenkins:

我在作业配置中启用了可以从外部触发作业的功能,我可以通过向 jenkins 发送包含如下内容的请求来真正触发它:

发布
http://myJenkins.com/myJob/buildWithParameters?token=MYTOKEN参数:SCREEN_SIZE:27

触发作业创建会返回一个成功的 201 CREATED http 响应.

我的问题是我不知道创建的构建作业的 ID.我想监控作业的状态.为了做到这一点,我需要知道 ID.否则,如果我只是采用该工作的最新版本,我可能会选择错误的工作.

是否有可靠的方法来获取创建的作业的 id?

解决方案

更新:

其他答案是在我的大约 8 个月后添加的.我当时不知道响应中的 location 标头.在某些情况下,这听起来确实是一个不错的选择.也就是说,根据答案中的警告和评论(尤其是围绕参数化构建),似乎这个答案在某些情况下仍然有一些实用性.

原答案:

不幸的是,他们并没有让这一切变得尽可能简单.即,例如,返回带有 id 之类信息的 JSON 响应.

但是,我认为一个可靠的解决方法是利用您用来触发构建的 URL 中的 cause 参数,并在该原因中添加 独特的文本,您可以稍后对其进行解析以确定您是从自动化中触发的.

为了进一步唯一化特定的作业,如果多个作业几乎同时运行,即使来自您的自动化,也包括某种类型的唯一 ID(它可能只是来自您的序列 IDRDBMS 或类似)在原因参数中.

然后,您可以使用 JSON API 获取有关您远程触发的 作业 的信息.同样,这有点间接,但可行:

点击表单的 URL:

http://:/job//lastBuild/api/json

您可以添加 ?pretty=true 以在浏览器中对其进行漂亮打印,以提高人类可读性.

这将为您提供上次构建的 JSON.它将在 actions 属性中包含 causes 属性,并且在那里(在另一个名为 shortDescription 的嵌套属性中)您会找到 原因您添加的参数,如果这是您触发的构建之一.

您可以解析特殊的静态文本和生成的 ID 以查看它们是否匹配.如果他们这样做了,那么您也可以从 JSON 中获取 Jenkins id(搜索嵌套的 buildNumber).>

如果该构建要么根本不是由您的自动化触发,要么是,但 ID 不匹配,您可以重复 N - 1 个构建的过程,直到找到您想要的正在寻找.

JSON 格式如下:

http://:/job///api/json

I am triggering a parameterized Jenkins from from outside of jenkins via a http POST request:

I have enabled in the job configuration that the job can be triggered from outside and i can really trigger it by sending jenkins a request with a content like this:

POST
http://myJenkins.com/myJob/buildWithParameters?token=MYTOKEN Parameter: SCREEN_SIZE: 27

Triggering the job creation returns a successfull 201 CREATED http response.

My problem is that i dont know the id of the build job that was created. I want to monitor the state of the job. In order to do that i need to know the id. Otherwise, if i just take the latest build of that job, i could take wrong job.

Is there a reliable way to get the id of the created job?

解决方案

Update:

The other answer was added ~8 months after mine. I was unaware of the location header in the response at the time. That does sound like a good option for some cases. That said, based on the caveats in the answer and the comments (especially around parameterized builds), it appears that this answer still has some utility for some cases.

Original answer:

Unfortunately, they don't make this as straightforward as it could be. i.e. by, say, returning a JSON response with information like an id.

However, I believe a solid, though certainly non-trivial, workaround to that would be to leverage the cause parameter in the URL you use to trigger the build, and within that cause, add unique text that you can later parse to determine that you triggered it from your automation.

To further uniqueify the specific job, in case multiple are running around the same time, even from your automation, also include a unique ID of some type (it could simply be a sequence ID from your RDBMS or similar) inside of the cause parameter.

Then, you can use the JSON API to get information about the job you're remotely triggering. Again, it's somewhat indirect, but doable:

Hit a URL of the form:

http://<server>:<port>/job/<jobname>/lastBuild/api/json

You can add ?pretty=true to pretty-print it within the browser for better human readability.

That will get you the JSON of the last build. It will contain the causes attribute within the actions attribute, and in there (in another nested attribute named shortDescription) you would find the cause parameter you added, if this was one of the builds you triggered.

You could parse out both the special static text, and your generated ID to see if they match. If they do, you can then get the Jenkins id out of the JSON as well (search for buildNumber, which is nested).

If that build is either not triggered by your automation at all, or was, but the ID doesn't match, you can repeat the process for the N - 1 build until you find what you're looking for.

That JSON would be of the form:

http://<server>:<port>/job/<jobname>/<buildNumber>/api/json

这篇关于检索远程触发的詹金斯作业的 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆