Skip to content

Merge pull request #7 from YAPP-Github/feat/T3-48 #29

Merge pull request #7 from YAPP-Github/feat/T3-48

Merge pull request #7 from YAPP-Github/feat/T3-48 #29

Workflow file for this run

name: Deploy to Amazon ECS
# release, develop 브랜치에 푸시되거나 PR이 닫힐 때마다 실행되는 워크플로우입니다.
on:
push:
branches:
- "release"
- "develop"
jobs:
deploy:
runs-on: ubuntu-latest
steps:
# 1. JDK 17 세팅
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
# 2. 환경변수(yml)가 있는 서브모듈 가져오기
- name: Checkout
uses: actions/checkout@v3
with:
token: ${{ secrets.ACTION_TOKEN }}
submodules: true
# 2-1. 환경변수(yml) 파일이 있는지 확인
- name: Check if config files exist - dev
if: github.ref == 'refs/heads/develop'
run: |
echo "Current Directory: $(pwd)"
cd config
echo "Current Directory: $(pwd)"
cd dev
echo "Current Directory: $(pwd)"
ls -al
- name: Check if config files exist - prod
if: github.ref == 'refs/heads/release'
run: |
echo "Current Directory: $(pwd)"
cd config
echo "Current Directory: $(pwd)"
cd prod
echo "Current Directory: $(pwd)"
ls -al
# 3. Gradle 실행 권한 부여
- name: Grant execute permission for Gradle
run: chmod +x ./gradlew
# 4. Gradle로 빌드
- name: Build with Gradle
run: ./gradlew clean build
# 5. AWS 자격 증명 구성
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ap-northeast-2
# 6. Amazon ECR에 로그인
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
# 7. Docker 이미지 빌드 및 태그, Amazon ECR에 이미지 푸시
- name: Build Docker image and tag, push image to Amazon ECR - release
if: github.ref == 'refs/heads/release'
id: build-image-release
run: |
docker build --platform linux/amd64 -t ${{ secrets.ECR_REPO_NAME_PROD }} .
docker tag ${{ secrets.ECR_REPO_NAME_PROD }}:latest ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.ap-northeast-2.amazonaws.com/${{ secrets.ECR_REPO_NAME_PROD }}:latest # 이미지를 ECR 리포지토리로 태깅합니다.
docker push ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.ap-northeast-2.amazonaws.com/${{ secrets.ECR_REPO_NAME_PROD }}:latest # 이미지를 ECR에 푸시합니다.
echo "image=${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.ap-northeast-2.amazonaws.com/${{ secrets.ECR_REPO_NAME_PROD }}:latest" >> $GITHUB_OUTPUT
- name: Build Docker image and tag, push image to Amazon ECR - develop
if: github.ref == 'refs/heads/develop'
id: build-image-develop
run: |
docker build --platform linux/amd64 -t ${{ secrets.ECR_REPO_NAME_DEV }} .
docker tag ${{ secrets.ECR_REPO_NAME_DEV }}:latest ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.ap-northeast-2.amazonaws.com/${{ secrets.ECR_REPO_NAME_DEV }}:latest # 이미지를 ECR 리포지토리로 태깅합니다.
docker push ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.ap-northeast-2.amazonaws.com/${{ secrets.ECR_REPO_NAME_DEV }}:latest # 이미지를 ECR에 푸시합니다.
echo "image=${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.ap-northeast-2.amazonaws.com/${{ secrets.ECR_REPO_NAME_DEV }}:latest" >> $GITHUB_OUTPUT
# 8. Amazon ECS 태스크 정의에 새 이미지 ID 채우기
- name: Fill in the new image ID in the Amazon ECS task definition - release
if: github.ref == 'refs/heads/release'
id: task-def-release
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: task-definition-prod.json
container-name: ${{ secrets.ECS_CONTAINER_NAME_PROD }}
image: ${{ steps.build-image-release.outputs.image }}
- name: Mask ECS container name secret - release
if: github.ref == 'refs/heads/release'
run: |
echo "ECS_CONTAINER_NAME_PROD is ${{ secrets.ECS_CONTAINER_NAME_PROD }}"
echo "Container Names in task-definition-prod.json:"
cat task-definition-prod.json | jq -r '.containerDefinitions[].name'
- name: Fill in the new image ID in the Amazon ECS task definition - develop
if: github.ref == 'refs/heads/develop'
id: task-def-develop
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: task-definition-dev.json
container-name: ${{ secrets.ECS_CONTAINER_NAME_DEV }}
image: ${{ steps.build-image-develop.outputs.image }}
# 9. ECS에 배포
- name: Deploy to ECS - release
if: github.ref == 'refs/heads/release'
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.task-def-release.outputs.task-definition }} # ECS 태스크 정의 파일을 지정합니다.
service: ${{ secrets.ECS_SERVICE_NAME_PROD }}
cluster: ${{ secrets.ECS_CLUSTER_NAME }}
wait-for-service-stability: true # 서비스가 안정화될 때까지 대기합니다.
- name: Deploy to ECS - develop
if: github.ref == 'refs/heads/develop'
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.task-def-develop.outputs.task-definition }} # ECS 태스크 정의 파일을 지정합니다.
service: ${{ secrets.ECS_SERVICE_NAME_DEV }}
cluster: ${{ secrets.ECS_CLUSTER_NAME }}
wait-for-service-stability: true # 서비스가 안정화될 때까지 대기합니다.