1. tamikura@github

    Posted

    tamikura@github
Changes in title
+Jenkins APIでジョブ情報を取得する
Changes in tags
Changes in body
Source | HTML | Preview
@@ -0,0 +1,153 @@
+## ジョブ一覧の取得(JSON)
+
+```shell-sessoin:リクエスト
+curl http://<jenkins host>/api/json
+```
+
+```json:レスポンス
+{
+ "assignedLabels": [
+ { }
+ ],
+ "mode": "NORMAL",
+ "nodeDescription": "ノード",
+ "nodeName": "",
+ "numExecutors": 2,
+ "description": null,
+ "jobs":[
+ {
+ "name": "test1",
+ "url": "http://<jenkins host>/job/test1/",
+ "color": "blue"
+ },
+ {
+ "name": "test2",
+ "url": "http://<jenkins host>/job/test2/",
+ "color": "red"
+ }
+ ],
+ "overallLoad": { },
+ "primaryView":
+ {
+ "name": "All",
+ "url": "http://<jenkins host>/"
+ },
+ "quietingDown": false,
+ "slaveAgentPort": 0,
+ "unlabeledLoad": { },
+ "useCrumbs": false,
+ "useSecurity": false,
+ "views":[
+ {
+ "name": "All",
+ "url": "http://<jenkins host>/"
+ }
+ ]
+}
+```
+
+## depthで更に情報を取得
+
+/api/jsonだけだとジョブの情報が少なすぎる(開始時刻・実行時間・ジョブの成否などは分からない)
+その場合は、クエリパラメータのdepthを指定することで、更に情報を増やすことが可能(数字が大きいほど情報量が増える)
+
+```shell-sessoin:リクエスト
+curl http://<jenkins host>/api/json?depth=1
+```
+
+```json:レスポンス(抜粋)
+{
+ "assignedLabels": [
+ {
+ "actions": [ ],
+ "busyExecutors": 0,
+ "clouds": [ ],
+ "description": null,
+ "idleExecutors": 2,
+ "loadStatistics": { },
+ "name": "master",
+ "nodes": [
+ {
+ "nodeName": ""
+ }
+ ],
+ "offline": false,
+ "tiedJobs": [ ],
+ "totalExecutors": 2,
+ "propertiesList": [ ]
+ }
+ ],
+ "mode": "NORMAL",
+ "nodeDescription": "ノード",
+ "nodeName": "",
+ "numExecutors": 2,
+ "description": null,
+ "jobs":[
+ {
+ "actions": [ ],
+ "description": "",
+ "displayName": "test1",
+ "displayNameOrNull": null,
+ "name": "test1",
+ "url": "http://<jenkins host>/job/test1/",
+ "buildable": true,
+ "builds": [
+ {
+ "number": 2,
+ "url": "http://<jenkins host>/job/test1/2/"
+ },
+ {
+ "number": 1,
+ "url": "http://<jenkins host>/job/test1/1/"
+ }
+ ],
+ "color": "blue",
+ "firstBuild":
+ }
+ ]
+}
+
+```
+
+## treeで情報を制限
+
+depthを深く指定すると、レスポンスサイズが爆発的に増えるので、リクエストが遅くなる。必要な情報だけ欲しい場合は、クエリパラメータのtreeでフィルタをかけることが可能
+
+```shell-session:リクエスト
+curl http://<jenkins host>/api/json?depth=2&tree=jobs[displayName,buildable,lastCompletedBuild[number,timestamp,result,url,duration]]
+```
+
+```json:レスポンス
+{
+ "jobs": [
+ {
+ "displayName": "test1",
+ "buildable": true,
+ "lastCompletedBuild":
+ {
+ "duration": 60047,
+ "number": 2,
+ "result": "SUCCESS",
+ "timestamp": 1426819241088,
+ "url": "http://<jenkins host>/job/test1/2/"
+ }
+ },
+ {
+ "displayName": "test2",
+ "buildable": true,
+ "lastCompletedBuild":
+ {
+ "duration": 53,
+ "number": 1,
+ "result": "FAILURE",
+ "timestamp": 1426822256482,
+ "url": "http://<jenkins host>/job/test2/1/"
+ }
+ }
+ ]
+}
+```
+buildableがジョブの有効・無効
+durationが実行時間(ミリ秒)
+timestampが開始時刻(UNIX時刻&ミリ秒)
+