使用 Hudson/Jenkins API 确定给定作业当前是否正在运行

发布于 2024-12-02 19:42:30 字数 259 浏览 0 评论 0原文

是否有 API 可以确定给定作业当前是否正在运行?

理想情况下,我还希望能够确定其估计完成百分比并获取 SVN 修订号和提交评论的详细信息!

编辑:

我找到了答案。 http://host/job/project/lastBuild/api/ 几乎拥有我需要的所有内容!如果您开始手动构建,它不会告诉您 SCM 变更集,但这是有道理的。不过它仍然会告诉您最新的 SCM 版本,所以这很好。总而言之,对于我现在的目的来说已经足够了。

Is there an API to determine whether a given job is currently running or not?

Ideally, I'd also like to be able to determine its estimated % complete and get the details of the SVN revision number and commit comment too!

EDIT:

I found the answer. http://host/job/project/lastBuild/api/ has almost all of what I need in it somewhere! If you kick off a manual build, it won't tell you the SCM changesets, but that makes sense. It does still tell you the latest SCM revision though, so that's good. All in all, good enough for my purposes right now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

玻璃人 2024-12-09 19:42:30

正如 gareth_bowles 和 Sagar 所说,使用 Jenkins API 是了解的方法。
如果将深度设置为 1,您将看到您要查找的内容:

http://host/job/project/lastBuild/api/xml?depth=1

您将看到有一个 标记来判断该构建是否正在运行

...
<build>
  <action>
    <cause>
        <shortDescription>Started by user Zageyiff</shortDescription>
        <userId>Zageyiff</userId>
        <userName>Zageyiff</userName>
    </cause>
  </action>
  <building>true</building>
  <duration>0</duration>
  <estimatedDuration>-1</estimatedDuration>
  <fullDisplayName>Project #12</fullDisplayName>
  <id>2012-08-24_08-58-45</id>
  <keepLog>false</keepLog>
  <number>12</number>
  <timestamp>123456789</timestamp>
  <url>
        http://host/job/project/12
  </url>
  <builtOn>master</builtOn>
  <changeSet/>
  <mavenVersionUsed>3.0.3</mavenVersionUsed>
</build>
...

As gareth_bowles and Sagar said, using the Jenkins API is the way to know.
If you put the depth to 1, you will see what you're looking for:

http://host/job/project/lastBuild/api/xml?depth=1

You will see there's a <building> tag to tell if that build is running

...
<build>
  <action>
    <cause>
        <shortDescription>Started by user Zageyiff</shortDescription>
        <userId>Zageyiff</userId>
        <userName>Zageyiff</userName>
    </cause>
  </action>
  <building>true</building>
  <duration>0</duration>
  <estimatedDuration>-1</estimatedDuration>
  <fullDisplayName>Project #12</fullDisplayName>
  <id>2012-08-24_08-58-45</id>
  <keepLog>false</keepLog>
  <number>12</number>
  <timestamp>123456789</timestamp>
  <url>
        http://host/job/project/12
  </url>
  <builtOn>master</builtOn>
  <changeSet/>
  <mavenVersionUsed>3.0.3</mavenVersionUsed>
</build>
...
此刻的回忆 2024-12-09 19:42:30

我正在使用 Groovy 插件,并作为系统运行以下代码片段:

import hudson.model.*
def version = build.buildVariableResolver.resolve("VERSION")
println "VERSION=$version"
def nextJobName = 'MY_NEXT_JOB'
def nextJob = Hudson.instance.getItem(nextJobName)
def running = nextJob.lastBuild.building
if (running) {
   println "${nextJobName} is already running. Not launching"
} else {
   println "${nextJobName} is not running. Launching..."
   def params = [
      new StringParameterValue('VERSION', version)
   ]
   nextJob.scheduleBuild2(0, new Cause.UpstreamCause(build), new ParametersAction(params))
}

它的工作方式就像一个魅力。

I'm using the Groovy plug-in, and run the following snippet as system:

import hudson.model.*
def version = build.buildVariableResolver.resolve("VERSION")
println "VERSION=$version"
def nextJobName = 'MY_NEXT_JOB'
def nextJob = Hudson.instance.getItem(nextJobName)
def running = nextJob.lastBuild.building
if (running) {
   println "${nextJobName} is already running. Not launching"
} else {
   println "${nextJobName} is not running. Launching..."
   def params = [
      new StringParameterValue('VERSION', version)
   ]
   nextJob.scheduleBuild2(0, new Cause.UpstreamCause(build), new ParametersAction(params))
}

It works like a charm.

泡沫很甜 2024-12-09 19:42:30

如果您转到职位页面,并将“api”添加到 URL 末尾,您将获得有关使用 API 的信息。

http://yourjenkins/job/job_name/api

有关使用 Jenkins API 的更多信息:

https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API

If you go to your job's page, and add "api" to the end of the URL, you'll get information on using the API.

http://yourjenkins/job/job_name/api

More information on using the Jenkins API:

https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API
一向肩并 2024-12-09 19:42:30

如果您习惯于深入研究 Jenkins Java API,则可以编写系统 Groovy 脚本来获取此数据。 Job 类 是起点。

If you're comfortable with digging through the Jenkins Java API, you could write a system Groovy script to get this data. The Job class is the place to start.

ゃ人海孤独症 2024-12-09 19:42:30

如构建的 /api 页面(“访问渐进式控制台输出”一章)所述,您可以通过调用 使用 GET 请求轮询控制台输出。 /lastBuild/logText/progressiveText。引用 API 文档:

如果响应还包含 X-More-Data: true 标头,则服务器指示构建正在进行中

,就这样。您可以通过简单地在浏览器中调用相应的 URL,然后使用浏览器的开发人员工具(通常通过按 F12 访问)检查响应标头来测试此行为。在Firefox中,相应的选项卡称为“网络分析”(假设我的翻译是正确的,我的浏览器没有设置为英语)。在 Chrome 中,导航至“网络”选项卡。

这个答案基于 Jenkins 版本 2.176.3。

As stated on the /api page of your build (chapter "Accessing Progressive Console Output"), you can poll the console output with a GET request by calling <url-to-job>/lastBuild/logText/progressiveText. To quote the API doc:

If the response also contains the X-More-Data: true header, the server is indicating that the build is in progress

And there you go. You can test this behaviour by simply calling the respective URL in your browser and then inspecting the response headers with your browser's developer tools (usually accessed by pressing F12). In Firefox, the respective tab is called "network analysis" (assuming my translation is correct, my browser is not set to English). In Chrome, navigate to the "Network" tab.

This answer is based on Jenkins version 2.176.3.

洋洋洒洒 2024-12-09 19:42:30

还可以查看颜色属性。我知道这不是想要的方式。但也许有人可以利用它。
通过“/job/api/xml”获取概述 xml,然后检查“anim”的颜色属性。

It is also possible to look at the color attribute. I know it is not the wanted way. But maybe someone can make use of it.
get the overview xml via "/job/api/xml" and then check the color attribute for "anim".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文