프로젝트를 진행하면서 코드 스타일, 빌드, 테스트 등의 수정사항이 발생할 때마다 수동으로 테스트를 진행해 왔습니다.
하지만 여러 명의 개발자가 함께 작업하면서, 각자의 설정 차이(IDE 코드 스타일 등)로 인해 잦은 코드 컨플릭트가 발생하는 문제가 있었습니다.
또한, 코드 변경사항이 발생할 때마다 일일이 테스트를 진행하는 것은 시간적 낭비가 크다고 판단하여, GitHub Actions를 활용한 자동화된 테스트 환경을 구축하게 되었습니다.
이 글에서는 GitHub Actions를 활용하여 코드 자동 검사 및 정렬, 빌드 성공 확인, 테스트 코드 자동 검증을 도입하는 방법을 정리합니다.
GitHub Actions를 설정하려면 .github/workflows
디렉토리에 build.yml
파일을 생성하고, 자동화 프로세스를 정의해야 합니다.
.github/workflows/build.yml
파일 생성mkdir -p .github/workflows
touch .github/workflows/build.yml
아래 워크플로우는 코드 정렬(lint), 빌드 검증, 테스트 검증을 자동화합니다.
name: CI
on: [push, pull_request]
jobs:
lint:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [ 21.x ]
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}
- name: Install the project dependencies
run: npm install
- name: Fix code style issues with Prettier & ESLint
run: npm run lint:fix && npm run prettier:fix
- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: Fix styling
test:
environment: development
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [ 21.x ]
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- name: Set up environment variables
run: |
touch .env
echo "APP_API_URL=${{ secrets.APP_API_URL }}" >> .env
echo "NEXTAUTH_URL=${{ secrets.NEXTAUTH_URL }}" >> .env
echo "NEXTAUTH_SECRET=${{ secrets.NEXTAUTH_SECRET }}" >> .env
env:
APP_API_URL: ${{ secrets.APP_API_URL }}
NEXTAUTH_URL: ${{ secrets.NEXTAUTH_URL }}
NEXTAUTH_SECRET: ${{ secrets.NEXTAUTH_SECRET }}
- name: Install the project dependencies
run: npm install
- name: Run tests
run: npm run jest
build:
needs: lint
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [ 21.x ]
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- name: Install the project dependencies
run: npm install
- name: Build the project
run: npm run build
📌 설정 설명
lint
Job)
"Fix styling"
이라는 메시지로 자동 커밋test
Job)
secrets
값을 사용하여 .env
파일을 생성하고, 테스트 코드 실행build
Job)
lint
가 통과된 후, Next.js 빌드를 테스트하여 정상 동작 여부 확인