글을 작성하게 된 계기
로컬에서 Github Action을 실행할 수 있는 방법을 알게 되었고, 이를 정리하기 위해 글을 작성하게 되었습니다.
1. Act
act는 로컬 환경에서 GitHub Actions 워크플로우를 실행할 수 있도록 도와주는 도구 입니다.
CI/CD를 구성할 때, 기본적으로 코드를 푸시한 후, 스크립트를 실행할 수 있는데, 로컬에서 Docker 컨테이너 기반으로 미리 테스트한 후 바로 적용할 수 있어 개발 속도 와 안정성 을 크게 향상시킬 수 있죠. 이전 프로젝트에서 무수히 많은 실패의 흔적들이 떠올랐는데요, 더 이상 이렇게 고생하지 않아도 되니 너무 좋습니다. 🚀
1-1. 설치
설치와 사용법도 정말 간단한데요, 이를 살펴보겠습니다. 먼저 Homebrew를 통해 act를 설치합니다.
1
2
# act 설치
$ brew install act
1-2. 사용법
act는 기본적으로 .github/workflows 폴더 내부에서 실행 되는데, 별도의 테스트 폴더를 하나 만들고 로컬에서 테스트하면 정말 편합니다. 다음과 같이요. 기존 CI/CD 파이프라인을 그대로 테스트 폴더로 복사한 후, 실행하는 것이죠.
예를 들어, 다음과 같이 폴더가 구성 돼 있다면 act push -W .github/workflows/act/retly-app-ci.yml 명령어를 통해 특정 폴더 내의 워크플로우를 실행할 수 있습니다.
1
2
3
4
└── .github/
└── workflows/
└── act/
└── retly-app-ci.yml
간단한 CI 스크립트를 작성한 후, 이를 실행하면 다음과 같이 Github Action이 로컬에서 실행되는 것을 볼 수 있습니다.
1
2
3
4
5
6
7
8
9
name: Retly App CI
on: [push]
jobs:
say-hello:
runs-on: ubuntu-latest
steps:
- name: Print hello
run: echo "👋 Hello from act!"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ act push -W .github/workflows/retly-app-ci.yml --container-architecture linux/arm64
INFO[0000] Using docker host 'unix:///var/run/docker.sock', and daemon socket 'unix:///var/run/docker.sock'
[Retly App CI/say-hello] ⭐ Run Set up job
[Retly App CI/say-hello] 🚀 Start image=catthehacker/ubuntu:act-latest
[Retly App CI/say-hello] 🐳 docker pull image=catthehacker/ubuntu:act-latest platform=linux/arm64 username= forcePull=true
[Retly App CI/say-hello] 🐳 docker create image=catthehacker/ubuntu:act-latest platform=linux/arm64 entrypoint=["tail" "-f" "/dev/null"] cmd=[] network="host"
[Retly App CI/say-hello] 🐳 docker run image=catthehacker/ubuntu:act-latest platform=linux/arm64 entrypoint=["tail" "-f" "/dev/null"] cmd=[] network="host"
[Retly App CI/say-hello] 🐳 docker exec cmd=[node --no-warnings -e console.log(process.execPath)] user= workdir=
[Retly App CI/say-hello] ✅ Success - Set up job
[Retly App CI/say-hello] ⭐ Run Main Print hello
[Retly App CI/say-hello] 🐳 docker exec cmd=[bash -e /var/run/act/workflow/0] user= workdir=
| 👋 Hello from act!
[Retly App CI/say-hello] ✅ Success - Main Print hello [54.860791ms]
[Retly App CI/say-hello] ⭐ Run Complete job
[Retly App CI/say-hello] Cleaning up container for job say-hello
[Retly App CI/say-hello] ✅ Success - Complete job
[Retly App CI/say-hello] 🏁 Job succeeded
또는 다음과 같은 명령어를 통해 특정 이벤트를 지정해 실행할 수도 있습니다.
1
2
$ act pull_request -W .github/workflows/act/retly-app-ci.yml
$ act workflow_dispatch -W .github/workflows/act/retly-app-ci.yml
즉, Github Action 스크립트 이벤트를 지정해, 로컬에서 테스트할 수 있는 것입니다. on에다가 조건만 넣으면 되죠.
1
2
3
4
5
name: Retly App CI
on: [push]
......
2. 정리
별 내용은 없는데요, 로컬에서 GitHub Action을 실행할 수 있어 정말 다양한 테스트를 할 수 있게 되었습니다. 앞으로는 CI/CD 파이프라인을 구축할 때 act를 적극 활용할 예정입니다. 👋🏻