# This workflow will build and push a new container image to Amazon ECR,
# and then will deploy a new task definition to Amazon ECS, when there is a push to the "dev" branch.
#
# To use this workflow, you will need to complete the following set-up steps:
#
# 1. Create an ECR repository to store your images.
# For example: `aws ecr create-repository --repository-name my-ecr-repo --region us-east-2`.
# Replace the value of the `ECR_REPOSITORY` environment variable in the workflow below with your repository's name.
# Replace the value of the `AWS_REGION` environment variable in the workflow below with your repository's region.
#
# 2. Create an ECS task definition, an ECS cluster, and an ECS service.
# For example, follow the Getting Started guide on the ECS console:
# https://us-east-2.console.aws.amazon.com/ecs/home?region=us-east-2#/firstRun
# Replace the value of the `ECS_SERVICE` environment variable in the workflow below with the name you set for the Amazon ECS service.
# Replace the value of the `ECS_CLUSTER` environment variable in the workflow below with the name you set for the cluster.
#
# 3. Store your ECS task definition as a JSON file in your repository.
# The format should follow the output of `aws ecs register-task-definition --generate-cli-skeleton`.
# Replace the value of the `ECS_TASK_DEFINITION` environment variable in the workflow below with the path to the JSON file.
# Replace the value of the `CONTAINER_NAME` environment variable in the workflow below with the name of the container
# in the `containerDefinitions` section of the task definition.
#
# 4. Store an IAM user access key in GitHub Actions secrets named `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`.
# See the documentation for each action used below for the recommended IAM policies for this IAM user,
# and best practices on handling the access key credentials.
name: Deploy to Amazon ECS
on:
push:
branches: [ "dev" ] # dev 브랜치로 push가 될 때(PR이 완료될 때)
env:
AWS_REGION: ap-northeast-2 # set this to your preferred AWS region, e.g. us-west-1
permissions:
contents: read
jobs:
build:
name: Build Jar
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4 # 기존 정의되어 있는 actions를 사용
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
- name: Setup Gradle
uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0
deploy:
needs: build
name: Deploy
runs-on: ubuntu-latest
environment: production
steps:
- name: Checkout
uses: actions/checkout@v4
- 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 dock-compose.yml
run: docker compose build # docker compose로 build하여 image 생성
- name: Build, tag, and push image to Amazon ECR
id: eureka-server-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} # 이전 step 중 id가 login-ecr인 step의 결과(outputs) 중 registry를 사용
IMAGE_TAG: ${{ github.sha }}
run: |
docker tag docker.io/library/eureka-server:latest ${{ env.ECR_REGISTRY }}/hobbing/eureka-server:${{ env.IMAGE_TAG }}
docker push ${{ env.ECR_REGISTRY }}/hobbing/eureka-server:${{ env.IMAGE_TAG }}
echo "image=${{ env.ECR_REGISTRY }}/hobbing/eureka-server:${{ env.IMAGE_TAG }}" >> $GITHUB_OUTPUT
- name: Build, tag, and push image to Amazon ECR
id: proxy-gateway-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: ${{ github.sha }}
run: |
docker tag docker.io/library/proxy-gateway:latest ${{ env.ECR_REGISTRY }}/hobbing/proxy-gateway:${{ env.IMAGE_TAG }}
docker push ${{ env.ECR_REGISTRY }}/hobbing/proxy-gateway:${{ env.IMAGE_TAG }}
echo "image=${{ env.ECR_REGISTRY }}/hobbing/proxy-gateway:${{ env.IMAGE_TAG }}" >> $GITHUB_OUTPUT # image 변수를 $GITHUB_OUTPUT으로 추가
# - name: Build, tag, and push image to Amazon ECR
# id: gateway-image
# env:
# ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
# IMAGE_TAG: ${{ github.sha }}
# run: |
# docker tag docker.io/library/gateway:latest ${{ env.ECR_REGISTRY }}/hobbing/gateway:${{ env.IMAGE_TAG }}
# docker push ${{ env.ECR_REGISTRY }}/hobbing/gateway:${{ env.IMAGE_TAG }}
# echo "image=${{ env.ECR_REGISTRY }}/hobbing/gateway:${{ env.IMAGE_TAG }}" >> $GITHUB_OUTPUT
# - name: Build, tag, and push image to Amazon ECR
# id: user-image
# env:
# ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
# IMAGE_TAG: ${{ github.sha }}
# run: |
# docker tag docker.io/library/user:latest ${{ env.ECR_REGISTRY }}/hobbing/user:${{ env.IMAGE_TAG }}
# docker push ${{ env.ECR_REGISTRY }}/hobbing/user:${{ env.IMAGE_TAG }}
# echo "image=${{ env.ECR_REGISTRY }}/hobbing/user:${{ env.IMAGE_TAG }}" >> $GITHUB_OUTPUT
# - name: Build, tag, and push image to Amazon ECR
# id: coupon-image
# env:
# ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
# IMAGE_TAG: ${{ github.sha }}
# run: |
# docker tag docker.io/library/coupon:latest ${{ env.ECR_REGISTRY }}/hobbing/coupon:${{ env.IMAGE_TAG }}
# docker push ${{ env.ECR_REGISTRY }}/hobbing/coupon:${{ env.IMAGE_TAG }}
# echo "image=${{ env.ECR_REGISTRY }}/hobbing/coupon:${{ env.IMAGE_TAG }}" >> $GITHUB_OUTPUT
# - name: Build, tag, and push image to Amazon ECR
# id: lecture-image
# env:
# ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
# IMAGE_TAG: ${{ github.sha }}
# run: |
# docker tag docker.io/library/lecture:latest ${{ env.ECR_REGISTRY }}/hobbing/lecture:${{ env.IMAGE_TAG }}
# docker push ${{ env.ECR_REGISTRY }}/hobbing/lecture:${{ env.IMAGE_TAG }}
# echo "image=${{ env.ECR_REGISTRY }}/hobbing/lecture:${{ env.IMAGE_TAG }}" >> $GITHUB_OUTPUT
# - name: Build, tag, and push image to Amazon ECR
# id: reservation-image
# env:
# ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
# IMAGE_TAG: ${{ github.sha }}
# run: |
# docker tag docker.io/library/reservation-pay:latest ${{ env.ECR_REGISTRY }}/hobbing/reservation:${{ env.IMAGE_TAG }}
# docker push ${{ env.ECR_REGISTRY }}/hobbing/reservation:${{ env.IMAGE_TAG }}
# echo "image=${{ env.ECR_REGISTRY }}/hobbing/reservation:${{ env.IMAGE_TAG }}" >> $GITHUB_OUTPUT
- name: Fill in the new image ID in the Amazon ECS task definition
id: eureka-server-task-def
env:
ECS_TASK_DEFINITION: .github/workflows/hobbing-eureka-server-task-revision1.json # set this to the path to your Amazon ECS task definition # file, e.g. .aws/task-definition.json
CONTAINER_NAME: hobbing-eureka-server-container-01 # set this to the name of the container in the
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: ${{ env.ECS_TASK_DEFINITION }}
container-name: ${{ env.CONTAINER_NAME }}
image: ${{ steps.eureka-server-image.outputs.image }}
- name: Fill in the new image ID in the Amazon ECS task definition
id: proxy-gateway-task-def
env:
ECS_TASK_DEFINITION: .github/workflows/hobbing-proxy-gateway-task-revision1.json # set this to the path to your Amazon ECS task definition # file, e.g. .aws/task-definition.json
CONTAINER_NAME: hobbing-proxy-server-container # set this to the name of the container in the
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: ${{ env.ECS_TASK_DEFINITION }}
container-name: ${{ env.CONTAINER_NAME }}
image: ${{ steps.proxy-gateway-image.outputs.image }}
# - name: Fill in the new image ID in the Amazon ECS task definition
# id: gateway-task-def
# env:
# ECS_TASK_DEFINITION: .github/workflows/hobbing-gateway-task-revision1.json # set this to the path to your Amazon ECS task definition # file, e.g. .aws/task-definition.json
# CONTAINER_NAME: hobbing-gateway-server-container # set this to the name of the container in the
# uses: aws-actions/amazon-ecs-render-task-definition@v1
# with:
# task-definition: ${{ env.ECS_TASK_DEFINITION }}
# container-name: ${{ env.CONTAINER_NAME }}
# image: ${{ steps.gateway-image.outputs.image }}
# - name: Fill in the new image ID in the Amazon ECS task definition
# id: user-redis-task-def
# env:
# ECS_TASK_DEFINITION: .github/workflows/hobbing-user-task-revision1.json # set this to the path to your Amazon ECS task definition # file, e.g. .aws/task-definition.json
# CONTAINER_NAME: hobbing-user-redis-container # set this to the name of the container in the
# uses: aws-actions/amazon-ecs-render-task-definition@v1
# with:
# task-definition: ${{ env.ECS_TASK_DEFINITION }}
# container-name: ${{ env.CONTAINER_NAME }}
# image: ${{ steps.login-ecr.outputs.registry }}/hobbing/redis-images:latest
# - name: Fill in the new image ID in the Amazon ECS task definition
# id: user-task-def
# env:
# ECS_TASK_DEFINITION: .github/workflows/hobbing-user-task-revision1.json # set this to the path to your Amazon ECS task definition # file, e.g. .aws/task-definition.json
# CONTAINER_NAME: hobbing-user-service-container # set this to the name of the container in the
# uses: aws-actions/amazon-ecs-render-task-definition@v1
# with:
# task-definition: ${{ env.ECS_TASK_DEFINITION }}
# container-name: ${{ env.CONTAINER_NAME }}
# image: ${{ steps.user-image.outputs.image }}
# - name: Fill in the new image ID in the Amazon ECS task definition
# id: coupon-task-def
# env:
# ECS_TASK_DEFINITION: .github/workflows/hobbing-coupon-task-revision1.json # set this to the path to your Amazon ECS task definition # file, e.g. .aws/task-definition.json
# CONTAINER_NAME: hobbing-coupon-server-container # set this to the name of the container in the
# uses: aws-actions/amazon-ecs-render-task-definition@v1
# with:
# task-definition: ${{ env.ECS_TASK_DEFINITION }}
# container-name: ${{ env.CONTAINER_NAME }}
# image: ${{ steps.coupon-image.outputs.image }}
# - name: Fill in the new image ID in the Amazon ECS task definition
# id: lecture-task-def
# env:
# ECS_TASK_DEFINITION: .github/workflows/hobbing-lecture-task-revision1.json # set this to the path to your Amazon ECS task definition # file, e.g. .aws/task-definition.json
# CONTAINER_NAME: hobbing-lecture-server-container-01 # set this to the name of the container in the
# uses: aws-actions/amazon-ecs-render-task-definition@v1
# with:
# task-definition: ${{ env.ECS_TASK_DEFINITION }}
# container-name: ${{ env.CONTAINER_NAME }}
# image: ${{ steps.lecture-image.outputs.image }}
# - name: Fill in the new image ID in the Amazon ECS task definition
# id: reservation-task-def
# env:
# ECS_TASK_DEFINITION: .github/workflows/hobbing-reservation-task-revision1.json # set this to the path to your Amazon ECS task definition # file, e.g. .aws/task-definition.json
# CONTAINER_NAME: hobbing-reservation-server-container # set this to the name of the container in the
# uses: aws-actions/amazon-ecs-render-task-definition@v1
# with:
# task-definition: ${{ env.ECS_TASK_DEFINITION }}
# container-name: ${{ env.CONTAINER_NAME }}
# image: ${{ steps.reservation-image.outputs.image }}
- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
env:
ECS_SERVICE: hobbing-eureka-service-03 # set this to your Amazon ECS service name
ECS_CLUSTER: hobbing-cluster-01 # set this to your Amazon ECS cluster name
with:
task-definition: ${{ steps.eureka-server-task-def.outputs.task-definition }}
service: ${{ env.ECS_SERVICE }}
cluster: ${{ env.ECS_CLUSTER }}
wait-for-service-stability: false
- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
env:
ECS_SERVICE: hobbing-proxy-service-03 # set this to your Amazon ECS service name
ECS_CLUSTER: hobbing-cluster-01 # set this to your Amazon ECS cluster name
with:
task-definition: ${{ steps.proxy-gateway-task-def.outputs.task-definition }}
service: ${{ env.ECS_SERVICE }}
cluster: ${{ env.ECS_CLUSTER }}
wait-for-service-stability: false
# - name: Deploy Amazon ECS task definition
# uses: aws-actions/amazon-ecs-deploy-task-definition@v1
# env:
# ECS_SERVICE: hobbing-gateway-service-04 # set this to your Amazon ECS service name
# ECS_CLUSTER: hobbing-cluster-01 # set this to your Amazon ECS cluster name
# with:
# task-definition: ${{ steps.gateway-task-def.outputs.task-definition }}
# service: ${{ env.ECS_SERVICE }}
# cluster: ${{ env.ECS_CLUSTER }}
# wait-for-service-stability: false
# - name: Deploy Amazon ECS task definition
# uses: aws-actions/amazon-ecs-deploy-task-definition@v1
# env:
# ECS_SERVICE: hobbing-user-service-03 # set this to your Amazon ECS service name
# ECS_CLUSTER: hobbing-cluster-01 # set this to your Amazon ECS cluster name
# with:
# task-definition: ${{ steps.user-task-def.outputs.task-definition }}
# service: ${{ env.ECS_SERVICE }}
# cluster: ${{ env.ECS_CLUSTER }}
# wait-for-service-stability: false
# - name: Deploy Amazon ECS task definition
# uses: aws-actions/amazon-ecs-deploy-task-definition@v1
# env:
# ECS_SERVICE: hobbing-coupon-service-04 # set this to your Amazon ECS service name
# ECS_CLUSTER: hobbing-cluster-01 # set this to your Amazon ECS cluster name
# with:
# task-definition: ${{ steps.coupon-task-def.outputs.task-definition }}
# service: ${{ env.ECS_SERVICE }}
# cluster: ${{ env.ECS_CLUSTER }}
# wait-for-service-stability: false
# - name: Deploy Amazon ECS task definition
# uses: aws-actions/amazon-ecs-deploy-task-definition@v1
# env:
# ECS_SERVICE: hobbing-lecture-service-03 # set this to your Amazon ECS service name
# ECS_CLUSTER: hobbing-cluster-01 # set this to your Amazon ECS cluster name
# with:
# task-definition: ${{ steps.lecture-task-def.outputs.task-definition }}
# service: ${{ env.ECS_SERVICE }}
# cluster: ${{ env.ECS_CLUSTER }}
# wait-for-service-stability: false
# - name: Deploy Amazon ECS task definition
# uses: aws-actions/amazon-ecs-deploy-task-definition@v1
# env:
# ECS_SERVICE: hobbing-reservation-service-03 # set this to your Amazon ECS service name
# ECS_CLUSTER: hobbing-cluster-01 # set this to your Amazon ECS cluster name
# with:
# task-definition: ${{ steps.reservation-task-def.outputs.task-definition }}
# service: ${{ env.ECS_SERVICE }}
# cluster: ${{ env.ECS_CLUSTER }}
# wait-for-service-stability: false