DevOps/Git

GitHub Action을 이용한 빌드 및 테스트 자동화

WooKGOD 2023. 1. 12. 14:27
반응형

node.js로 작성된 애플리케이션을 테스트 해보고 GitHub Action을 이용해 자동화

  1. 애플리케이션은 node.js로 작성한다. (작성법은 레퍼런스 참고)
  2. npm install을 이용하여 애플리케이션의 의존성을 설치한다. 
  3. 테스트가 통과하는지 npm test를 이용한다.

작성된 node.js 애플리케이션에 따르면 다음과 같은 결과를 얻을 수 있다.

유닛 테스트 예시

이러한 테스트를 수동으로 계속 진행할 경우 다운타임이 발생할 수 있고 비용, 시간적으로도 손해를 볼 수 있다.

그렇다면 자동화를 하는 방법을 알아보자!

 

 

GitHub Action을 이용하여 Node.js CI 적용

  1. node 버전은 16.x 버전으로 사용하는 것을 권장한다. (17.x 이상 버전에서 build시 에러발생 가능)
  2. master brach로 push, pull request를 보냈을 때 GitHub Action이 트리거되도록 설정
  3. npm test 명령어를 CI(GitHub Action) 상에서 자동으로 실행되도록 설정

 

GitHub repo의 Actions 탭에서 workflow 예시들을 참고하여 작성할 수 있다.

Actions탭의 New workflow

 

- Node.js CI의 예시를 참고한 후 작성한 YAML 코드는 다음과 같다.

name: Node.js CI

on:
  push:
    branches: [ "master" ]
  pull_request:
    branches: [ "master" ]

jobs:
  build:

    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [16.x]
        # See supported Node.js release schedule at https://nodejs.org/en/about/releases/

    steps:
    - uses: actions/checkout@v3
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v3
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'npm'
    - run: npm ci
    - run: npm run build --if-present
    - run: npm test

참고 레퍼런스

 

Building and testing Node.js - GitHub Docs

Introduction This guide shows you how to create a continuous integration (CI) workflow that builds and tests Node.js code. If your CI tests pass, you may want to deploy your code or publish a package. Prerequisites We recommend that you have a basic unders

docs.github.com

작성이 끝났다면 Start commit을 눌러주자!

모두 성공했다면 아래와 같은 결과를 볼 수 있을 것이다.

 

반응형