0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

保護ブランチへのPRマージ条件にpathを限定したCIの成功を指定している場合、そこ以外の変更のときCIが回らなくて条件を満たせない → そんなときはpaths-ignoreを使っていつでも回るCIにすればいい

Last updated at Posted at 2023-01-25

GitHubのブランチには保護設定をすることができます。
例えばmasterブランチにマージするためには指定したCIが通っていなければいけない、などの仕組みが作れます。

また、GitHub Actionsによるワークフローではon.pull_request.pathsで「特定のディレクトリの変更があった場合のみワークフローを回す」といったことが可能です。

では、次のようなワークフローtestを用意して、かつマージの可能条件にtestが通ったことを指定したらどうなるでしょうか?

.github/workflows/ci.yaml
name: test
on:
  pull_request:
    paths:
      - "dir/**"
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - run: echo "You have to pass this test before merging."

PullRequestがディレクトリdir/配下の変更を含むものであれば、そのPullRequestはマージできます。
しかし、例えば/README.mdへの追記などdir/外の変更であればそもそもワークフローtestが発火しないのでマージ可能条件を満たしません。

保護を無視して強制マージをする、などのふるまいによってこの問題は解決できますがスマートではありません。
そこで、次のようなワークフローをもう一つ用意することで解決しましょう。

.github/workflows/ci_ignore.yaml
name: test
on:
  pull_request:
    paths-ignore:
      - "dir/**"
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - run: echo "But you can ignore the test when changes are out of dir/"

ワークフローに同じtestという名前をつけてon.pull_request.paths-ignoredir/**を指定することで、PullRequestがどんな変更であっても必ずワークフローtestが実行されるようになります。

on.pull_request.paths-ignoreon.pull_request.pathsと逆の動きをするものです。つまり、そこに指定した箇所のみの変更があった場合はワークフローが動きません
テストが通るように、ignore用のワークフローは必ず成功する内容にしましょう。

これで、指定した箇所の変更ではCIが回ってくれるし、それ以外の変更では自動でsuccessになって保護ブランチへのマージ可能条件を満たしてくれます。

ただしこの方法だと、PullRequesstがpathsに指定した箇所の変更とそれ以外の変更とを同時に含む場合にワークフローが2つとも実行されてしまうので少し格好悪いんですよね......。
image.png

参考:https://docs.github.com/ja/repositories/configuring-branches-and-merges-in-your-repository/defining-the-mergeability-of-pull-requests/troubleshooting-required-status-checks#handling-skipped-but-required-checks

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?