Command Line output

Hi everyone,

I am new to Jenkins Rest API calls. I am trying to create a script which will kick Jenkins JOB and wait for the completion of JOB.

I’m trying to use curl to get the output about the job ID assigned in Jenkins. Here is my curl command:

curl -v -D - -X POST “http://username:Token@192.168.146.133:8080/job/deneme/build?token=111222333” --user username:password

I want to extract the job ID assigned from the build history, similar to how it’s displayed in the Jenkins GUI. Specifically, after triggering a build, I need to find and display the assigned job ID.

I get the output after using curl command. At the output Location similar to my jobid but when im trying to connect http below it gives me error.
job id almost similar as you can see

http://192.168.146.133:8080/job/deneme/39/.

I have managed to extract the job ID with a bash script, but is there any other option instead of using bash? Is there any way to see the output job ID directly?

But this bash script just change the query id to job id so if query and jobid is not same it is not working.

I need to get the Build Number for me to query the status.

Thank you

Hello and welcome to this community, @Ozanb. :wave:

You could maybe have a look at our script there: quickstart-tutorials/.github/workflows/test-jenkins.yml at 4d37351a84648b026db3fb1270b86990bf42f5d5 · jenkins-docs/quickstart-tutorials · GitHub .

It would be better to do all that in a bash script instead of a GitHub Action, but I think it shouldn’t be too hard to extract the Bash parts.

When you trigger a build via rest api you get in the response header a new location that points to the queue, e.g.

$ curl -X POST http://localhost:9090/job/testpipeline/build -u user:token
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0HTTP/1.1 201 Created
Date: Tue, 04 Jun 2024 09:01:04 GMT
X-Content-Type-Options: nosniff
Location: http://localhost:9090/queue/item/90/
Content-Length: 0
Server: Jetty(10.0.20)

You can now take the location and ask it’s rest API with
http://localhost:9090/queue/item/90/api/json
as long as the build is in the queue you will get "blocked": true in the json answer. Once the build has started you get an executable in the json with number and url

{
  "_class": "hudson.model.Queue$LeftItem",
  "actions": [
    {
      "_class": "hudson.model.CauseAction",
      "causes": [
        {
...
        }
      ]
    }
  ],
  "blocked": false,
  "buildable": false,
  "id": 90,
  "inQueueSince": 1717491664856,
  "params": "",
  "stuck": false,
  "task": {
    "_class": "org.jenkinsci.plugins.workflow.job.WorkflowJob",
    "name": "testpipeline",
    "url": "http://localhost:9090/job/testpipeline/",
    "color": "blue"
  },
  "url": "queue/item/90/",
  "why": null,
  "cancelled": false,
  "executable": {
    "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
    "number": 66,
    "url": "http://localhost:9090/job/testpipeline/66/"
  }
}

You will need to poll the rest api until the build has started to get the build number. A few minutes after the build started the information about the build will be deleted from the queue and querying the api will return a 404

3 Likes

Hello @poddingue ,

Thanks for quick response i check there and i find a solution.

I did with bash scripting but right now i change it to python it work well thanks a lot !

1 Like

hello @mawinter69,

It gives me idea to for bash thanks a lot for your quick response

1 Like

Thanks a lot for your feedback, @Ozanb. :+1: