Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make HTTP call to jenkins build with parameters then fetch output of running job triggered by that call?

Tags:

jenkins

I'm new to jenkins, what's the best way to do this? I make a call to jenkins URL for triggering remote builds with a token:

http://host/job/job-name/buildWithParameters?token=value&param1=val1

from testing with a REST client, I see this returns HTTP 201 with a location header for redirecting to another resource:

http://host/queue/item/34355/

but viewing that resource URL returns 404 not found

is there a better way than screen scraping (or is there an API call for?) for getting the last/latest job # (running or completed) for the given job that I triggered for? Ideally though, I'd rather have the HTTP response of the call tell me what's the matching job run # that I just kicked off, since in concurrent triggers to same job (with different parameters), the last/latest job run may not be the one you kicked off.

I need to get the job run number to view the console output to fetch some info. e.g. the job executes some task in the background and outputs the process ID that I need to fetch to later terminate, etc.

like image 238
David Avatar asked Oct 27 '25 16:10

David


1 Answers

Triggering a build with the REST API like this just enqueues a build, waiting to be executed. Therefore the API call returns immediately, without waiting for the build to start, or complete (it's possible that neither may happen).

But the response to your request will contain a Location HTTP header, pointing to the newly created queue item. For example:

HTTP/1.1 201 Created
Location: http://localhost:8080/queue/item/16/

Checking the API URL of this resource (i.e. append either api/json or api/xml to the URL) will return information about the queued item.

If the build is still in the queue, you'll see the why reason (e.g. waiting for an available agent), but if the build has been started, you'll see an executable.url field pointing to the build. For example, the API JSON for a build that started:

{
  …
  "url": "queue/item/16/",
  "executable": {
    "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
    "number": 16,
    "url": "http://localhost:8080/job/test/4/"
  }
  …
}

Via the logText endpoint for that build URL, you can fetch the build logs, e.g.:
http://localhost:8080/job/test/4/logText/progressiveText?start=0

This feature is documented in more detail on the API page for the build (i.e. /job/test/4/api).

like image 119
Christopher Orr Avatar answered Oct 30 '25 12:10

Christopher Orr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!