Contents
GitHub Actions のテンプレート概念と活用シーン
GitHub Actions の ワークフローテンプレート は、組織内で共通化した CI/CD 手順を「設計図」として管理できる仕組みです。
同一のビルド・テスト・デプロイ手順を複数リポジトリに展開すると、設定ミスやバージョン差異が発生しやすくなりますが、テンプレート化することで 保守コストの削減 と 標準化された品質 を実現できます。
テンプレートを導入するメリット
| 項目 | 内容 |
|---|---|
| 再利用性 | workflow_call で呼び出すだけで同一ロジックを他リポジトリでも使用可能 |
| 一元管理 | テンプレート側の変更が即座に全プロジェクトへ反映され、バージョン管理が容易になる |
| 誤設定防止 | メタデータ (action.yml) に入力項目や権限を明示できるため、UI/CLI からの作成時にチェックが走る |
テンプレートの作成とメタデータ
テンプレートはリポジトリの .github/workflow-templates/ 配下に配置します。
GitHub が期待するファイル構成は次の通りです(※2024‑04 の公式仕様)【1】:
|
1 2 3 4 5 6 7 |
.github/ └─ workflow-templates/ ├─ <template-name>/ │ ├─ action.yml ← メタデータ (inputs, outputs, permissions) │ ├─ main.yml ← 実際のワークフロー定義 │ └─ README.md ← テンプレート利用者向け説明書 |
action.ymlは必須です。inputs.ymlのような別ファイルはサポートされていません。README.mdに記載された情報は GitHub UI の「Add workflow → Use a template」画面で自動的に表示されます。
action.yml の最小例
|
1 2 3 4 5 6 7 8 9 10 11 12 |
name: "Node.js Build" description: "Standard Node.js build & test workflow" inputs: node-version: description: "Node.js version to use" required: true default: "20" runs: using: "workflow-call" permissions: contents: read # 最小権限の例(後述参照) |
公式 starter‑workflows と workflow_call の組み合わせ
GitHub が提供する actions/starter-workflows リポジトリは、最新ランナーイメージやベストプラクティスに沿ったテンプレートが揃っています【2】。
これらをそのまま workflow_call 用に流用すれば、ゼロから書く手間を大幅に削減できます。
starter‑workflows リポジトリからテンプレートを取得する手順
- リポジトリをクローン
bash
gh repo clone actions/starter-workflows - 必要なディレクトリ(例:
nodejs,java,docker)からmain.ymlと同名の README をコピーし、上記テンプレート構成に合わせて配置する。 - コピー後は
action.ymlを追加し、inputsやpermissionsを自組織向けに調整する。
Tip:GitHub Docs の「[Organization workflow templates]」ページでは UI からテンプレートを作成・管理する手順が詳細に掲載されています【3】。
workflow_call の基本構文と実装例
以下は最小限の再利用可能ワークフローです。action.yml に入力項目を定義し、呼び出し側からバージョンだけ差し替えて使用します。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# .github/workflows/reusable-build.yml name: Reusable Build on: workflow_call: inputs: node-version: description: "Node.js version" required: true type: string outputs: artifact-path: description: "Uploaded artifact path" jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Setup Node.js uses: actions/setup-node@v3 with: node-version: ${{ inputs.node-version }} - run: npm ci && npm run build - name: Upload artifact id: upload uses: actions/upload-artifact@v4 with: name: dist path: ./dist outputs: artifact-path: ${{ steps.upload.outputs.artifact-path }} |
呼び出し側の記述は次のとおりです。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# .github/workflows/ci.yml name: CI on: push: branches: [main] jobs: call-build: uses: ./.github/workflows/reusable-build.yml with: node-version: "20" |
組織全体でのテンプレート共有
共有テンプレートリポジトリの作り方
.githubリポジトリを作成(例:org/.github)- 公開・プライベートは組織のポリシーに合わせる。
- 先ほど示したディレクトリ構成でテンプレートを配置する。
|
1 2 3 4 5 6 7 8 9 10 11 |
.github/ └─ workflow-templates/ ├─ node-build/ │ ├─ action.yml │ ├─ main.yml │ └─ README.md └─ docker-publish/ ├─ action.yml ├─ main.yml └─ README.md |
mainブランチにプッシュすると、組織内のすべてのリポジトリで 「Add workflow → Use a template」 が自動的に有効化されます【4】。
GitHub UI からテンプレートを追加する手順
- 対象リポジトリの Actions タブを開く。
- 「New workflow」→画面下部の “Use a template” をクリック。
- 表示された一覧から目的のテンプレート(例:
Node.js Build)を選択し、Create this workflow を押すだけで.github/workflows/に YAML が生成されます。
GitHub CLI (gh) でテンプレートをコピーする方法
|
1 2 3 4 5 6 7 8 |
# .github リポジトリから node-build テンプレートをローカルに取得 gh repo clone org/.github tmp-repo cp -r tmp-repo/.github/workflow-templates/node-build . rm -rf tmp-repo # 必要に応じて inputs を埋め込んでコミット git add .github/workflows/node-build.yml git commit -m "Add Node.js Build workflow from template" |
CLI だけで UI と同等のテンプレート選択は現時点(2024‑04)では提供されていませんが、上記スクリプトを自動化すれば新規リポジトリ作成時に一括適用できます。
実践サンプルテンプレートとローカル検証
以下の 3 つのサンプルは実務で頻繁に使われるシナリオです。どれも workflow_call に対応しているので、他プロジェクトから簡単に呼び出せます。
1. テスト・ビルド(Node.js / Java)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# .github/workflows/test-build.yml name: Test & Build on: workflow_call: inputs: node-version: type: string default: "20" java-version: type: string default: "21" jobs: test-node: runs-on: ubuntu-latest strategy: matrix: os: [ubuntu-latest, windows-latest] steps: - uses: actions/checkout@v4 - name: Setup Node.js uses: actions/setup-node@v3 with: node-version: ${{ inputs.node-version }} - run: npm ci - run: npm test - name: Cache node_modules uses: actions/cache@v4 with: path: ~/.npm key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }} build-java: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Setup JDK uses: actions/setup-java@v4 with: distribution: temurin java-version: ${{ inputs.java-version }} - run: ./gradlew build |
2. Docker イメージ作成・プッシュ
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# .github/workflows/docker-build.yml name: Docker Build & Push on: workflow_call: inputs: image-name: type: string required: true dockerfile-path: type: string default: "./Dockerfile" secrets: REGISTRY_TOKEN: required: true jobs: build-and-push: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Login to GHCR run: echo "${{ secrets.REGISTRY_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin - name: Build image run: | docker build -f ${{ inputs.dockerfile-path }} \ -t ghcr.io/${{ github.repository_owner }}/${{ inputs.image-name }}:${{ github.sha }} . - name: Push image run: docker push ghcr.io/${{ github.repository_owner }}/${{ inputs.image-name }}:${{ github.sha }} |
3. リリース自動化
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# .github/workflows/release.yml name: Release Automation on: workflow_call: inputs: tag-prefix: type: string default: "v" secrets: GH_TOKEN: required: true jobs: create-release: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Determine next version id: version run: | latest=$(git describe --tags $(git rev-list --tags --max-count=1) || echo "0.0.0") IFS='.' read -r major minor patch <<< "${latest#${{ inputs.tag-prefix }}}" new="${major}.${minor}.$((patch+1))" echo "new_version=${new}" >> $GITHUB_OUTPUT - name: Create GitHub Release uses: softprops/action-gh-release@v2 with: tag_name: "${{ inputs.tag-prefix }}${{ steps.version.outputs.new_version }}" generate_release_notes: true env: GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} |
act でローカル検証する手順
- インストール(Homebrew 推奨)
bash
brew install act - シークレットファイルを作成(
.secrets)
text
REGISTRY_TOKEN=ghp_XXXXXXXXXXXXXXXXXXXX
GH_TOKEN=ghp_YYYYYYYYYYYYYYYYYYYY - 対象ジョブだけ実行
bash
act -j build-and-push --secret-file .secrets - デバッグ情報が必要なときは
-vオプションを付与。
ローカルでのシミュレーションにより、PR マージ前にワークフローの挙動や依存関係エラーを早期に検出できます。
テンプレートの保守・セキュリティベストプラクティス
バージョン管理と Dependabot の設定例
|
1 2 3 4 5 6 7 8 9 10 11 12 |
# .github/dependabot.yml version: 2 updates: - package-ecosystem: "github-actions" directory: "/.github/workflow-templates" schedule: interval: "weekly" commit-message: prefix: "chore(deps)" include: "scope" open-pull-requests-limit: 5 |
directoryをテンプレート格納パスに限定することで、他ワークフローへの影響を防ぎます。- Pull Request が作成されたらレビューと自動テスト(例:
act test) を走らせ、本番ブランチへマージします。
最小権限での GITHUB_TOKEN 設定例
|
1 2 3 4 5 6 |
# .github/workflows/reusable-build.yml の冒頭に追記 permissions: contents: read # ソースコードの読み取りのみ packages: write # Docker イメージのプッシュが必要な場合だけ許可 id-token: write # OIDC 用トークン(必要時) |
- デフォルトでは
GITHUB_TOKENはリポジトリ全体に書き込み権限があります。 permissions:キーで 最小権限 を明示すると、意図しない操作や漏洩リスクを低減できます【5】。
シークレットと環境ごとの管理例
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# Organization → Settings → Secrets で作成したシークレット例 REGISTRY_TOKEN # Docker Hub 用(production のみ) GHCR_TOKEN # GitHub Container Registry 用(全環境共通) # 環境設定(Settings → Environments)例: production environment: name: production url: https://example.com reviewers: - team: ops-team secrets: REGISTRY_TOKEN # この環境でだけ参照可能 |
- 環境ごとのシークレット を利用すれば、ステージングと本番で異なる資格情報を安全に切り替えられます。
- PR テンプレートに「シークレットは
secrets.経由か?」というチェック項目を追加すると、レビュー時の抜け漏れ防止につながります。
まとめ
GitHub Actions のテンプレート機能は 再利用性・保守性・セキュリティ を同時に高める強力なツールです。
1. .github/workflow-templates/ 配下に action.yml と本体の main.yml を置き、メタデータで UI/CLI から使えるようにする。
2. 公式 starter‑workflows リポジトリをベースにしつつ、組織固有の入力項目や最小権限設定を追加。
3. .github リポジトリでテンプレートを一元管理し、UI と gh CLI から手軽に適用。
4. Dependabot・permissions: キー・環境シークレットで 継続的なセキュリティ強化 を実現する。
上記の流れとサンプルを参考にすれば、チーム全体で安全かつ高速に CI/CD パイプラインを標準化できるはずです。
参考リンク
- GitHub Docs – Creating a workflow template
https://docs.github.com/ja/actions/using-workflows/creating-a-reusable-workflow-template - actions/starter‑workflows リポジトリ(GitHub)
https://github.com/actions/starter-workflows - GitHub Docs – Creating workflow templates for an organization
https://docs.github.com/ja/actions/using-workflows/creating-a-reusable-workflow-template#creating-an-organization-wide-template-repository - GitHub Docs – Using a workflow template in the UI
https://docs.github.com/ja/actions/managing-workflow-templates/using-a-workflow-template-in-the-ui - GitHub Docs – Permissions for the GITHUB_TOKEN
https://docs.github.com/ja/actions/security-guides/automatic-token-authentication#permissions-for-the-github_token