LoginSignup
2
2

More than 5 years have passed since last update.

Jenkinsのキューに入っているビルドのキャンセル結果

Last updated at Posted at 2014-11-03

以下を参考にしました。

"How to stop a build in Jenkins via the REST api ?"
http://stackoverflow.com/questions/21021905/how-to-stop-a-build-in-jenkins-via-the-rest-api

If the build has not started, you have the queueItem, then POST on:
http://<Jenkins_URL>/queue/cancelItem?id=<queueItem>

以下は curl で試してみた結果です。

$ curl -X POST -v 'http://ayweak:password@localhost:8080/queue/cancelItem?id=1'
* About to connect() to localhost port 8080
*   Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 8080
* Server auth using Basic with user 'ayweak'
> POST /queue/cancelItem?id=1 HTTP/1.1
> Authorization: Basic YXl3ZWFrOnBhc3N3b3Jk
> User-Agent: curl/7.15.5 (i386-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5
> Host: localhost:8080
> Accept: */*
> 
< HTTP/1.1 302 Found
< Location: http://localhost:8080/queue/
< Content-Length: 0
< Server: Jetty(winstone-2.8)
* Connection #0 to host localhost left intact
* Closing connection #0

指定した id のアイテムの有無に関係なく 302 が返ってきます。

キャンセルが間に合ったかどうかは続けて lastBuild を確認すれば良いのでしょうか。


追記です。

以下のようにジョブの REST API のページに説明がありました。

http://<Jeknins_URL>/job/<job_name>/api/

Perform a build

To programmatically schedule a new build, post to this URL. If the build has parameters, post to this URL and provide the parameters as form data. Either way, the successful queueing will result in 201 status code with Location HTTP header pointing the URL of the item in the queue. By polling the api/xml sub-URL of the queue item, you can track the status of the queued task. Generally, the task will go through some state transitions, then eventually it becomes either cancelled (look for the "cancelled" boolean property), or gets executed (look for the "executable" property that typically points to the AbstractBuild object.)

キューアイテムの cancelledexecutable の有無や値を確認することで、キャンセルの結果を判断できるようです。

以下のようにしてキューアイテムのデータを参照できました。

http://<Jenkins_URL>/queue/item/<id>/api/json?pretty=true

以下はキューに追加した状態です。cancelledexecutable はありません。

{
  "actions" : [
    {
      "parameters" : [
        {
          "name" : "param1",
          "value" : "7"
        }
      ]
    },
    {
      "causes" : [
        {
          "shortDescription" : "ユーザーJenkins Ayweakが実行",
          "userId" : "ayweak",
          "userName" : "Jenkins Ayweak"
        }
      ]
    }
  ],
  "blocked" : false,
  "buildable" : false,
  "id" : 7,
  "inQueueSince" : 1415119577407,
  "params" : "\nparam1=7",
  "stuck" : false,
  "task" : {
    "name" : "Test1",
    "url" : "http://<Jenkins_URL>/job/Test1/",
    "color" : "blue"
  },
  "url" : "queue/item/7/",
  "why" : "ビルド #27 は既に実行中です。 (予定時間:59 秒)",
  "timestamp" : 1415119582406
}

以下はキャンセルした場合です。cancelledtrue に、executablenull になりました。

{
  ... 省略 ...
  "cancelled" : true,
  "executable" : null
}

以下はビルドが開始された場合です。cancelledfalse に、executable はビルドのデータが入りました。

{
  ... 省略 ...
  "cancelled" : false,
  "executable" : {
    "number" : 21,
    "url" : "http://<Jenkins_URL>/job/Test1/21/"
  }
}
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2