<aside> 💡 yarn 사용을 기준으로 작성되어 있습니다.

</aside>

배포를 위해 Github Actions 스크립트를 준비한다.

<aside> ⚠️ 방금 보니 노드 모듈 캐싱 쪽이 잘못 되어있는 것 같다. 지금 테스트를 해볼 시간이 없어서… 수정되기 전엔 아래 문서를 보고 수정하자.

</aside>

Cache - GitHub Marketplace

Caching Dependencies on GitHub Actions

.github/workflows/develop.yml

name: Deploy Develop to Amazon ECS

on:
  push:
    branches:
      - develop

jobs:
  build:
    name: Deploy Development API Server
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Setup Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '18.17.1'

      - name: Cache node modules
        uses: actions/cache@v3
        with:
          path: node_modules
          key: ${{ runner.os }}-build-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-build-
            ${{ runner.os }}-

      - run: |
          yarn install --frozen-lockfile

      - 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_DEFAULT_REGION }}

      - name: Login to Amazon ECR
        id: login-ecr
        uses: aws-actions/amazon-ecr-login@v1

      - name: Build, tag, and push image to Amazon ECR
        id: build-image
        env:
          ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
          ECR_REPOSITORY: dev-nestjs-guide-api
          IMAGE_TAG: ${{ github.sha }}
        run: |
          docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -f ecs/develop/Dockerfile .
          docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
          echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT

      - name: Fill in the new image ID in the Amazon ECS task definition
        id: task-def
        uses: aws-actions/amazon-ecs-render-task-definition@v1
        with:
          task-definition: ecs/develop/task-definition.json
          container-name: nestjs-guide-api
          image: ${{ steps.build-image.outputs.image }}

      - name: Deploy Amazon ECS task definition
        uses: aws-actions/amazon-ecs-deploy-task-definition@v1
        with:
          task-definition: ${{ steps.task-def.outputs.task-definition }}
          cluster: dev-nestjs-guide
          service: nestjs-guide-api
          wait-for-service-stability: false
          codedeploy-appspec: ecs/develop/appspec.yml
          codedeploy-application: develop-nestjs-guide-api
          codedeploy-deployment-group: develop-nestjs-guide-api

Configure AWS Credentials

secrets

Fill in the new image ID in the Amazon ECS task definition

image

Deploy Amazon ECS task definition

wait-for-service-stability

codedeploy-application

codedeploy-deployment-group