Retrieve id of remotely triggered jenkins job

后端 未结 5 1708
执念已碎
执念已碎 2020-12-05 04:32

I am triggering a parameterized Jenkins from from outside of jenkins via a http POST request:

I have enabled in the job configuration that the job can be triggered f

5条回答
  •  时光取名叫无心
    2020-12-05 05:14

    Since Jenkins 1.519, enqueuing a build responds with a URL in the Location, pointing you to an item in the build queue:

    $ nc localhost 8666
    POST /jenkins/job/morgRemote/buildWithParameters?jenkins_status=1&jenkins_sleep=20&token=morgRemote HTTP/1.1
    Host: localhost:8666
    
    HTTP/1.1 201 Created
    Location: http://localhost:8666/jenkins/queue/item/39/
    Content-Length: 0
    Server: Jetty(winstone-2.8)
    

    Now if you add api/json (or api/xml and so on) to the end of it (so in this example it would be http://localhost:8666/jenkins/queue/item/39/api/json) then you will get a document that will contain build id for the given job. For json the retrieved object has executable attribute, which in turn has number and url attributes. number is the build id for the given job (35 here) and url is the jenkins build page url.

    {
      "actions" : [
        {
          "parameters" : [
            {
              "name" : "jenkins_status",
              "value" : "1"
            },
            {
              "name" : "jenkins_sleep",
              "value" : "20"
            }
          ]
        },
        {
          "causes" : [
            {
              "shortDescription" : "Started by remote host 127.0.0.1",
              "addr" : "127.0.0.1",
              "note" : null
            }
          ]
        }
      ],
      "blocked" : false,
      "buildable" : false,
      "id" : 39,
      "inQueueSince" : 1423993879845,
      "params" : "\njenkins_status=1\njenkins_sleep=20",
      "stuck" : false,
      "task" : {
        "name" : "morgRemote",
        "url" : "http://localhost:8666/jenkins/job/morgRemote/",
        "color" : "red"
      },
      "url" : "queue/item/39/",
      "why" : null,
      "cancelled" : false,
      "executable" : {
        "number" : 35,
        "url" : "http://localhost:8666/jenkins/job/morgRemote/35/"
      }
    }
    

    be aware of 2 things:

    • inactive items in the build queue are garbage collected after few minutes, so you should retrieve build id ASAP
    • by default it takes few seconds between item is added to the queue until it gets build id. During this time executable and canceled attributes will be missing and why will be not null. You can change this behavior in "Advanced Project Options" of your job config by modifying "Quiet period" setting or in the jenkins global configuration.

    :

      ...
      "url" : "queue/item/39/",
      "why" : "In the quiet period. Expires in 2.4 sec",
      "timestamp" : 1423993879845
    }
    

提交回复
热议问题