배포를 자동화할 ECS 작업 테스크의 JSON파일을 가져온다.
task-definition.json 파일을 프로젝트 폴더안으로 옮긴다.
나는 프로젝트폴더 최상위 폴더에 aws폴더를 만들어서 넣어줬다.
깃액션 yml 작성
프로젝트 폴더 최상위에 /github/workflows/deploy-ecr.yml 파일을 만들어준다.
env에 설정된 값은 다음과 같다.
- AWS_REGION: ap-northeast-2
- 사용하고 있는 aws region이다.
- ECR_REGISTRY: 725601756882.dkr.ecr.ap-northeast-2.amazonaws.com
- ecr의 registry이다 이미지의 앞부분
- ECR_REPOSITORY: mildstone-backend
- ecr의 repository이다 이미지의 뒷부분
- ECS_SERVICE: fastify-sv
- 배포할 서비스의 이름
- ECS_CLUSTER: mongo-cluster
- 배포할 클러스터이름
- ECS_TASK_DEFINITION: ./aws/task-definition.json
- 사용할 task-definition의 위치
- CONTAINER_NAME: fastify
- task-definition.json에서 containerDefinitions -> name을 가져 온다.
name: Deploy to Amazon ECR
on:
push:
branches:
- main
pull_request:
branches:
- main
env:
AWS_REGION: ap-northeast-2
ECR_REGISTRY: 725601756882.dkr.ecr.ap-northeast-2.amazonaws.com
ECR_REPOSITORY: mildstone-backend
ECS_SERVICE: fastify-sv
ECS_CLUSTER: mongo-cluster
ECS_TASK_DEFINITION: ./aws/task-definition.json
CONTAINER_NAME: fastify
jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
environment: production
steps:
- name: Checkout
uses: actions/checkout@v2
- 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 }}
working-directory: ./backend-was
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 .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
- 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
'DevOps BootCamp > Section 2 실습과제' 카테고리의 다른 글
Git Action-S3 배포 (0) | 2023.05.03 |
---|---|
프로젝트 2 회고 (0) | 2023.05.02 |