GitHub PRにラベルがないとCIを失敗させる方法:Actions + Branch Protection

スポンサーリンク

GitHub PRに「bug」「feature」「chore」などのラベルを必ず付けるルールを運用していても、付け忘れは起こりがちです。GitHub ActionsでラベルチェックのCIジョブを作り、Branch Protectionの required status checks と組み合わせることで、ラベルなしのPRをマージ不能にできます。

仕組み

  1. PRが作成・更新・ラベル変更されたときに Actions ワークフローを実行する
  2. PRに必要なラベルが付いているか確認する
  3. 付いていなければジョブを失敗させる
  4. Branch Protection の required status checks に登録して、マージをブロックする

ワークフローの作成

.github/workflows/require-label.yml を作成します。

name: Require Label

on:
  pull_request:
    types: [opened, synchronize, reopened, labeled, unlabeled]

jobs:
  check-label:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/github-script@v7
        with:
          script: |
            const labels = context.payload.pull_request.labels.map(l => l.name);
            const required = ['bug', 'feature', 'chore', 'docs'];

            const hasRequired = required.some(l => labels.includes(l));

            if (!hasRequired) {
              core.setFailed(
                `ラベルが必要です。次のうちいずれかを付けてください: ${required.join(', ')}`
              );
            } else {
              console.log(`ラベル確認OK: ${labels.join(', ')}`);
            }

ポイント

  • typeslabeledunlabeled を入れることで、ラベルの付け外し時にも再チェックが走る
  • required 配列に必須ラベルの候補を列挙する(いずれか1つあればOK)
  • core.setFailed() でジョブを失敗させると、PRのチェック欄に ❌ が表示される

複数ラベルをすべて必須にする場合

「type系ラベル」「priority系ラベル」を両方必須にする場合は、グループごとにチェックします。

- uses: actions/github-script@v7
  with:
    script: |
      const labels = context.payload.pull_request.labels.map(l => l.name);

      const typeLabels = ['bug', 'feature', 'chore', 'docs'];
      const priorityLabels = ['priority:high', 'priority:medium', 'priority:low'];

      const hasType = typeLabels.some(l => labels.includes(l));
      const hasPriority = priorityLabels.some(l => labels.includes(l));

      const errors = [];
      if (!hasType) errors.push(`種別ラベルが必要: ${typeLabels.join(', ')}`);
      if (!hasPriority) errors.push(`優先度ラベルが必要: ${priorityLabels.join(', ')}`);

      if (errors.length > 0) {
        core.setFailed(errors.join('\n'));
      }

Branch Protection でマージをブロックする

CIジョブが失敗するだけでは、管理者はマージできてしまいます。Branch Protection の required status checks に登録することで、全員に対してマージをブロックできます。

  1. GitHubリポジトリの Settings → Branches を開く
  2. main(または対象ブランチ)の Branch protection rules を編集
  3. Require status checks to pass before merging を有効にする
  4. 検索欄に check-label(ワークフローの jobs: キー名)と入力して追加する
  5. Require branches to be up to date before merging も有効にする(推奨)

これでラベルなしのPRは ❌ のままとなり、マージボタンがグレーアウトします。

ラベルの作成

GitHubのラベルは Issues → Labels から作成できます。CLIで一括作成する場合:

gh label create bug --color d73a4a --description "バグ修正"
gh label create feature --color 0075ca --description "新機能"
gh label create chore --color e4e669 --description "雑務・依存関係更新"
gh label create docs --color 0052cc --description "ドキュメント修正"

PRテンプレートと組み合わせる

PRテンプレートにラベル付けを促すコメントを入れておくと、付け忘れを減らせます。.github/pull_request_template.md に記載例:

## 概要



## チェックリスト

- [ ] ラベルを付けた(bug / feature / chore / docs のいずれか)
- [ ] レビュワーをアサインした

まとめ

設定 効果
labeled / unlabeled イベント ラベル変更のたびに再チェック
core.setFailed() CIを失敗させてPRに ❌ を表示
Branch Protection の required status checks マージをブロックして強制化
グループ別チェック 種別・優先度など複数ルールを同時に強制

PRテンプレートの作り方は「GitHubのPR・ISSUEテンプレートを作る方法」を参照してください。

GitHub ActionsのシークレットをCLIで管理する方法は「GitHub ActionsのシークレットをCLIで設定・確認する方法」を参照してください。