🔄

GitHubサブモジュールの自動更新システムの構築

に公開

解法

GitHubで複数のリポジトリがサブモジュール関係にある場合、親モジュールの更新を自動的に子リポジトリに反映させるシステムを構築できます。

GitHub Actions + Repository Dispatch を使用したアプローチ

1. サブモジュール側(RepoB)での変更検知

# .github/workflows/notify-dependents.yml
name: Notify Dependent Repositories
on:
  push:
    branches: [main]
    paths:
      - 'specific-resource-path/**'

jobs:
  notify:
    runs-on: ubuntu-latest
    steps:
      - name: Notify dependent repositories
        run: |
          REPOS=("owner/RepoA" "owner/RepoC")
          
          for repo in "${REPOS[@]}"; do
            curl -X POST \
              -H "Accept: application/vnd.github.v3+json" \
              -H "Authorization: token ${{ secrets.REPO_DISPATCH_TOKEN }}" \
              https://api.github.com/repos/$repo/dispatches \
              -d '{"event_type":"submodule_update","client_payload":{"repository":"${{ github.repository }}"}}'
          done

2. 依存リポジトリ側(RepoA)での自動更新

# .github/workflows/update-submodule.yml
name: Update Submodule
on:
  repository_dispatch:
    types: [submodule_update]

jobs:
  update-submodule:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Update submodule
        run: |
          git config --global user.email "action@github.com"
          git config --global user.name "GitHub Action"
          git submodule update --remote --merge

      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v5
        with:
          commit-message: "chore: update submodule"
          title: "Auto-update submodule"
          branch: "auto-update-submodule"

解説

Repository Dispatch の仕組み

Repository Dispatchは、外部からリポジトリのワークフローをトリガーできるGitHub APIの機能です。サブモジュールが更新されたタイミングで、依存する全てのリポジトリに通知を送信できます。

自動化の流れ

  1. RepoBで変更検知: 特定のパスにプッシュが発生
  2. 通知送信: 依存リポジトリリストに基づいてRepository Dispatchイベントを送信
  3. 自動更新: 各依存リポジトリでサブモジュールを最新に更新
  4. Pull Request作成: 変更内容をレビュー可能な形で提案

設定要件

Personal Access Token の作成

  • repo スコープを持つトークンを作成
  • サブモジュール側のSecrets設定で REPO_DISPATCH_TOKEN として保存

権限設定

各依存リポジトリで、サブモジュールからのrepository_dispatchイベントを受信できるよう設定

Discussion