はじめに
GitHub Actions で composite action なるものが 1 年ほど前にリリースされていたが使えてなかったので使ってみた。
Creating a composite action - GitHub Docs
In this guide, you'll learn how to build a composite action.
![Creating a composite action - GitHub Docs](https://github.githubassets.com/images/modules/open_graph/github-logo.png)
npm エコシステムでは npm install
や npm run xxx
みたいなコマンドを実行するようなアクションを毎回定義しては似たような YAML ファイルが増えていた。今回は composite action を利用してリファクタリングしてみる。
composite action を作る
.github/actions/npm/action.yaml
を作成し、共通箇所を抜き出した。
inputs:
node-version-file:
required: false
default: ".tool-versions"
cache:
required: false
default: "npm"
cache-dependency-path:
required: false
default: "./package-lock.json"
runs:
using: "composite"
steps:
- name: Setup node
uses: actions/setup-node@v3
with:
node-version-file: ${{ inputs.node-version-file }}
cache: ${{ inputs.cache }}
cache-dependency-path: ${{ inputs.cache-dependency-path }}
- name: Install dependencies
shell: bash
run: npm ci
ポイントとしては以下になる。
using
句ではcompose
を指定する。inputs
句で composite action 側にパラメータを渡すことができる。- 詳細としては他にも
require
,default
,description
が指定できる。 - Metadata syntax for GitHub Actions - GitHub Docs
- 詳細としては他にも
run
句ではshell
の設定が必須となっている。
composite action を使う
ここでは、例として .github/workflows/ci.yaml
という CI 用に定義しておいた action ファイルを上記で作成した composite action を使用するように修正した。
name: "build"
on:
pull_request:
jobs:
build:
runs-on: ubuntu-latest
timeout-minutes: 3
steps:
- name: Checkout source
uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}
+ - name: Initialize node & npm
+ uses: ./.github/actions/npm
- - name: Setup node
- uses: actions/setup-node@v3
- with:
- node-version-file: ${{ inputs.node-version-file }}
- cache: ${{ inputs.cache }}
- cache-dependency-path: ${{ inputs.cache-dependency-path }}
-
- - name: Install dependencies
- run: npm ci
- name: Execute prettier
run: npm run prettier
- name: Execute eslint
run: npm run eslint
- name: Build
run: npm run build
uses
句で作成した composite action のファイルパスを指定する。
注意点としてはリポジトリにチェックアウトする最初の処理も共通化したいところだが、composite action 自体がこのリポジトリに含まれるのでこれを共通化することはできない。
まとめ
これだけで composite action を利用でき、毎回書いていた共通処理をまとめることができた!
参考
Creating a composite action - GitHub Docs
In this guide, you'll learn how to build a composite action.
![Creating a composite action - GitHub Docs](https://github.githubassets.com/images/modules/open_graph/github-logo.png)
ワークフローの再利用 - GitHub Docs
既存のワークフローを再利用してワークフローを作成するときに重複を回避する方法について説明します。
![ワークフローの再利用 - GitHub Docs](https://github.githubassets.com/images/modules/open_graph/github-logo.png)
GitHub Actionsの共通したアクションを切り出してシンプルに保つ
![GitHub Actionsの共通したアクションを切り出してシンプルに保つ](https://res.cloudinary.com/zenn/image/upload/s--er28RoCh--/c_fit%2Cg_north_west%2Cl_text:notosansjp-medium.otf_55:GitHub%2520Actions%25E3%2581%25AE%25E5%2585%25B1%25E9%2580%259A%25E3%2581%2597%25E3%2581%259F%25E3%2582%25A2%25E3%2582%25AF%25E3%2582%25B7%25E3%2583%25A7%25E3%2583%25B3%25E3%2582%2592%25E5%2588%2587%25E3%2582%258A%25E5%2587%25BA%25E3%2581%2597%25E3%2581%25A6%25E3%2582%25B7%25E3%2583%25B3%25E3%2583%2597%25E3%2583%25AB%25E3%2581%25AB%25E4%25BF%259D%25E3%2581%25A4%2Cw_1010%2Cx_90%2Cy_100/g_south_west%2Cl_text:notosansjp-medium.otf_34:ikkitang%2Cx_220%2Cy_108/bo_3px_solid_rgb:d6e3ed%2Cg_south_west%2Ch_90%2Cl_fetch:aHR0cHM6Ly9zdG9yYWdlLmdvb2dsZWFwaXMuY29tL3plbm4tdXNlci11cGxvYWQvYXZhdGFyLzk5ZjYyZGQwYWQuanBlZw==%2Cr_20%2Cw_90%2Cx_92%2Cy_102/co_rgb:6e7b85%2Cg_south_west%2Cl_text:notosansjp-medium.otf_30:%25E3%2582%25B9%25E3%2582%25BF%25E3%2583%2595%25E3%2582%25A7%25E3%2582%25B9%25E3%2583%2586%25E3%2583%2583%25E3%2582%25AF%25E3%2583%2596%25E3%2583%25AD%25E3%2582%25B0%2Cx_220%2Cy_160/bo_4px_solid_white%2Cg_south_west%2Ch_50%2Cl_fetch:aHR0cHM6Ly9saDMuZ29vZ2xldXNlcmNvbnRlbnQuY29tL2EtL0FPaDE0R2hRdXgzaUJOYkZBSk54bUZBZ1hURE5HM3RLTWNoZGg2WHA0STN6PXMyNTAtYw==%2Cr_max%2Cw_50%2Cx_139%2Cy_84/v1627283836/default/og-base-w1200-v2.png)
【GitHub Actions】Composite ActionのTipsと注意点 - すぎしーのXRと3DCG
概要 動作環境 用語 使用するプロジェクト Composite Actions の実装 Composite Actions を組み込むワークフロー Composite Actions 対応 Composite Actionのファイル構成 Composite Actionの組込方針 .Netのビルドの一連の流れを集約 (複数のステップを1つに集約) upload-artifactの有効日数3日をデフォルト化 (入力のデフォルト値を独自に定義) Composite Action を使用 Composite Actionの細かな仕様 Actionの補足 通常のActionとComposite Act…
![【GitHub Actions】Composite ActionのTipsと注意点 - すぎしーのXRと3DCG](https://cdn.blog.st-hatena.com/images/theme/og-image-1500.png)