Help us understand the problem. What is going on with this article?

Jenkins APIでジョブ情報を取得する

More than 5 years have passed since last update.

ジョブ一覧の取得(JSON)

リクエスト
curl http://<jenkins host>/api/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を指定することで、更に情報を増やすことが可能(数字が大きいほど情報量が増える)

リクエスト
curl http://<jenkins host>/api/json?depth=1
レスポンス(抜粋)
{
  "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でフィルタをかけることが可能

リクエスト
curl http://<jenkins host>/api/json?depth=2&tree=jobs[displayName,buildable,lastCompletedBuild[number,timestamp,result,url,duration]]
レスポンス
{
  "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時刻&ミリ秒)

ジョブ単体の情報取得

api/jsonはジョブ全てだが、個別のジョブ情報は「job/<job_name>/api/json」で取得可能

ジョブ=test1のリクエスト
curl http://<jenkins host>/job/test1/api/json
レスポンス
{
  "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":
    {
      "number": 1,
      "url": "http://<jenkins host>/job/test1/1/"
    },
  "healthReport":[
    {
      "description": "ビルドの安定性: 最近のビルドは失敗してません。",
      "iconClassName": "icon-health-80plus",
      "iconUrl": "health-80plus.png",
      "score": 100
    }
  ],
  "inQueue": false,
  "keepDependencies": false,
  "lastBuild":
    {
      "number": 2,
      "url": "http://<jenkins host>/job/test1/2/"
    },
  "lastCompletedBuild":
    {
      "number": 2,
      "url": "http://<jenkins host>/job/test1/2/"
    },
  "lastFailedBuild": null,
  "lastStableBuild":
    {
      "number": 2,
      "url": "http://<jenkins host>/job/test1/2/"
    },
  "lastSuccessfulBuild":
    {
      "number": 2,
      "url": "http://<jenkins host>/job/test1/2/"
    },
  "lastUnstableBuild": null,
  "lastUnsuccessfulBuild": null,
  "nextBuildNumber": 3,
  "property": [ ],
  "queueItem": null,
  "concurrentBuild": false,
  "downstreamProjects": [ ],
  "scm": { },
  "upstreamProjects": [ ]
}

複数のジョブ情報を取得したい場合は、複数回リクエストしないといけないので、api/jsonでdepth & treeで絞ったほうが良い感じ

jrits
信頼と魅力のある先進のITをもとに、お客様のワークスタイル・イノベーションの実現を目指します。
http://www.jrits.co.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away