【CircleCi】パイプラインを手動トリガーにする


はじめに

CICD導入+自分で1からAndroidアプリを作成しAppiumでE2Eテスト自動化をやってみて学んだことを備忘録として残しておく。
自分で1からAndroidアプリを作成してE2Eテスト自動化を行ったリポジトリは以下。

CircleCiのパイプラインのトリガー

デフォルトの設定について

基本的にはVCSへのpushをトリガーにしてパイプラインが走るようになっている。
push時にパイプラインが走らないようにするには、[ci skip]とか[skip ci]という文字列をコミットメッセージ or 説明に記載すればいい。1

By default, CircleCI automatically builds a project whenever you push changes to a version control system (VCS). You can override this behavior by adding a [ci skip] or [skip ci] tag anywhere in a commit’s title or description.

手動でパイプラインを実行する

手動のトリガーでCircleCiのパイプラインを実行したくなる場面も出てくるが、そんな時には、Web APIをcallすればいい。2

sample.sh
curl https://circleci.com/api/v2/project/<vcs-type>/<org>/<repo>/pipeline \
  --request POST \
  --header "Circle-Token: <API_KEY>"
  • <vcs-type>
    CircleCiのconfig.ymlを置いているVCSのタイプ
    ex) github
  • <org>
    CircleCiのorganizationの名前
  • <repo>
    VCSのリポジトリ名(CircleCiのパイプラインでcheckoutするブランチのリポジトリ)
  • <API_KEY>
    CircleにアクセスするためのToken

また、この手動トリガー以外でパイプラインを実行させたくない場合には、yamlを以下のように記述し、curlコマンドも次のように変更する事でそれが実現できる。

sample.sh
JSON=$(cat << EOS
  {
    "branch": "main",
    "parameters": {
      "manual": true
    }
  }
EOS)

curl https://circleci.com/api/v2/project/<vcs-type>/<org>/<repo>/pipeline \
  --request POST \
  --header "Circle-Token: <API_KEY>"
  --header "Content-Type: application/json" \
  --data "${JSON}"
sample.yml
version: 2.1

parameters:
  manual:
    type: boolean
    default: false

workflows:
  run_pipeline:
    when: << pipeline.parameters.manual >>
    jobs:
      - hoge
      - fuga

上記のような実装したソースコード(yaml)全体は以下。