GitHub ActionsをローカルPC上で試せるactを使ってみた
2022-10-03T11:14:51+09:00
前提
GitHub Actionsを作成する際、動作確認してからPRを出すと思います。
毎回GitHubにプッシュして試すより、自分の端末で試したいなというときはactが使えるそうです。
環境
- macOS: 12.6
- IntelliJ IDEA: 2021.2.3 (Community Edition)
Actについて
Rather than having to commit/push every time you want to test out the changes you are making to your
.github/workflows/
files (or for any changes to embedded GitHub actions), you can use act to run the actions locally. The environment variables and filesystem are all configured to match what GitHub provides`
.github/workflows/
や埋め込まれているGitHub Actionsの変更をテストするたびにコミット/プッシュする代わりに、actをローカルで実行できます。環境変数やファイルシステムはすべてGitHubが提供するものと一致するように構成されています。
手順
準備
dockerをインストール
actはdockerを利用してワークフローを実行します。現在はpodmanをはじめとした、他のコンテナは利用できないそうです。そのため、まずはdockerをインストールしてください。
- Mac:https://docs.docker.com/desktop/install/mac-install/
- Windows:https://docs.docker.com/desktop/install/windows-install/
- Linux:https://docs.docker.com/desktop/install/linux-install/
パッケージマネージャーを使用してactをインストール
以下のパッケージマネージャーを利用してactをインストールできるようです。
- Homebrew(Linux/macOS)
- MacPorts(maxOS)
- Chocolatey(Windows)
- Scoop(Windows)
- COPR(Linux)
- Nix(Linux/macOS)
他にはBashスクリプトを利用した方法や、手動インストールをする方法もありました。
今回はHomebrewでインストールしてみます。
brew install act
サンプルレポジトリをクローン
サンプル用にレポジトリが用意されていたのでクローンしておきます。
https://github.com/cplee/github-actions-demo
試してみる
コマンド構成について
act [<event>] [options]
If no event name passed, will default to "on: push"
If actions handles only one event it will be used as default instead of "on: push"
eventに何も記述しない場合、on: push
がデフォルトで指定されるようです。
実行可能なGitHub Actionsを見る
act -l
コマンドを実行すると、実行可能なGitHub Actionsを見ることができます。
(base) XXXX github-actions-demo-master % act -l
Stage Job ID Job name Workflow name Workflow file Events
0 test test CI main.yml push
.github/workflows/main.yml
のon句を書き換えると、結果が変わります。
name: CI
on: [push, release]
(base) XXXX github-actions-demo-master % act -l
Stage Job ID Job name Workflow name Workflow file Events
0 test test CI main.yml push,release
実行
.github/workflows/main.yml
を見てみると、ubuntu上にソースをチェックアウトしてきてnodeをセットアップをし、npm install
とnpm test
が実行されるようです。
name: CI
on: push
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- run: npm install
- run: npm test
実行してみます。最初は実行するにあたり使用するデフォルトのイメージを指定しろという指示が表示されます。とりあえずnodeだけで良いので、Micro size image
を選択します。
(base) XXXX github-actions-demo-master % act push
? Please choose the default image you want to use with act:
- Large size image: +20GB Docker image, includes almost all tools used on GitHub Actions (IMPORTANT: currently only ubuntu-18.04 platform is available)
- Medium size image: ~500MB, includes only necessary tools to bootstrap actions and aims to be compatible with all actions
- Micro size image: <200MB, contains only NodeJS required to bootstrap actions, doesn't work with all actions
ログは長いので省略しますが、テストが成功していそうです。
[CI/test] 🐳 docker exec cmd=[bash --noprofile --norc -e -o pipefail /var/run/act/workflow/3] user= workdir=
|
| > github-actions-demo@1.0.0 test /Users/XXXX/Documents/develop/github-actions-demo-master
| > mocha ./tests --recursive
|
|
|
| GET /
| ✓ should respond with hello world
|
|
| 1 passing (27ms)
|
[CI/test] ✅ Success - Main npm test
[CI/test] 🏁 Job succeeded
さいごに
都度コミットして検証するより、はるかにストレスがなくなりそうです。積極的に使ってみたいと思います。