gitのカレントブランチのCircleCIのビルドステータスをCLIで確認する


CircleCIのAPI叩いて、ビルドステータスがターミナルで見れないかなぁーと思ったら、既にCLIツール作ってる人がいたのでこれを使う。

インストールはGo環境があれば、

$ go get github.com/jszwedko/circleci-cli

なければ、GitHubのRealsesページからビルド済のバイナリをダウンロードしてPATHが通ったところに置けばおk。

APIトークンがいるので、CircleCIのWeb画面から 「AccountSettings > API Tokens」から発行して環境変数としてエクスポートしておく。

$ export CIRCLE_TOKEN=XXXXXX

使い方

$ circleci-cli --help
NAME:
   circleci - Tool for interacting with the CircleCI API

USAGE:
   circleci-cli [global options] command [command options] [arguments...]

VERSION:
    ()

COMMANDS:
     projects                   Print projects
     recent-builds, recent      Recent builds for the current project
     show                       Show details for build
     list-artifacts, artifacts  Show artifacts for build (default to latest)
     test-metadata              Show test metadata for build
     retry-build, retry         Retry a build
     cancel-build, cancel       Cancel a build
     build                      Trigger a new build
     clear-cache                Clear the build cache
     add-env-var                Add an environment variable to the project (expects the name and value as arguments)
     delete-env-var             Add an environment variable to the project (expects the name as argument)
     add-ssh-key                Add an SSH key to be used to access external systems (expects the hostname and private key as arguments)
     help, h                    Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --host value, -H value   CircleCI URI (default: "https://circleci.com") [$CIRCLE_HOST]
   --token value, -t value  API token to use to access CircleCI (not needed for displaying information about public repositories) [$CIRCLE_TOKEN]
   --help, -h               show help
   --version, -v            print the version

circleci-cli recent でビルドステータスの一覧が見れる。

$ circleci-cli recent --help
NAME:
   circleci-cli recent-builds - Recent builds for the current project

USAGE:
   circleci-cli recent-builds [command options] [arguments...]

OPTIONS:
   --limit value, -l value    Maximum of builds to return -- set to -1 for no limit (default: 30) [$CIRCLE_LIMIT]
   --offset value, -o value   Offset in results to start at (default: 0) [$CIRCLE_OFFSET]
   --all, -a                  Show builds for all projects; overrides --project [$CIRCLE_ALL_BUILDS]
   --project value, -p value  Show all builds for specified project rather than the current (default: XXXX/XXXX) [$CIRCLE_PROJECT]
   --branch value, -b value   Show only builds on specified branch (cannot be used with --all); leave empty for all [$CIRCLE_BRANCH]
   --filter value, -f value   Show only builds with given status (cannot be used with --all); leave empty for all; must be one of completed,successful,failed,running [$CIRCLE_FILTER]

ブランチを特定する場合は -b でブランチ名を渡せばよいんで、 $(git symbolic-ref --short HEAD) でgitからカレントブランチを取得して渡す。 -l 5 は直近だけ見れればよいので取得件数を絞ってる。 -p のプロジェクト名は何もしてしなくてもgit remoteから推測してくれるっぽい。

$ circleci-cli recent -l 5 -b $(git symbolic-ref --short HEAD)

というのを ~/.gitconfig のエイリアスにでも登録しておくと git ci-status で見れるようになってべんり。

[ailias]
  ci-status = !circleci-cli recent -l 5 -b $(git symbolic-ref --short HEAD)

(おまけ) カレントブランチのCircleCIのWebページをブラウザで開く

$ open https://circleci.com/gh/$(git rev-parse --show-toplevel | rev | cut -f1,2 -d '/' | rev)/tree/$(git symbolic-ref --short HEAD)

URLがGitHub決め打ちだけど、 git rev-parse --show-toplevel でGitのリポジトリルートを取って、そこからディレクトリ2つ分のパスをorg/repoで特定して、末尾にカレントブランチ名を付ける。これも適当に git ci-open とかにエイリアスしておけば、Webページもすぐ開けるようになってよいんじゃなかろうか。