1
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?

GitHub ActionsのComposite Actionでセットアップ手順を共通化する

1
Posted at

はじめに

複数のワークフローを運用していると、checkout・pnpm・Node.js のインストールなどがジョブやファイルごとに何度も登場することがあります。

UIをコンポーネントで共通化する・同じ処理を関数で共通化するように、GitHub ActionsでもComposite Action を使うことで、共通のステップを1か所にまとめて、各ワークフローから呼び出せるようになります。

Composite Action とは

.github/actions/<name>/action.yml に共通ステップをまとめておき、ワークフローから uses: で呼び出す方法です。

ディレクトリ構成は以下のようになります。

.github/
  actions/
    setup/
      action.yml      ← 共通ステップをここにまとめる
  workflows/
    firebase-hosting-merge.yml
    firebase-hosting-pull-request.yml

Composite Action の定義方法

.github/actions/setup/action.yml を作ってこんな内容にします。

name: Setup pnpm and Node.js
description: Setup pnpm, Node.js, and install dependencies

runs:
  using: composite
  steps:
    - name: Setup pnpm
      uses: pnpm/action-setup@v4
      with:
        version: 10.7.1
        run_install: false

    - name: Setup Node.js
      uses: actions/setup-node@v4
      with:
        node-version: 22
        cache: "pnpm"

    - run: pnpm install --frozen-lockfile
      shell: bash

Composite Action定義時の注意点

  • runs.using: composite の指定が必須
  • run ステップには shell: bash の明示が必須
    • workflowでは省略可能なので、切り出し時に付け加えるのを忘れがちなので注意です。
  • シークレットは使用できない
    • シークレットを読み込む必要がある場合は、Reusable Workflow(後述)を使いましょう。

ワークフローからの呼び出し

定義したら、各ワークフローからこう呼び出します。

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4          # ← 必ずcheckoutを先に行う
      - uses: ./.github/actions/setup      # ← ローカルの Composite Action
      - run: pnpm test

uses: ./.github/actions/setup のようにリポジトリルートからの相対パスで参照します。

詰まったポイント: checkoutをComposite Actionに含めてはいけない

最初、「checkout も共通化できるじゃん」と思って actions/checkout@v4 を Composite Actionの中に書いたら、こんなエラーが出ました。

Can't find 'action.yml', 'action.yaml' or 'Dockerfile' under
'/home/runner/work/<repo>/<repo>/.github/actions/setup'.
Did you forget to run actions/checkout before running your local action?

原因は単純で、ローカルの Composite Action(./.github/actions/setup)を使うとき、ランナーはまず action.yml を読み込もうとします。
しかし、リポジトリがチェックアウトされていないと action.yml が存在しないので、読み込み自体が失敗します。

Composite Action vs Reusable Workflow

先ほどチラっと名前が出てきましたが、似た仕組みに Reusable Workflow があります。

Composite Action Reusable Workflow
単位 ステップの集合 ジョブの集合
参照方法 uses: ./.github/actions/xxx uses: ./.github/workflows/xxx.yml
向いている用途 セットアップなど複数ジョブで共通のステップ ジョブ全体を再利用したいとき
シークレットの読込 不可 可

セットアップ系の共通化なら Composite Action、ジョブごと丸ごと再利用 or シークレットが必要なら Reusable Workflowという使い分けになるかと思います。

まとめ

  • Composite Action を使うとセットアップ手順を1か所にまとめられてメンテが楽になる
  • actions/checkout@v4 は必ず呼び出し元ワークフローの最初のステップに書く(Composite Action に含めると詰まる)
  • run ステップには shell: bash の明示が必要
  • secrets は Action 内に持たせず、呼び出し元から渡す

参考リンク

1
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
1
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?