LoginSignup
3
2

Composite Actionsを使う際にハマったポイント

Posted at

はじめに

Github Actionsで何度も繰り返し記述している部分を共通化したくてググったら、Composite Actions(複合アクション)というものを使うといいらしい。

でも、ネット上の記事を参考に書いても全然できない。
Githubのドキュメントも 参考にならない。

ハマった部分の覚え書きです。

基本ルール & 完成形

  • 共通化したい処理を書くファイル名はaction.yml
  • 配置ディレクトリはどこでもいい
  • runsに続けて書く
  • using: "composite"を指定する
action.yml(定義)
runs:
  using: "composite"
  steps: # ↓ ここに共通化したい処理を列挙する
    - name: checkout
      uses: actions/checkout@v3

    - name: setup-node
      uses: actions/setup-node@v3
      with:
        node-version: 18
        cache: npm

    - name: package install
      run: npm i
      shell: bash
  • actions/checkout@v3を呼び出してから使う
  • プロジェクトルートからaction.ymlのディレクトリまでの相対パスを記述する
hoge.yml(利用側)

# ~~略~~

jobs:
  Type-Check:
    name: Type-Check
    runs-on: ubuntu-latest

    steps:
      - name: checkout local action
        uses: actions/checkout@v3
      - name: initialize
        uses: ./.github/workflows/initialize # ← 作成した共通処理の呼び出し

      - name: tsc
        run: npm run tsc

# ~~略~~

ハマったところ @action.yml

  • ファイル名

actions.yml action.yml

(指が勝手にタイポした)


  • shell: bashが必要

半分嘘。
run:を定義しているjobにのみ必要。

    - name: setup-node
      uses: actions/setup-node@v3
      with:
        node-version: 18
        cache: npm
      # ← ここは不要

    - name: package install
      run: npm i
      shell: bash # ← ここは必要

  • using: "composite"

cは大文字ではなく小文字。

runs:
  using: "composite"

ハマったところ @利用側

  • 利用前の準備が必要

actions/checkoutを呼び出さないとアクションが使えない。
(同じsteps内の前段で呼び出しているなら不要)

    steps:
      - name: checkout local action
        uses: actions/checkout@v3 # ← これが必要
      - name: initialize
        uses: ./.github/workflows/initialize

  • action.ymlのパスは相対パスで書く

ただし、呼び出すファイルではなく プロジェクトルートからの相対パス
かつ、指定するのは ディレクトリ名

      - name: initialize
        uses: ./.github/workflows/initialize # ← これ

エラー別

  • the `uses' attribute must be a path, a Docker image, or owner/repo@ref

利用側の設定ミス。
どこかからコピペしたコードで発生。
中身のない- nameディレクティブが生まれていた。

    steps:
      - name: initialize
      - uses: actions/checkout@v3
        uses: ./.github/workflows/initialize

正しくはこう。

    steps:
      - name: checkout local action
        uses: actions/checkout@v3
      - name: initialize
        uses: ./.github/workflows/initialize

  • Can't find 'action.yml', 'action.yaml' or 'Dockerfile' under '/home/runner/work/hoge/fuga/common/actions/build'. Did you forget to run actions/checkout before running your local action?

利用側の設定ミス。
原因はふたつ。

actions/checkoutを使用していなかった

    steps:
      - name: checkout local action
        uses: actions/checkout@v3 # ← これが必要
      - name: initialize
        uses: ./.github/workflows/initialize
  • パスを書き間違えた
    • actions.ymlにしていた
    • ディレクトリではなくファイル名まで記述していた
    • コピペコードのパスそのままだった
    steps:
      - name: checkout local action
        uses: actions/checkout@v3
      - name: initialize
        uses: ./.github/actions/build ← これ
    
    • ↑を修正したにも関わらず、パスを書き間違えた
    steps:
      - name: checkout local action
        uses: actions/checkout@v3
      - name: initialize
        uses: ./.github/.workflows/initialize ← workflowsに.は不要
    

  • Invalid workflow file: .github/workflows/on_pull_request.yml#L18 The workflow is not valid. .github/workflows/hoge.yml (Line: 18, Col: 9): 'uses' is already defined .github/workflows/hoge.yml (Line: 30, Col: 9): 'uses' is already defined

  • GitHub.DistributedTask.ObjectTemplating.TemplateValidationException: The template is not valid. /home/runner/work/hoge/fuga/./.github/workflows/initialize/action.yml (Line: 13, Col: 7): Required property is missing: shell at GitHub.DistributedTask.ObjectTemplating.TemplateValidationErrors.Check() at GitHub.Runner.Worker.ActionManifestManager.ConvertRuns(IExecutionContext executionContext, TemplateContext templateContext, TemplateToken inputsToken, String fileRelativePath, MappingToken outputs) at GitHub.Runner.Worker.ActionManifestManager.Load(IExecutionContext executionContext, String manifestFile)

利用側の設定ミス。
usesを続けて書いていた。

    steps:
     - name: initialize
       uses: actions/checkout@v3
       uses: ./.github/workflows/initialize # ← これ
  • /home/runner/work/hoge/fuga/./.github/workflows/initialize/action.yml (Line: 13, Col: 7): Required property is missing: shell

定義側の設定ミス。これ → #shell-bashが必要
runがあるのにshellを指定していなかった。

    - name: setup-node
      uses: actions/setup-node@v3
      with:
        node-version: 18
        cache: npm
      # ← ここは不要

    - name: package install
      run: npm i
      shell: bash # ← ここは必要

おしまい

タイポには注意しましょう。
Github Actions初心者の誰かの助けになれば幸いです。
間違いがあればご指摘くださいmm

3
2
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
3
2