본문 바로가기
Research/CICD

Github 액션 관련 요약

by RIEM 2023. 12. 9.

65

출처

깃허브 관련 개념

배경

  • 과거 : Jenkins, Buildkite, circleci 등 외부 툴을 사용했어야 했다
  • 2018년 이후 : GitHub Action을 많이 사용

핵심 개념

  • Event : push, merge, issue open 등 어떤 이벤트가 발생했는지 감지
  • Workflows : 이벤트 발생 시 어떤 일들을 수행할 것인가에 대한 수행 목록
    • Jobs : 워크플로우를 구성하는 일들의 유닛. 병렬적으로 수행. 순차적으로도 가능. 개별 steps들로 구성.
  • Actions : 공유하여 재사용 가능한 actions들. node 환경 셋팅을 직접할 필요없이 관련 action을 가져다 쓰면 됨
  • Runners : VM머신, Docker 컨테이너와 같음. 각 Job은 개별 Runner에서 수행

파일 디렉터리

  • .github/workflows/workflow.yml

소스코드

Screen Shot 2023-02-15 at 1 11 00 AM

index.test.js

const factorial = require('../src');

describe('Factorial function', () => {
  it('correctly computes factorial of a positive integer number', () => {
    expect(factorial(3)).toEqual(6);
  });

  it('throws an error if a negative input is provided', () => {
    expect(() => {
      factorial(-1);
    }).toThrow('Factorial is only defined for non-negative integers!');
  });
});

index.js

// Computes a factorial of a given positive integer
function factorial(n) {
  if (n < 0) {
    throw new Error('Factorial is only defined for non-negative integers!');
  }

  if (n === 0) {
    return 1;
  }
  return n * factorial(n - 1);
}

module.exports = factorial;

github action 설정

Screen Shot 2023-02-15 at 1 14 40 AM
nodejs action 선택하기

# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs

name: Example CI

on:
  push:
    branches: [ "main" ] # main 브랜치에 push 이벤트 발생할 때마다
  pull_request:
    branches: [ "main" ]

jobs:
  test:

    runs-on: ubuntu-latest #ubuntu 최신 버전

#     strategy:
#       matrix: # 각각 다른 버전에서 접속하여 테스트
#         node-version: [14.x, 16.x, 18.x]
        # See supported Node.js release schedule at https://nodejs.org/en/about/releases/

    steps:
    - uses: actions/checkout@v3
    - name: Setup node.js 14.x
      uses: actions/setup-node@v3
      with:
        node-version: 14.x
        cache: 'npm'
    - run: npm ci
    - run: npm test

YML 파일을 이렇게 작성해준다. 파일명은 ci.yml로 했다. 그 다음 커밋을 해준다.

Screen Shot 2023-02-15 at 1 23 10 AM
잘 작동한다!

// Computes a factorial of a given positive integer
function factorial(n) {
  // if (n < 0) {
  //   throw new Error('Factorial is only defined for non-negative integers!');
  // }

  if (n === 0) {
    return 1;
  }
  return n * factorial(n - 1);
}

module.exports = factorial;

index.js

  • 의도적으로 factorial 테스트 코드에 에러가 나도록 코드를 수정해주자.

Screen Shot 2023-02-15 at 1 27 24 AM
의도적으로 테스트 코드가 에러가 나도록 만든 뒤 푸시를 하면 github action에서도 에러가 발생한다! 놀랍다.

다른 브랜치에서 다시 push 하기

break-test 브랜치를 따로 만들어서 문제가 발생하는 코드를 의도적으로 넣고 push 해보자. push 뒤에 PR을 하면 이렇게 된다.

Screen Shot 2023-02-15 at 1 30 09 AM
PR한 것을 테스트해주는데...

Screen Shot 2023-02-15 at 1 31 47 AM
그렇게 테스트하다가 테스트 통과하지 못하여 에러가 발생하는 것을 볼 수 있다. 이렇게 되면 PR한 사람이 문제가 발생한 것을 알게 된다.

'Research > CICD' 카테고리의 다른 글

CICD_github action-s3-codedeploy로 CICD 구축하기  (0) 2023.03.10
CICD_Jenkins vs Github Action  (0) 2023.02.15
깃허브 액션 관련 영상 요약  (0) 2023.02.15

댓글