Dev

Github actions - Github pages / S3 / NodeJS Jest

foxlee 2022. 2. 5. 23:37

* 그동안 Gituhb actions을 사용했던 예들을 정리해보았다.

1. 리액트 앱  깃헙 페이지에 배포하기 - Github repo

name: github pages

on:
  push:
    branches:
      - client/deploy  # 현재 프로젝트에는 client와 lambda 디렉토리로 나눠져있음. client/deploy 브랜치에 푸쉬하면 실행되고, gh-pages 브랜치는 자동으로 업데이트된다.

jobs:
  deploy:
    runs-on: ubuntu-18.04  # 잡을 실행하는 환경
    env:
      working-directory: ./client # client 안에 리액트 앱이 있고, 아래에서 반복해서 쓸 예정이므로 env 선언
    steps:
      - uses: actions/checkout@v2 # 코드 소스 체크아웃

      - name: Setup Node
        uses: actions/setup-node@v2.1.2 # 사용할 노드 버전 선택
        with:
          node-version: "12.x"

      - name: Cache dependencies # 패키지 캐시 
        uses: actions/cache@v2
        with:
          path: ~/.npm
          key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-node-
      - run: npm ci # 패키지 설치(devDendencies 제외)
        working-directory: ${{ env.working-directory }}

      - run: npm run build
        working-directory: ${{ env.working-directory }}
        env:
          CI: false

      - name: Deploy  # 깃헙 페이지 빌드 기본 설정 
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}  # 인증
          publish_dir: ./${{ env.working-directory }}/build # 위 단계에서 npm build로 빌드된 파일 위치 설정 # gh-pages로 빌드된 파일이 올라감

2. 리액트 앱 S3에 배포 - Github repo

# 위와 동일한 부분은 설명 생략
name: build
on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-18.04
    steps:
      - name: Checkout source code.
        uses: actions/checkout@main

      - name: Cache node modules
        uses: actions/cache@v1
        with:
          path: node_modules
          key: ${{ runner.OS }}-build-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.OS }}-build-
            ${{ runner.OS }}-
      - name: Install Dependencies
        run: npm install

      - name: Build
        run: CI= npm run build

      - name: Deploy # AWS에 접근하기 위한 시크릿 키
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        run: |
          aws s3 cp \
            --recursive \
            --region ap-northeast-2 \
            ./build s3://furfellas-ts.foxlee.kr # s3 버킷 주소 (s3 에 버킷 생성, 정적 웹사이트 호스팅, 퍼미션, 버킷 정책 설정 필요) )

3. 노드 익스프레스 앱 테스트 - Github repo

name: Node.js test

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  test:
    runs-on: ubuntu-latest
    env:
      working-directory: ./server

    steps:
      - uses: actions/checkout@v2

      - name: Setup Node
        uses: actions/setup-node@v2.1.2
        with:
          node-version: "12.x"

      - name: Cache dependencies
        uses: actions/cache@v2
        with:
          path: ~/.npm
          key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-node-
      - run: npm i
        working-directory: ${{ env.working-directory }}
      - run: npm run test
        working-directory: ${{ env.working-directory }}