ハイパーマッスルエンジニア

Vim、ShellScriptについてよく書く

Githhub Actionsをローカルで実行するnectos/actでcommand not foundが出たときの対処法

f:id:rasukarusan:20210127224510p:plain:w600
Github Actionsをローカル実行できるツールnektos/actの話。

act -P でimageを指定するも必ずnode:12.6-buster-slimで実行されてしまう

actの実行時の環境は、デフォルトではnode:12.6-buster-slimが選択されるが、このimageは最小限の構成なのでgitコマンド等が入っていない。 なので下記のようにcommand not foundと出てしまい、実行に失敗する。

$ act
WARN[0000] unable to get git repo: section "remote \"origin\"" does not exist
[Test Workflow/Run Git Commands] 🚀  Start image=node:12.6-buster-slim
...略
[Test Workflow/Run Git Commands] ⭐  Run execute command!
| /github/workflow/set_value_for_formula: line 1: git: command not found
[Test Workflow/Run Git Commands]   ❌  Failure - execute command!
Error: exit with `FAILURE`: 127

しかしactにはオプションが用意されていてact -Pで実行するimageが選択できる。

act -P ubuntu-18.04=nektos/act-environments-ubuntu:18.04

ただ上記のコマンドを実行しても選択されるimageがnode:12.6-buster-slimのままになってしまう。

f:id:rasukarusan:20210128010718p:plain
nektos/act-environments-ubuntu:18.04を指定しているのにnode:12.6-buster-slimが選択される

これを解決する。

原因:yamlのruns-on:ubuntu-latestとact -P ubuntu-18.04=...がちぐはぐだったから

.github/workflows/test-workflow.yml

name: Test Workflow
on:
  push:
    tags:
      - 'v*'
jobs:
  build:
    name: Run Git Commands
    runs-on: ubuntu-latest
    steps:
      - name: execute command!
        id: set_value_for_formula
        run: |
          git status

actコマンド

act -P ubuntu-18.04=nektos/act-environments-ubuntu:18.04

yamlファイルはruns-on: ubuntu-latestなのにコマンドがact -P ubuntu-18.04=...になっているとnode:12.6-buster-slimが選択されてしまう。

対処法

ymlファイルとコマンド実行をちゃんと合わせる。

- act -P ubuntu-18.04=nektos/act-environments-ubuntu:18.04
+ act -P ubuntu-latest=nektos/act-environments-ubuntu:18.04

公式のREADMEだとubuntu-18.04で書かれているため、そのままコピペするとハマってしまうかもしれない。

確認

f:id:rasukarusan:20210128011028p:plain

ちゃんと🚀 Start image=nektos/act-environments-ubuntu:18.04になっていますね。

Tips

actの実行前にdocker pull nektos/act-environments-ubuntu:18.04しておいたほうがいい

docker pullをせずにいきなりact -P ubuntu-latest=nektos/act-environments-ubuntu:18.04を実行しても問題ないが、初回実行時はimageをpullしてくるのでめちゃくちゃ時間がかかる上に進捗が見えないので止まったように見えてしまう。
docker pullなら進捗が見えるので、先にやっておいたほうが精神上良い。

docker pull nektos/act-environments-ubuntu:18.04

yamlのruns-onubuntu-18.04だったらgitコマンドが使えるnode:12-busterが選択される

jobs:
  build:
    name: Run Git Commands
    runs-on: ubuntu-18.04

としておけばactで実行するときslimよりちょっとリッチなnode:12-busterで実行される。

f:id:rasukarusan:20210127223032p:plain
gitコマンドが使えるぐらいにはリッチなnode:12-busterが選択される

READMEにはnode:12.6-buster-slimと書かれていたので、自分が間違っているのか更新し忘れなのかわからないが、一応プルリクを出しておいた。結果はまたここに書く。

github.com

Dockerのバージョンを上げないとそもそもactが実行できない

下記のエラーが出る場合、Dockerのバージョンを上げたら実行できるようになる。

$ act
[Test Workflow/printInputs] 🚀  Start image=node:12.6-buster-slim
[Test Workflow/printInputs]   🐳  docker run image=node:12.6-buster-slim entrypoint=["/usr/bin/tail" "-f" "/dev/null"] cmd=[]
[Test Workflow/printInputs]   🐳  docker cp src=/Users/tanakanaoto/Documents/github/gitblamer/. dst=/github/workspace
Error: error during connect: Post "http://%2Fvar%2Frun%2Fdocker.sock/v1.40/exec/dd9940466880af832e8b93ba9d2fbde450785bfac24ae2a73dd2cbd99aec5ce2/start": net/http: HTTP/1.x transport connection broken: unsupported transfer encoding: "identity"

github.com

f:id:rasukarusan:20210127223005p:plain
2.2.0.4→2.5.0.0にアップデートしたら実行できるようになった