CircleCIで変更があった箇所だけに限定してビルドする
CircleCi が公式で path-filtering という orb を提供しているのでそれを使う。現在のバージョンは 0.1.1。
詳しくは公式のドキュメントにも書いてある。https://circleci.com/docs/2.0/configuration-cookbook/#execute-specific-workflows-or-steps-based-on-which-files-are-modified
以下、自分がやったこと
- Project ページ (自分の場合は GitHub を使っているので?
https://app.circleci.com/pipelines/github/$org_name/$project_name
に行って、Project Settings > Advanced > Enable dynamic config using setup workflows にチェックをつける - 設定ファイルを追加
例えば以下のようなディレクトリ構成を想定する。Gradle を使ったマルチプロジェクト構成で、project1 と project 2 がサブプロジェクトとなっているような場合。project1 と project2 がお互いに依存関係がない場合、project1 のファイルを変えた時は project 1に関する CI だけ走らせたいという時(project2 も同様)。
. ├── .circleci │ ├── config.yml │ └── continue_config.yml ├── build.gradle.kts ├── project1 │ └── ... └── project2 └── ...
ポイントは
正規表現でマッチした変更ファイルに変更があれば
mappings
で定義した parameter に値が設定される(1)- mappings の1行は pathReg, parameter, value が空白でつながっているフォーマット https://github.com/CircleCI-Public/path-filtering-orb/blob/da0876ad145f3e99ed71537468768a44d6fd0fce/src/scripts/create-parameters.py#L65
- Python の正規表現 https://github.com/CircleCI-Public/path-filtering-orb/blob/da0876ad145f3e99ed71537468768a44d6fd0fce/src/scripts/create-parameters.py#L66
(1) を
config-path
で設定したファイル(ここでは.circleci/continue_config.yml
) 渡して実行config-path
で設定したファイルはworkflows
>$your_workflow_name
>when
のなかで parameter にアクセスして、true だったらその workflow を実行
たとえば project1/Main.kt を変更したとすると、project1/.* run-build-project1 true
のルールによりrun-build-project1
に true
がセットされる 。project1 workflow は pipeline.parameters.run-build-project1
が true
なので実行される。この時 project2 は pipeline.parameters.run-build-base
とpipeline.parameters.run-build-project1
が両方 false
(default 値)なので実行されない。
.circleci/config.yaml
version: 2.1 setup: true orbs: path-filtering: circleci/path-filtering@0.1.1 workflows: always-run: jobs: - path-filtering/filter: name: check-updated-files mapping: | build.gradle.kts run-build-base true project1/.* run-build-project1 true project2/.* run-build-project2 true base-revision: master config-path: .circleci/continue_config.yml
.circleci/continue_config.yml
version: 2.1 jobs: test_project1: - checkout - build: name: project1 test pwd: project1 command: | ./gradlew test test_project2: - checkout - build: name: project2 test pwd: project2 command: | ./gradlew test parameters: run-build-base: type: boolean default: false run-build-project1: type: boolean default: false run-build-project2: type: boolean default: false workflows: version: 2 project1: jobs: - test_project1 when: or: - << pipeline.parameters.run-build-base >> - << pipeline.parameters.run-build-project1 >> project2: jobs: - test_project2 when: or: - << pipeline.parameters.run-build-base >> - << pipeline.parameters.run-build-project2 >>