Jenkinsのビルドエグゼキュータ(ビルド状況)をREST APIで取得したい

個人的なメモ。

Jenkinsのビルドエグゼキュータは、たとえば https://ci.jenkins-ci.org/ajaxExecutors のように /ajaxExecutors 経由でJenkinsのウェブサイトに定期的に描画されているものですが、これはHTMLのスニペットを定期的に配信していることで実現していて、たとえばTwitter BootstrapのカッコイイUIでこのビルドエグゼキュータを描画したいといった場合、HTMLのスニペットの定期取得では難しいこともあるでしょう。

(以下はそのスニペット

<div id="executors" class="container-fluid pane-frame"><div class="row"><div class="col-xs-24 pane-header"><a title="collapse" class="collapse" href="/toggleCollapse?paneId=executors"><img style="width: 16px; height: 16px; " alt="collapse" class="icon-collapse icon-sm" src="/static/8e43c810/images/16x16/collapse.png" /></a><a href='/computer/'>ビルド実行状態</a></div></div><div class="row pane-content"><table class="pane "><colgroup><col width="30" /><col width="200*" />

<!-- 以下略 -->

ということで、Jenkinsに備え付けられているREST APIから、ビルドエグゼキュータの情報を取ろうと考えつくわけですが、どこを取ればいいかわからない。

まあ正解からいうと、 https://ci.jenkins-ci.org/ (公式Jenkins)を例にとったとして、 https://ci.jenkins-ci.org/computer/api/json?pretty=true を取ればいいんですが、エグゼキュータ描画に必要な情報が入っていない。( ?pretty=true は実装時には要らないです)

なので、より深い情報を取るために https://ci.jenkins-ci.org/computer/api/json?pretty=true&depth=2 などと depth=2 を指定すりゃあいいんですが、

  • 余計な情報つきすぎ
  • ノードをON/OFFしていると例外をBeanからシリアライズできないとかで500出る(バグ?)

といった苦難にあうので、きちんと取るべき情報を指定してあげましょう。

https://ci.jenkins-ci.org/api/ に書いてあるんですが、RESTで取る際のデータはある程度選べます。

今回のビルドエグゼキュータを自分で描画したいといった場合、JSON上の下記の構造の部分だけデータが取れれば、まあだいたい僕の欲望は満たせました。

  • computer (複数) スレーブごとに存在
    • displayName スレーブの名前
    • executors (複数) スレーブのエグゼキュータ(キュー)ごとに存在
      • progress ジョブの進捗%
      • idle 空いているか?
      • currentExecutable 実行中ビルドの詳細
        • estimatedDuration ジョブの完了見積もりミリ秒
        • fullDisplayName ビルドのフル名
        • url ビルドのURL

上記のデータだけを取る場合は tree パラメータを駆使して、 https://ci.jenkins-ci.org/computer/api/json?pretty=true&tree=computer[displayName,executors[progress,idle,currentExecutable[estimatedDuration,fullDisplayName,url]]] とすればよいです。前までつけていた depth は不要になります。

下記に(Lisp並みに)わかりやすく分解したURLを置いておきます。

https://ci.jenkins-ci.org/computer/api/json?pretty=true&tree=
    computer[
        displayName,
        executors[
            progress,
            idle,
            currentExecutable[
                estimatedDuration,
                fullDisplayName,
                url]]]

上記のURLで下記のようなJSONがとれるので、後は定期的に再取得しなおしたり、再取得するまでの間は estimatedDurationprogress からビルドの予定時間を算出してスムーズにプログレスバーでビルド進捗率を描画するなりすればいいと思います。

{
    "computer" : [
    {
      "displayName" : "master",
      "executors" : [
        {
          "currentExecutable" : {
            "estimatedDuration" : 8807897,
            "fullDisplayName" : "infra_update_center_v3 #57",
            "url" : "http://ci.jenkins-ci.org/job/infra_update_center_v3/57/"
          },
          "idle" : false,
          "progress" : 81
        },
        {
          "currentExecutable" : null,
          "idle" : true,
          "progress" : -1
        }
      ]
    },
    {
      "displayName" : "celery",
      "executors" : [
        {
          "currentExecutable" : null,
          "idle" : true,
          "progress" : -1
        },
        {
          "currentExecutable" : null,
          "idle" : true,
          "progress" : -1
        }
      ]
// 以下略

ちなみになんでこんなことを僕が調べているかというと、開発サーバ上で動くJenkins数台などを巻き込んだSPAをAngularJSとか使って作っているからです。