How to view Jenkins console output during a build from the terminal after invoking build using curl from the terminal?

huangapple go评论83阅读模式
英文:

How to view Jenkins console output during a build from the terminal after invoking build using curl from the terminal?

问题

我已经构建了一个Jenkins任务来运行自动化的ZAP-Proxy扫描。

我使用curl -X POST -u YOUR_USER:YOUR_USER_PASSWORD http://YOUR_JENKINS_URL/job/YOUR_JOB来从终端构建该任务。是否有一种方法可以在任务正在构建时在终端中显示控制台输出?

英文:

I have built a Jenkins job to run automated ZAP-Proxy scans.

I used curl -X POST -u YOUR_USER:YOUR_USER_PASSWORD http://YOUR_JENKINS_URL/job/YOUR_JOB to build the job from the terminal. Is there a way to display the console output in the terminal while the job is building?

答案1

得分: 2

可以的,但由于两个原因,这可能会稍微复杂一些:

  1. 当你通过 curl 触发一个新的构建时,构建不会立即启动。构建会进入构建队列,只有在Jenkins找到合适的执行者后才会开始执行。在此之前,根本没有构建URL。

  2. 从技术上讲,(持续的) 控制台输出以多个片段交付,必须通过HTTP单独检索。

因此,一旦触发了构建,你需要在它离开构建队列之后找到它的URL。这可以在Groovy中很好地完成——作为一个更简单的启发式方法,你可以等待一定的时间,然后使用 lastBuild 引用。

要获取控制台日志片段,你将使用 <buildUrl>/logText/progressiveText 终点。创建一个循环来获取该URL,并检查HTTP标头中的 X_More_DataX_Text_Size 信息,以了解是否可用(以及什么)控制台输出。你可以在 bash 中使用 curl 在循环中执行这个操作;我在网络上找到了这个Groovy示例

最后,最优雅的解决方案可能是:

  • 通过提交一个Groovy脚本来触发一个新的构建,该脚本将触发构建,然后等待构建离开构建队列,并返回构建URL。
  • 然后使用另一个脚本来轮询/获取/显示该构建的控制台输出。

如果你使用CLI界面来提交脚本,那么你可以在单个脚本中执行所有这些步骤。如果你使用REST API,那么第二部分("持续输出")可能不会工作,因为REST API端会进行输出缓冲。

英文:

It is possible, but it's a little more complicated for two reasons:

  1. When you trigger a new build via curl, then the build will not start immediately. The build will enter the build queue, and it will only start executing once Jenkins found a suitable executor. Before that time, there is no build URL at all.
  2. Technically, the (continuous) console output is delivered in multiple fragments that must be retrieved individually via HTTP.

So, once you triggered the build, you need to find its URL after if left the build queue. It can be done nicely in Groovy -- as a simpler heuristic, you could just wait for certain time and then use the lastBuild reference.

For fetching the console log fragments, you'll use the &lt;buildUrl&gt;/logText/progressiveText end point. Create a loop fetching that URL and checking the X_More_Data and X_Text_Size HTTP headers for information on whether (and what) console output is available. You can do that in bash with curl in a loop; I found this Groovy example on the Web.

In the end, the most elegant solution is probably to

  • trigger a new build by submitting a Groovy script that will trigger the build and then waits for the build to leave the build queue, returning the build URL.
  • Then use another script that will poll/fetch/display that build's console output.

If you use the CLI interface for submitting the script, then you can do all those steps in a single script. If you use the REST API, then second part ("continuous output") probably won't work due to output buffering on REST API side.

huangapple
  • 本文由 发表于 2020年1月3日 14:37:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574203.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定