
GitHub Actionsのデバックを楽にするactコマンド
自動でテストやデプロイを行うとき、Travis CIやCircle CI、Bitriseなどのサービスを利用していることも多いと思います。ローカルでJenkinsを使っているというところもあるかもしれません。
以前はこう言ったサービスを使うことが多かったのですが、GitHubでタスクランナーの機能がGitHub Actionsとして2020年に追加されました。このGitHub Actionsの登場で上記のような外部サービスを利用せずに自動テストや自動デプロイをGitHub上で行うことができるようになりました。
ただ、Cirlce CI等を使っていた時も同様でしたが、ジョブの定義のyamlのデバックのため、何回もcommitして四苦八苦するということが多くあり、その際にCommitのログが汚れてしまうということがありました。
GitHub Actionsの場合は act というコマンドでローカルで動作を確認することができます。ローカルで動作確認を行うことで、一通りの動作確認が終わった後にpushすれば良いのでコミットログも汚れませんし、早くyamlの動作確認を終わらせることができますので、導入や使い方を詳細します。
act導入の注意点
注意点としては仮想環境としてubuntuしかサポートしていないことです。サーバー系の開発であったりAndroidのビルドであれば良いのですが、iOSアプリのビルドを行いたい場合はタスクランナーの環境としてサポートしていないので、yamlのデバックは以前の通りGitHubにpushして動かしみるしかないということになります。
現在サポートしているランナーの環境は下記であるようです。
GitHub Runner | Docker Image |
---|---|
ubuntu-latest | node:12.6-buster-slim |
ubuntu-18.04 | node:12.6-buster-slim |
ubuntu-16.04 | node:12.6-stretch-slim |
windows-latest | unsupported |
windows-2019 | unsupported |
macos-latest | unsupported |
macos-10.15 | unsupported |
actの導入方法
まず、元々ローカル環境にDockerがインストール済みで、Dockerが利用できる状態でないといけないのでDockerを利用できるようにしておきましょう。Dockerが利用できる状態でないと下記のような表示になり実行できないようです。
❯ act
WARN[0000] unable to get git repo: unable to find git repo
WARN[0000] unable to get git revision: unable to find git repo
WARN[0000] unable to get git ref: unable to find git repo
[Node.js CI/build] 🧪 Matrix: map[node-version:12.x]
[Node.js CI/build] 🚀 Start image=node:12.6-buster-slim
WARN[0000] unable to get git repo: unable to find git repo
WARN[0000] unable to get git revision: unable to find git repo
WARN[0000] unable to get git ref: unable to find git repo
Error: unable to determine if image already exists for image "node:12.6-buster-slim": Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
そして、Macの場合は、Homebrewで下記を実行する
brew install act
または
curl https://raw.githubusercontent.com/nektos/act/master/install.sh | sudo bash
を実行します。
Windowsの場合は下記のようですが、私はMacユーザーなので試したことないです。
choco install act-cli
actの実行
基本的には act
とコマンドを実行すれば.github/workflowに設定されているタスクが実行されます。GitHub上にsecretで環境変数を設定している場合 act -s TOKEN=hogehoge
という感じで-sでsecretの情報を設定してあげると良いでしょう。
下記のようなyamlを書いたとします。
name: Node.js CI
on: [push]
env:
TOKEN: ${{ secrets.TOKEN }}
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: echo ${TOKEN}
- run: npm test test/index_test_assert.js
env:
CI: true
実行すると下記のような感じです。(わざとテストで失敗させています)
❯ act -s TOKEN="aaaaaaa"
WARN[0000] unable to get git repo: unable to find git repo
WARN[0000] unable to get git revision: unable to find git repo
WARN[0000] unable to get git ref: unable to find git repo
[Node.js CI/build] 🧪 Matrix: map[node-version:12.x]
[Node.js CI/build] 🚀 Start image=node:12.6-buster-slim
WARN[0000] unable to get git repo: unable to find git repo
WARN[0000] unable to get git revision: unable to find git repo
WARN[0000] unable to get git ref: unable to find git repo
[Node.js CI/build] 🐳 docker run image=node:12.6-buster-slim entrypoint=["/usr/bin/tail" "-f" "/dev/null"] cmd=[]
[Node.js CI/build] 🐳 docker cp src=/Users/eiden/Documents/workspaces/blog/testAsync/. dst=/github/workspace
WARN[0001] unable to get git repo: unable to find git repo
WARN[0001] unable to get git revision: unable to find git repo
WARN[0001] unable to get git ref: unable to find git repo
WARN[0001] unable to get git repo: unable to find git repo
WARN[0001] unable to get git revision: unable to find git repo
WARN[0001] unable to get git ref: unable to find git repo
[Node.js CI/build] ⭐ Run actions/checkout@v2
WARN[0001] unable to get git repo: unable to find git repo
WARN[0001] unable to get git revision: unable to find git repo
WARN[0001] unable to get git ref: unable to find git repo
[Node.js CI/build] ✅ Success - actions/checkout@v2
WARN[0001] unable to get git repo: unable to find git repo
WARN[0001] unable to get git revision: unable to find git repo
WARN[0001] unable to get git ref: unable to find git repo
WARN[0001] unable to get git repo: unable to find git repo
WARN[0001] unable to get git revision: unable to find git repo
WARN[0001] unable to get git ref: unable to find git repo
[Node.js CI/build] ⭐ Run Use Node.js ${{ matrix.node-version }}
[Node.js CI/build] ☁ git clone 'https://github.com/actions/setup-node' # ref=v1
[Node.js CI/build] 🐳 docker cp src=/Users/eiden/.cache/act/actions-setup-node@v1 dst=/actions/
[Node.js CI/build] 💬 ::debug::isExplicit:
[Node.js CI/build] 💬 ::debug::explicit? false
[Node.js CI/build] 💬 ::debug::evaluating 0 versions
[Node.js CI/build] 💬 ::debug::match not found
[Node.js CI/build] 💬 ::debug::evaluating 426 versions
[Node.js CI/build] 💬 ::debug::matched: v12.20.0
[Node.js CI/build] 💬 ::debug::isExplicit: 12.20.0
[Node.js CI/build] 💬 ::debug::explicit? true
[Node.js CI/build] 💬 ::debug::checking cache: /opt/hostedtoolcache/node/12.20.0/x64
[Node.js CI/build] 💬 ::debug::not found
[Node.js CI/build] 💬 ::debug::Downloading https://nodejs.org/dist/v12.20.0/node-v12.20.0-linux-x64.tar.gz
[Node.js CI/build] 💬 ::debug::Destination /tmp/8e875081-e6da-4927-a2b4-e8843bccf0d4
[Node.js CI/build] 💬 ::debug::download complete
[Node.js CI/build] 💬 ::debug::Checking tar --version
[Node.js CI/build] 💬 ::debug::tar (GNU tar) 1.30%0ACopyright (C) 2017 Free Software Foundation, Inc.%0ALicense GPLv3+: GNU GPL version 3 or later .%0AThis is free software: you are free to change and redistribute it.%0AThere is NO WARRANTY, to the extent permitted by law.%0A%0AWritten by John Gilmore and Jay Fenlason.
| [command]/bin/tar xz --warning=no-unknown-keyword -C /tmp/a5f2dd90-e82e-4c4b-9583-e1ba37f8af14 -f /tmp/8e875081-e6da-4927-a2b4-e8843bccf0d4
[Node.js CI/build] 💬 ::debug::Caching tool node 12.20.0 x64
[Node.js CI/build] 💬 ::debug::source dir: /tmp/a5f2dd90-e82e-4c4b-9583-e1ba37f8af14/node-v12.20.0-linux-x64
[Node.js CI/build] 💬 ::debug::destination /opt/hostedtoolcache/node/12.20.0/x64
[Node.js CI/build] 💬 ::debug::finished caching tool
[Node.js CI/build] ⚙ ::add-path:: /opt/hostedtoolcache/node/12.20.0/x64/bin
| [command]/opt/hostedtoolcache/node/12.20.0/x64/bin/node --version
| v12.20.0
| [command]/opt/hostedtoolcache/node/12.20.0/x64/bin/npm --version
| 6.14.8
[Node.js CI/build] ❓ ##[add-matcher]/actions/actions-setup-node@v1/.github/tsc.json
[Node.js CI/build] ❓ ##[add-matcher]/actions/actions-setup-node@v1/.github/eslint-stylish.json
[Node.js CI/build] ❓ ##[add-matcher]/actions/actions-setup-node@v1/.github/eslint-compact.json
[Node.js CI/build] ✅ Success - Use Node.js ${{ matrix.node-version }}
WARN[0010] unable to get git repo: unable to find git repo
WARN[0010] unable to get git revision: unable to find git repo
WARN[0010] unable to get git ref: unable to find git repo
WARN[0010] unable to get git repo: unable to find git repo
WARN[0010] unable to get git revision: unable to find git repo
WARN[0010] unable to get git ref: unable to find git repo
[Node.js CI/build] ⭐ Run npm install
| npm WARN testasync@1.0.0 No repository field.
|
| audited 101 packages in 1.475s
|
| 15 packages are looking for funding
| run `npm fund` for details
|
| found 0 vulnerabilities
|
[Node.js CI/build] ✅ Success - npm install
WARN[0012] unable to get git repo: unable to find git repo
WARN[0012] unable to get git revision: unable to find git repo
WARN[0012] unable to get git ref: unable to find git repo
WARN[0012] unable to get git repo: unable to find git repo
WARN[0012] unable to get git revision: unable to find git repo
WARN[0012] unable to get git ref: unable to find git repo
[Node.js CI/build] ⭐ Run echo ${TOKEN}
| ***
[Node.js CI/build] ✅ Success - echo ${TOKEN}
WARN[0012] unable to get git repo: unable to find git repo
WARN[0012] unable to get git revision: unable to find git repo
WARN[0012] unable to get git ref: unable to find git repo
WARN[0012] unable to get git repo: unable to find git repo
WARN[0012] unable to get git revision: unable to find git repo
WARN[0012] unable to get git ref: unable to find git repo
[Node.js CI/build] ⭐ Run npm test test/index_test_assert.js
|
| > testasync@1.0.0 test /github/workspace
| > node_modules/mocha/bin/mocha "test/index_test_assert.js"
|
|
|
| unit test
| 1) 1 + 2 = 3 test
| ✓ 1 * 2 = 2 test
| ✓ 1 / 2 = 0.5 test
|
|
| 2 passing (9ms)
| 1 failing
|
| 1) unit test
| 1 + 2 = 3 test:
|
| AssertionError [ERR_ASSERTION]: 1 + 2 error !!!
| + expected - actual
|
| -3
| +2
|
| at /github/workspace/test/index_test_assert.js:8:14
| at Object.module.exports.add (index.js:2:3)
| at Context. (test/index_test_assert.js:6:11)
| at processImmediate (internal/timers.js:461:21)
|
|
|
| npm ERR! Test failed. See above for more details.
[Node.js CI/build] ❌ Failure - npm test test/index_test_assert.js
Error: exit with `FAILURE`: 1