「Jenkins」Jenkins APIでジョブ情報取得やジョブ登録をやってみる

Jenkins API の使い方メモ。API の利用には API Token が必要なようでして、これは Jenkins の画面右上に見えているユーザの [下矢印クリック] - [設定] - [APIトークン] - [APIトークンの表示...] から確認可能です。

後述のコマンド例では以下の通りになってます。

  ・UserID:jenkins
  ・APIトークン:3ab6599ee0329470aa51234eebd1be9c

今回 JSON でデータを受け取る感じにしてますが、「/api/json の部分を 「/api/xml としてあげると XML 形式でデータを返してくれたります。

※ 各コマンド例のパイプでつないでいる jqJSON プロセッサーです(なかなか便利)。

https://stedolan.github.io/jq/

・ジョブ情報の取得

# curl --user jenkins:3ab6599ee0329470aa51234eebd1be9c "http://xxx.xxx.xxx.xxx:8080/job/Folder1/job/Job11/api/json" | jq
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   789  100   789    0     0   285k      0 --:--:-- --:--:-- --:--:--  385k
{
  "_class": "hudson.model.FreeStyleProject",
  "actions": [
    {},
    {},
    {
      "_class": "com.cloudbees.plugins.credentials.ViewCredentialsAction"
    }
  ],
  "description": "Job11",
  "displayName": "Job11",
  "displayNameOrNull": null,
  "fullDisplayName": "Folder1 ≫ Job11",
  "fullName": "Folder1/Job11",
  "name": "Job11",
  "url": "http://192.168.39.170:8080/job/Folder1/job/Job11/",
  "buildable": true,
  "builds": [],
  "color": "notbuilt",
  "firstBuild": null,
  "healthReport": [],
  "inQueue": false,
  "keepDependencies": false,
  "lastBuild": null,
  "lastCompletedBuild": null,
  "lastFailedBuild": null,
  "lastStableBuild": null,
  "lastSuccessfulBuild": null,
  "lastUnstableBuild": null,
  "lastUnsuccessfulBuild": null,
  "nextBuildNumber": 1,
  "property": [],
  "queueItem": null,
  "concurrentBuild": false,
  "downstreamProjects": [],
  "scm": {
    "_class": "hudson.scm.NullSCM"
  },
  "upstreamProjects": []
}


フォルダの場合も同じ感じで取得できる。

# curl --user jenkins:3ab6599ee0329470aa51234eebd1be9c "http://xxx.xxx.xxx.xxx:8080/job/Folder1/api/json" | jq
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   722  100   722    0     0   276k      0 --:--:-- --:--:-- --:--:--  352k
{
  "_class": "com.cloudbees.hudson.plugins.folder.Folder",
  "actions": [
    {},
    {
      "_class": "com.cloudbees.plugins.credentials.ViewCredentialsAction"
    }
  ],
  "description": "テスト用フォルダ1",
  "displayName": "Folder1",
  "displayNameOrNull": null,
  "fullDisplayName": "Folder1",
  "fullName": "Folder1",
  "name": "Folder1",
  "url": "http://192.168.39.170:8080/job/Folder1/",
  "healthReport": [],
  "jobs": [
    {
      "_class": "hudson.model.FreeStyleProject",
      "name": "Job11",
      "url": "http://192.168.39.170:8080/job/Folder1/job/Job11/",
      "color": "notbuilt"
    }
  ],
  "primaryView": {
    "_class": "hudson.model.AllView",
    "name": "All",
    "url": "http://192.168.39.170:8080/job/Folder1/"
  },
  "views": [
    {
      "_class": "hudson.model.AllView",
      "name": "All",
      "url": "http://192.168.39.170:8080/job/Folder1/"
    }
  ]
}


・config.xml を取得する

# curl -so config.xml --user jenkins:3ab6599ee0329470aa51234eebd1be9c "http://xxx.xxx.xxx.xxx:8080/job/Folder1/job/Job11/config.xml"

# cat config.xml
<?xml version='1.0' encoding='UTF-8'?>
<project>
  <description>Job11</description>
  <keepDependencies>false</keepDependencies>
  <properties/>
  <scm class="hudson.scm.NullSCM"/>
  <canRoam>true</canRoam>
  <disabled>false</disabled>
  <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
  <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
  <triggers/>
  <concurrentBuild>false</concurrentBuild>
  <builders/>
  <publishers/>
  <buildWrappers/>


・CRUMB の取得

# curl --user jenkins:3ab6599ee0329470aa51234eebd1be9c "http://192.168.39.170:8080/crumbIssuer/api/json" | jq
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   131  100   131    0     0  64247      0 --:--:-- --:--:-- --:--:--  127k
{
  "_class": "hudson.security.csrf.DefaultCrumbIssuer",
  "crumb": "8102868e8f301ce58b9f0ce4dbd431f0",
  "crumbRequestField": "Jenkins-Crumb"
}


・新規ジョブの登録

# curl -s -XPOST --data-binary @config.xml -H "Content-Type:text/xml" --user jenkins:3ab6599ee0329470aa51234eebd1be9c -H "Jenkins-Crumb: 8102868e8f301ce58b9f0ce4dbd431f0" "http://jenkins:jenkins@xxx.xxx.xxx.xxx:8080/job/Folder2/createItem?name=NewJob"

取得した config.xml を元に新たなジョブを登録しています。


今回はとりあえずこんなところで。API 使ったビルドなどはまた別途書きたいと思います。

以上です。

[環境情報]
Jenkins 2.60.3