cshake98
cshake98

Reputation: 31

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

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?

Upvotes: 2

Views: 2498

Answers (1)

Alex O
Alex O

Reputation: 8164

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 <buildUrl>/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.

Upvotes: 3

Related Questions