act/nektosを使ってローカル環境でgithub actions走らせてコケた時の話

# 概要
github actionsをローカル環境で動作確認したい！という理由から、
[nektos/act](https://github.com/nektos/act)　を導入してみた。

# どんなworkflowを組みたかったか
github actions のtask内でpythonのスクリプトを実行したい。
ランナーで走らせているUbuntuにpythonをインストールしたい。

# 構成
## ディレクトリ
```shell
.
├── .github
│   └── workflows
│       ├── deploy.yaml
│       └── scripts
│           └── main.py
```
## workflows/deploy.yaml
```python
name: Test

on: [push]

jobs:
  test:
    runs-on: ubuntu-20.04
    timeout-minutes: 30

    steps:
      - name: "checkout"
        uses: actions/checkout@v4

      - name: "install python"
        uses: actions/setup-python@v4
        with:
          python-version: "3.9"
        env:
          AGENT_TOOLSDIRECTORY: /opt/hostedtoolcache

      - name: "Run a Python script"
        run: python .github/workflows/scripts/main.py
```
## workflows/scripts/main.py
```python
print("Hello World")
```

# 問題発生
```shell
$ act push

[Test/test] 🚀  Start image=node:16-buster-slim
[Test/test]   🐳  docker pull image=node:16-buster-slim platform= username= forcePull=true
[Test/test]   🐳  docker create image=node:16-buster-slim platform= entrypoint=["tail" "-f" "/dev/null"] cmd=[]
[Test/test]   🐳  docker run image=node:16-buster-slim platform= entrypoint=["tail" "-f" "/dev/null"] cmd=[]
[Test/test]   ☁  git clone 'https://github.com/actions/setup-python' # ref=v4
[Test/test] ⭐ Run Main checkout
[Test/test]   🐳  docker cp src=/Users/owner/works/**/. dst=/Users/owner/works/**
[Test/test]   ✅  Success - Main checkout
[Test/test] ⭐ Run Main install python
[Test/test]   🐳  docker cp src=/Users/owner/.cache/act/actions-setup-python@v4/ dst=/var/run/act/actions/actions-setup-python@v4/
[Test/test]   🐳  docker exec cmd=[node /var/run/act/actions/actions-setup-python@v4/dist/setup/index.js] user= workdir=
[Test/test]   💬  ::debug::Python is expected to be installed into /opt/hostedtoolcache
[Test/test]   ❓  ::group::Installed versions
[Test/test]   💬  ::debug::Semantic version spec of 3.9 is 3.9
[Test/test]   💬  ::debug::isExplicit: 
[Test/test]   💬  ::debug::explicit? false
[Test/test]   💬  ::debug::evaluating 0 versions
[Test/test]   💬  ::debug::match not found
| Version 3.9 was not found in the local cache
[Test/test]   💬  ::debug::Getting manifest from actions/python-versions@main
[Test/test]   💬  ::debug::check 3.12.0 satisfies 3.9
[Test/test]   💬  ::debug::check 3.12.0-rc.3 satisfies 3.9
[Test/test]   💬  ::debug::check 3.12.0-rc.2 satisfies 3.9
[Test/test]   💬  ::debug::check 3.12.0-rc.1 satisfies 3.9
[Test/test]   💬  ::debug::check 3.12.0-beta.4 satisfies 3.9
[Test/test]   💬  ::debug::check 3.12.0-beta.3 satisfies 3.9
[Test/test]   💬  ::debug::check 3.12.0-beta.2 satisfies 3.9
[Test/test]   💬  ::debug::check 3.12.0-beta.1 satisfies 3.9

〜〜中略〜〜

[Test/test]   💬  ::debug::check 3.6.13 satisfies 3.9
[Test/test]   💬  ::debug::check 3.6.12 satisfies 3.9
[Test/test]   💬  ::debug::check 3.6.11 satisfies 3.9
[Test/test]   💬  ::debug::check 3.6.10 satisfies 3.9
[Test/test]   💬  ::debug::check 3.6.9 satisfies 3.9
[Test/test]   💬  ::debug::check 3.6.8 satisfies 3.9
[Test/test]   💬  ::debug::check 3.6.7 satisfies 3.9
[Test/test]   💬  ::debug::check 3.5.10 satisfies 3.9
[Test/test]   💬  ::debug::check 3.5.9 satisfies 3.9
[Test/test]   💬  ::debug::check 3.5.4 satisfies 3.9
[Test/test]   💬  ::debug::check 3.4.10 satisfies 3.9
[Test/test]   💬  ::debug::check 3.4.4 satisfies 3.9
[Test/test]   💬  ::debug::check 3.3.7 satisfies 3.9
[Test/test]   💬  ::debug::check 3.3.5 satisfies 3.9
[Test/test]   💬  ::debug::check 3.2.5 satisfies 3.9
[Test/test]   💬  ::debug::check 3.1.4 satisfies 3.9
[Test/test]   💬  ::debug::check 3.0.1 satisfies 3.9
[Test/test]   💬  ::debug::Unable to locate executable file: lsb_release. Please verify either the file path exists or the file can be found within a directory specified by the PATH environment variable. Also check the file mode to verify the file is executable.
[Test/test]   ❗  ::error::The version '3.9' with architecture 'x64' was not found for this operating system.%0AThe list of all available versions can be found here: https://raw.githubusercontent.com/actions/python-versions/main/versions-manifest.json
[Test/test]   ❌  Failure - Main install python
[Test/test] exitcode '1': failure
[Test/test] 🏁  Job failed
```

ん、 `Version 3.9 was not found in the local cache`... python v3.9 not found?
おや、 `architecture 'x64' was not found for this operating` ... MacBook Pro intelチップに
対応していないのか？んな、馬鹿な。

# 解決方法
```shell
act -P ubuntu-20.04=catthehacker/ubuntu:act-20.04 push
```

# 解説
actのランナーがdockerコンテナイメージ `node:16-buster-slim` だったらしく、
これが原因でエラーになっていた。
github actions をローカル環境で動作させる時のコンテナイメージを `catthehacker/ubuntu:act-20.04` に
変更したらうまくいった。

# 補足
明示的にコマンドにオプション `-P <platform>=<docker-image>` を付けて指定しているが、
ここは永続化できる。
```
$ cat ~/.actrc 
-P ubuntu-latest=node:16-buster-slim
-P ubuntu-22.04=node:16-bullseye-slim
-P ubuntu-20.04=node:16-buster-slim
-P ubuntu-18.04=node:16-buster-slim
```
この部分をエディタで開いて書き換えればOK

# 参考
- [cannot find specific version when running with python-setup #1856](https://github.com/nektos/act/issues/1856#issuecomment-1679427871)
- [act - User Guide Runners](https://nektosact.com/usage/runners.html)
- [act/nektosの導入で詰まった点](https://zenn.dev/kawataku/articles/508c0abd50482b)