LoginSignup
11
8

GitHub Actionsをローカルでテストする方法を理解する

Last updated at Posted at 2023-09-24

目次

  1. 背景
  2. homebrewのinstall
  3. actのinstall
  4. actで色々動かしてみる

1. 背景

こちらの検証でgithub-actionsのworkflowファイルを作る必要が出た為です。

こちらに従って進めます。
https://github.com/nektos/act#act-user-guide

2. homebrewのinstall

https://brew.sh/
以下をコピーするように言われますので、コピーして実行します。

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

以下のような警告が表示されました。

Warning: /opt/homebrew/bin is not in your PATH.
  Instructions on how to configure your shell for Homebrew
  can be found in the 'Next steps' section below.
==> Installation successful!

vi ~/.zshrcで.zshrcを開いて
以下を貼り付けます。

.zshrc
export PATH="/usr/local/bin:$PATH"
source ~/.zshrc
brew -v
Homebrew 4.1.12

これでhomebrewが使えます。

3. actのinstall

brew install act

4. actで色々動かしてみる

すべてのアクションをリストアップ

act -l
WARN  ⚠ You are using Apple M-series chip and you have not specified container architecture, you might encounter issues while running act. If so, try running it with '--container-architecture linux/amd64'. ⚠ 

警告が出ました。
container-architectureを指定しないといけないそうです。

act -l --container-architecture linux/amd64
Stage  Job ID  Job name  Workflow name  Workflow file  Events           
0      build   build     Test CI        test.yml       push,pull_request

これで正常にコマンド実行できました。

imageサイズを聞かれましたのでMediumにしました。

~/Desktop/github.com/yamatai12/jest-lab(main) $ act  --container-architecture -j build  
? 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) Medium

Docker daemonに接続できないというエラーが出ました。

~/Desktop/github.com/yamatai12/jest-lab(main) $ act pull_request --container-architecture linux/amd64
[Test CI/build] 🚀  Start image=catthehacker/ubuntu:act-latest
[Test CI/build]   🐳  docker pull image=catthehacker/ubuntu:act-latest platform=linux/amd64 username= forcePull=true
Error: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

DockerDesktopを起動したら上のコマンドを実行できました。

vオプションをつけるとログが表示されます。

act pull_request -v --container-architecture linux/amd64
~~~~
[Test CI/build] [DEBUG] Writing entry to tarball workflow/3 len:14
[Test CI/build] [DEBUG] Extracting content to '/var/run/act'
[Test CI/build]   🐳  docker exec cmd=[bash --noprofile --norc -e -o pipefail /var/run/act/workflow/3] user= workdir=
[Test CI/build] [DEBUG] Exec command '[bash --noprofile --norc -e -o pipefail /var/run/act/workflow/3]'
[Test CI/build] [DEBUG] Working directory '/Users/yamanetaisei/Desktop/github.com/yamatai12/jest-lab'
| 
| > jest-lab@1.0.0 test
| > jest
| 
|  PASS  tests/processNumbers1.test.ts (13.534 s)
|  PASS  tests/processNumbers2.test.ts (13.825 s)
|  PASS  tests/processNumbers3.test.ts (13.763 s)
|  PASS  tests/test3.test.ts
|  PASS  tests/test2.test.ts
|  PASS  tests/test1.test.ts
|  PASS  tests/fetchData3.test.ts (16.532 s)
|  PASS  tests/fetchData1.test.ts (5.003 s)
|  PASS  tests/fetchData2.test.ts (5.252 s)

| Test Suites: 9 passed, 9 total
| Tests:       15 passed, 15 total
| Snapshots:   0 total
| Time:        20.65 s
| Ran all test suites.
[Test CI/build]   ✅  Success - Main Run Jest tests
~~~~~~~

参考

11
8
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
11
8