JavaScriptを有効にしてください

[Github Actions] 動作確認のツールを入れる

 ·  ☕ 2 分で読めます

やること

ローカルでワークフローの挙動を確認できるようにactを入れる。

前提

  • macos 12.5
  • Homebrew 3.6.16

actについて

本番環境で試すのはコミットが汚れるし、pushも面倒。
コミットしなくても動作確認ができて便利なので、actを使用する。
https://github.com/nektos/act

※READMEにも記載されているが、完全に同じ動作とはならないため最終的には本番で確認する必要がある。

適当なファイルを用意

動作確認のために今回は以下のファイルを作成した。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
name: 'test'
on:
  push:

jobs:
  test1:
    runs-on: ubuntu-latest
    steps:
      - name: Echo hoge
        run: |
          echo 'hoge'
          echo 'hogehoge'          

インストール

READMEに従って作業を進める。
Homebrewを使用しているのでインストールは簡単。

$ brew install act

初回起動時にデフォルトで使用するイメージの選択がある。

❯ act
? 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

Default image and other options can be changed manually in ~/.actrc (please refer to https://github.com/nektos/act#configuration for additional information about file structure)  [Use arrows to move, type to filter, ? for more help]
  Large
> Medium
  Micro

実行するファイルで構文エラー等があると起動の前後でエラーが発生する。
イメージ選択まで行かずにエラー

❯ act
Error: workflow is not valid. 'publish.yaml': yaml: line 9: mapping values are not allowed in this context

イメージ選択した後でエラー

Error: Invalid run/uses syntax for job:deploy step:Checkout

上記エラーについては記述ミスを修正したら解消した。
他にイメージを取得する際のエラーも発生した。

❯ act
[publish-draft-false/deploy] 🚀  Start image=catthehacker/ubuntu:act-latest
[publish-draft-false/deploy]   🐳  docker pull image=catthehacker/ubuntu:act-latest platform= username= forcePull=false
Error: Error response from daemon: Get "https://registry-1.docker.io/v2/": proxyconnect tcp: dial tcp 192.168.65.1:3128: connect: network is unreachable

これについてはDockerdesctopの再起動で解消できた。
https://stackoverflow.com/questions/47580528/error-response-from-daemon-get-https-registry-1-docker-io-v2-dial-tcp-look

動作確認

用意しておいたファイルだとこんな感じで実行される。

❯ act
[test/test1] 🚀  Start image=catthehacker/ubuntu:act-latest
[test/test1]   🐳  docker pull image=catthehacker/ubuntu:act-latest platform= username= forcePull=false
[test/test1]   🐳  docker create image=catthehacker/ubuntu:act-latest platform= entrypoint=["/usr/bin/tail" "-f" "/dev/null"] cmd=[]
[test/test1]   🐳  docker run image=catthehacker/ubuntu:act-latest platform= entrypoint=["/usr/bin/tail" "-f" "/dev/null"] cmd=[]
[test/test1] ⭐ Run Main Echo hoge
[test/test1]   🐳  docker exec cmd=[bash --noprofile --norc -e -o pipefail /var/run/act/workflow/0] user= workdir=
| hoge
| hogehoge
[test/test1]   ✅  Success - Main Echo hoge
[test/test1] 🏁  Job succeeded

実行時にオプションを指定して、トークンを設定したりもできるみたい。


書いた人
keee
Webエンジニア

目次