1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

GitHub ActionsをローカルPC上で試せるactを使ってみた

Posted at

前提

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をインストールしてください。

パッケージマネージャーを使用して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句を書き換えると、結果が変わります。

.github/workflows/main.yml
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 installnpm testが実行されるようです。

.github/workflows/main.yml
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

さいごに

都度コミットして検証するより、はるかにストレスがなくなりそうです。積極的に使ってみたいと思います。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?