[AWS 서버 구성] ECS로 서버 구동 및 CI/CD 환경 구성하기 (4 - CI/CD)

steve·2023년 12월 6일
0

Cloud

목록 보기
5/6
post-thumbnail

목표

  • 생성한 AWS 서비스들을 활용하여 GitHub Action에 연동 후 CI/CD 환경을 구성한다

1. task-definition 파일 저장

  • 생성한 task definition 설정 화면에서,
    • 생성된 task definition에서 JSON 탭으로 이동 후 json 값을 복사

  • GitHub에 연결된 프로젝트 루트 경로에 파일 생성 후, 붙여넣기

2. GitHub action 파일 설정

  • 지금까지 생성한 AWS의 서비스들의 이름을 참고하여, GitHub action 파일의 양식에 알맞은 이름으로 수정
    • 예시
      name: Deploy to Amazon ECS
      
      on:
        push:
          branches: [ "main" ]
      
      env:
        AWS_REGION: "ap-northeast-2"                   # set this to your preferred AWS region, e.g. us-west-1
        ECR_REPOSITORY: "stv-ecr"                       # set this to your Amazon ECR repository name
        ECS_SERVICE: "stv-ecs-service"               # set this to your Amazon ECS service name
        ECS_CLUSTER: "stv-ecs-cluster"               # set this to your Amazon ECS cluster name
        ECS_TASK_DEFINITION: "./task-definition.json"  # set this to the path to your Amazon ECS task definition
                                                       # file, e.g. .aws/task-definition.json
        CONTAINER_NAME: "my-ecs-container"                       # set this to the name of the container in the
                                                       # containerDefinitions section of your task definition
      
      permissions:
        contents: read
      
      jobs:
        deploy:
          name: Deploy
          runs-on: ubuntu-latest
          environment: production
      
          steps:
          - name: Checkout
            uses: actions/checkout@v3
      
          - 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: ${{ env.AWS_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 }}
              IMAGE_TAG: ${{ github.sha }}
              NODE_ENV: dev
            run: |
              # Build a docker container and
              # push it to ECR so that it can
              # be deployed to ECS.
              docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . --build-arg NODE_ENV=$NODE_ENV
              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: ${{ env.ECS_TASK_DEFINITION }}
              container-name: ${{ env.CONTAINER_NAME }}
              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 }}
              service: ${{ env.ECS_SERVICE }}
              cluster: ${{ env.ECS_CLUSTER }}
              wait-for-service-stability: true

3. CI/CD 테스트

  • GitHub repository 프로젝트의 main 브랜치에 push 수행
  • 정상적으로 배포가 완료됨

0개의 댓글