GitHub Actions 사용하여 CI/CD 구현 방법

GitHub Actions

GitHub 리포지토리에서 빌드, 테스트, 배포 등 CI/CD 파이프라인을 자동화할 수 있는 플랫폼입니다.
CI/CD 작업을 수행할 Job과 Step을 정의한 워크플로는 .github/workflows 경로에 .yml 파일로 작성합니다.
push/pull_request 이벤트, 스케줄, 외부 이벤트 등으로 워크플로를 자동 실행할 수 있습니다.
워크플로 작업은 GitHub에서 제공하는 가상 머신 또는 직접 등록한 Runner에서 실행됩니다.

GitHub Actions 실행 방법

name: GitHub Actions Demo # 워크플로명
run-name: $ is testing out GitHub Actions # 실행 이름
on: [push] # push 이벤트 발생 시 실행
jobs:
  Explore-GitHub-Actions: # 작업명
    runs-on: ubuntu-latest # GitHub에서 제공하는 최신 우분투 서버 = 가상 머신 Runner에서 실행
    steps:
      - run: echo "🎉 The job was automatically triggered by a $ event." # echo 명령어를 통해 문자열 출력
      - run: echo "🐧 This job is now running on a $ server hosted by GitHub!"
      - run: echo "🔎 The name of your branch is $ and your repository is $."
      - name: Check out repository code # Step 이름
        uses: actions/checkout@v6 # Runner의 작업 공간에 리포지토리 코드를 체크아웃하는 Action 사용
      - run: echo "💡 The $ repository has been cloned to the runner."
      - run: echo "🖥️ The workflow is now ready to test your code on the runner."
      - name: List files in the repository
        run: |
          ls $
      - run: echo "🍏 This job's status is $."

.github/workflows 경로에 위와 같은 .yml 파일을 작성하고 커밋 후 Push 하면 자동 실행 됩니다.

GitHub Actions 실행 이력 및 결과 확인 방법

GitHub 저장소 > Actions 탭 > 좌측 워크플로 목록에서 워크플로명 선택 > 워크플로 실행 이력 목록에서 실행 이름 클릭 > 워크플로 실행 페이지 왼쪽 Jobs 아래에서 작업명 클릭 또는 우측 yml 파일명 하단에서 작업명 버튼 클릭 > 각 Step 단계별 세부 정보 로그 확인

GitHub Actions 실행 시 자동 빌드 및 테스트 방법

name: Likelion CI Test
run-name: $ is testing out GitHub Actions
on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4 # 리포지토리 체크아웃

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '18' # Node.js 설정 (노드 버전 세팅)

      - name: Install dependencies
        run: npm ci # package-lock.json을 기준으로 node_modules 의존성 패키지 폴더 설치

      - name: Build project
        run: npm run build # 프로젝트 빌드

      - name: Run tests
        run: npm test # 단위테스트 등 package.json에 정의된 test 스크립트 실행

      - run: echo "🍏 This job's status is $."

Push가 발생할 때마다 정상적으로 빌드되고 정의된 테스트가 성공하는지 자동으로 확인할 수 있습니다.