CircleCIで「特定ファイルの変更だけなら、Job内の処理をスキップする」を試してみる

CircleCIで「特定ファイルの変更だけなら、Job内の処理をスキップする」を試してみる

CI/CDの実行時間を短縮できます。
Clock Icon2025.06.09

.github内のファイルを更新することがあります。たとえば、Pull Requestのテンプレートファイルです。

このファイルの更新に伴ってCI/CDも動きます。リポジトリが育ってくると、CI/CDも長くなります。そこで、「Pull Requestのテンプレートファイルの変更だけなら、Job内の不要な処理をスキップする(Unit TestやDeployを想定)」を試してみます。なお、CI/CDは、CircleCIを利用します。

おすすめの方

  • CircleCIで特定ファイルの更新だけのとき、Jobをskipしたい方
  • CircleCIの「circleci-agent step halt」コマンドの動作を知りたい方
  • CircleCIのシンプルな設定ファイルを知りたい方

まずは、CircleCIを準備する

CircleCIの設定ファイルを作成する

mkdir .circleci
touch .circleci/config.yml

次の2つのJobを順番に実行します。

  • unit-test
  • deploy
.circleci/config.yml
version: 2.1

executors:
  normal_container:
    docker:
      - image: cimg/node:lts

jobs:
  unit-test:
    executor: normal_container
    steps:
      - checkout
      - run:
          name: unit-test
          command: |
            echo "run unit-test"

  deploy:
    executor: normal_container
    steps:
      - run:
          name: deploy
          command: |
            echo "run deploy"

workflows:
  release:
    jobs:
      - unit-test
      - deploy:
          requires:
            - unit-test

CircleCIでプロジェクトを設定する

01_circleci

無事に動きました。

02_circleci

Pull Requestのテンプレートファイルを作成する

mkdir .github
touch .github/pull_request_template.md

ファイルの中身は、適当です。

.github/pull_request_template.md
This is pull request template file.

Pull Requestのテンプレートファイルの変更だけなら、Job内の処理をスキップする(CircleCI)

「Pull Requestのテンプレートファイルの変更だけか?」を判断するスクリプトを作成し、Jobを終了させます。

「Pull Requestのテンプレートファイルの変更だけか?」を判断するスクリプトを作成

check_skip_job.sh
#!/bin/bash

# pull_request_template.md のみが変更されているか? を確認するスクリプト

# 変更されたファイルを取得
changed_files=$(git diff --name-only "origin/main" "HEAD")

if [[ "$changed_files" == ".github/pull_request_template.md" ]]; then
    echo "Only pull_request_template.md has been changed."
    exit 0
fi

echo "Changes detected in files other than pull_request_template.md."
exit 1

なお、git commitする際は、パーミッションを変更しておきます。

chmod 755 check_skip_job.sh

スクリプトの結果によって、Jobを終了させる

「check_skip_job」の実行結果によって、CircleCIのJobを終了させます。

.circleci/config.yml
version: 2.1

executors:
  normal_container:
    docker:
      - image: cimg/node:lts

commands:
  check_skip_job:
    description: "Check if job should be skipped"
    steps:
      - run:
          name: Check if job should be skipped
          command: |
            if ./check_skip_job.sh; then
              echo "This job is skipped."
              circleci-agent step halt
            else
              echo "Job will run."
            fi

jobs:
  unit-test:
    executor: normal_container
    steps:
      - checkout
      - check_skip_job
      - run:
          name: unit-test
          command: |
            echo "run unit-test"

  deploy:
    executor: normal_container
    steps:
      - checkout
      - check_skip_job
      - run:
          name: deploy
          command: |
            echo "run deploy"

workflows:
  release:
    jobs:
      - unit-test
      - deploy:
          requires:
            - unit-test

CircleCIのJobを確認する

Pull Requestのテンプレートファイルのみを変更した場合

それぞれのJobにて、後続のstepがskipされていました。

11_circleci_workflow

12_circelci_job

13_circelci_job

Pull Requestのテンプレートファイルと他のファイルを変更した場合

それぞれのJobは、途中でskipされませんでした。

21_circleci_workflow

22_circelci_job

23_circelci_job

Pull Requestのテンプレートファイル以外を変更した場合

それぞれのJobは、途中でskipされませんでした。

31_circleci_workflow

32_circelci_job

33_circelci_job

さいごに

「README.mdだけの更新ならskipする」などでも活用できると思います。

参考

Share this article

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.