[AWS] Elastic Beanstalk에 github actions로 next.js 배포하기(EB 에서 yarn 사용)

Ash·2022년 5월 2일
1

Today YJ Learned

목록 보기
10/10

선행사항

  1. Elastic beanstalk에서 기본적으로 npm을 사용하기 때문에 yarn을 사용하는경우 prebuild hook에서 yarn을 사용하도록 설정해주어야합니다.

  2. 로컬환경에 aws cli 가 설치되어있어야합니다.

  3. aws console에서 어플리케이션, 환경을 구축한 후 deploy를 진행하는 것이 수월합니다.

    (EC2 용량은 t3.small 이상 사용 권장)

구성 및 코드


(AWS Linux 2 버전 기준으로 작성)

  1. .platform/hooks/prebuild/yarn.sh 파일 생성

    .platform/hooks/prebuild/*.sh 을 모두 실행하므로 파일명은 자유롭게 생성해주세요.

#!/bin/bash

# install node
curl --silent --location https://rpm.nodesource.com/setup_16.x | bash -;

# install yarn
curl --silent --location https://dl.yarnpkg.com/rpm/yarn.repo | tee /etc/yum.repos.d/yarn.repo;
yum -y install yarn;

# install node_modules with yarn
app="$(/opt/elasticbeanstalk/bin/get-config container -k app_staging_dir)";
cd "${app}";
echo "Inside ${app}, about to run yarn."
yarn --production;
  1. yarn.sh 파일에 권한 부여
$ chmod +x .platform/hooks/prebuild/yarn.sh
  1. Elastic Beanstalk 애플리케이션 기본 값 설정 (사전에 aws cli 설치 필요)
$ eb init

Select a default region
$10 // seoul region 선택

Select an application to use
...
aws console에서 미리 생성한 application 선택)

Enter Application Name
$test-app

It appears you are using Node.js. Is this correct?
(Y/n): 
$Y

Select a platform branch.
1) Node.js 16 running on 64bit Amazon Linux 2
2) Node.js 14 running on 64bit Amazon Linux 2
3) Node.js 12 running on 64bit Amazon Linux 2 (Deprecated)
4) Node.js 10 running on 64bit Amazon Linux 2 (Deprecated)
5) Node.js running on 64bit Amazon Linux (Deprecated)
$ 1

Do you wish to continue with CodeCommit? (Y/n)
$ n

Do you want to set up SSH for your instances?
$ n
  1. 파일 압축 업로드 or github actions 등록
  • aws console에 파일 압축 업로드 및 배포
    1. 파일 압축

      rm -rf node_modules
      zip ./deploy.zip -r * .[^.]*
    2. aws console 접속 후 “업로드 및 배포" 클릭하여 deploy.zip 업로드

  • github actions *.yml
bash
name: Deploy to EB prod

on:
  push:
    branches:
      - main

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: ['16.x']

    steps:
      - name: Checkout source code.
        uses: actions/checkout@v3

      - name: Set up Node.js
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
          cache: 'npm'
          
      - name: Install dependencies
        run: yarn install

      - name: Run build
        run: yarn build:production          

      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: ${{ secrets.AWS_REGION }}

##       - name: Current timestamp
        id: timestamp
        run: echo "::set-output name=date::$(date +'%Y-%m-%dT%H-%M-%S-%3NZ')"

      - name: remove node_modules
        run: rm -rf node_modules
        
      - name: Generate deployment package # (1)
        run: zip ./deploy.zip -r * .[^.]*
          
      - name: Deploy to EB
        uses: einaregilsson/beanstalk-deploy@v20
        with:
          aws_access_key: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws_secret_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          region: ${{ secrets.AWS_REGION }}
          application_name: test-app
          environment_name: testApp-env
          version_label: '${{ steps.timestamp.outputs.date }}'
          deployment_package: deploy.zip
profile
기록남기기👩‍💻

0개의 댓글