[FIX] AWS CICD 수정 #7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: rhkr8521 CICD to EU Region | |
| # main 브랜치에 push 될 때마다 실행 | |
| on: | |
| push: | |
| branches: | |
| - main | |
| # 공통 환경 변수 | |
| env: | |
| AWS_REGION: eu-central-1 # 사용할 AWS 리전 | |
| ECR_REPOSITORY: mapping_backend # ECR 리포지토리 이름 | |
| ECS_CLUSTER: mapping-eu # ECS 클러스터 이름 | |
| ECS_SERVICE: mapping-backend-task-2-service-3hw0c5wu # ECS 서비스 이름 | |
| ECS_TASK_DEFINITION: ecs/eu/taskdef.json # 태스크 정의 JSON 파일 경로 | |
| CONTAINER_NAME: mapping_backend # taskdef.json 내 컨테이너 이름 | |
| permissions: | |
| contents: read | |
| jobs: | |
| build-and-push: | |
| name: Build & Push to ECR | |
| runs-on: ubuntu-latest | |
| outputs: | |
| image: ${{ steps.build-image.outputs.image }} | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up JDK 17 | |
| uses: actions/setup-java@v3 | |
| with: | |
| java-version: '17' | |
| distribution: 'temurin' | |
| # application-*.yml 불러오기 | |
| - name: Create application.yml | |
| run: | | |
| mkdir -p src/main/resources | |
| echo "${{ secrets.APPLICATION_YML_EU }}" > src/main/resources/application.yml | |
| - name: Create application-jwt.yml | |
| run: | | |
| mkdir -p src/main/resources | |
| echo "${{ secrets.APPLICATION_JWT_YML }}" > src/main/resources/application-jwt.yml | |
| - name: Create application-oauth.yml | |
| run: | | |
| mkdir -p src/main/resources | |
| echo "${{ secrets.APPLICATION_OAUTH2_YML }}" > src/main/resources/application-oauth.yml | |
| - name: Create application-aws.yml | |
| run: | | |
| mkdir -p src/main/resources | |
| echo "${{ secrets.APPLICATION_AWS_YML }}" > src/main/resources/application-aws.yml | |
| - name: Set up Gradle | |
| uses: gradle/actions/setup-gradle@v4 | |
| - name: Build with Gradle | |
| run: ./gradlew clean bootJar | |
| - name: Configure AWS credentials | |
| uses: aws-actions/configure-aws-credentials@v4 | |
| 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@v2 | |
| - name: Build, tag and push Docker image | |
| id: build-image | |
| run: | | |
| IMAGE_TAG=${GITHUB_SHA::8} | |
| ECR_URI=${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY }} | |
| docker build -t $ECR_URI:$IMAGE_TAG . | |
| docker push $ECR_URI:$IMAGE_TAG | |
| echo "image=$ECR_URI:$IMAGE_TAG" >> $GITHUB_OUTPUT | |
| deploy: | |
| name: Deploy to ECS | |
| needs: build-and-push | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Configure AWS credentials | |
| uses: aws-actions/configure-aws-credentials@v4 | |
| 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: Render new Task Definition | |
| id: render-task-def | |
| uses: aws-actions/amazon-ecs-render-task-definition@v1 | |
| with: | |
| task-definition: ${{ env.ECS_TASK_DEFINITION }} | |
| container-name: ${{ env.CONTAINER_NAME }} | |
| image: ${{ needs.build-and-push.outputs.image }} | |
| - name: Write rendered task definition to file | |
| run: | | |
| printf '%s' "${{ steps.render-task-def.outputs.task-definition }}" > rendered-task-def.json | |
| - name: Register new Task Definition | |
| id: register-task-def | |
| run: | | |
| NEW_ARN=$(aws ecs register-task-definition \ | |
| --cli-input-json file://rendered-task-def.json \ | |
| --query 'taskDefinition.taskDefinitionArn' \ | |
| --output text) | |
| echo "new-task-def-arn=$NEW_ARN" >> $GITHUB_OUTPUT | |
| - name: Update ECS Service with force-new-deployment | |
| run: | | |
| aws ecs update-service \ | |
| --cluster "${{ env.ECS_CLUSTER }}" \ | |
| --service "${{ env.ECS_SERVICE }}" \ | |
| --task-definition "${{ steps.register-task-def.outputs.new-task-def-arn }}" \ | |
| --force-new-deployment \ | |
| --deployment-configuration maximumPercent=100,minimumHealthyPercent=0 | |
| - name: Wait for service to stabilize | |
| run: | | |
| aws ecs wait services-stable \ | |
| --cluster "${{ env.ECS_CLUSTER }}" \ | |
| --services "${{ env.ECS_SERVICE }}" | |
| # deploy: | |
| # name: Deploy to ECS | |
| # needs: build-and-push | |
| # runs-on: ubuntu-latest | |
| # steps: | |
| # - name: Checkout code | |
| # uses: actions/checkout@v4 | |
| # - name: Configure AWS credentials | |
| # uses: aws-actions/configure-aws-credentials@v4 | |
| # 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: Render new Task Definition | |
| # id: render-task-def | |
| # uses: aws-actions/amazon-ecs-render-task-definition@v1 | |
| # with: | |
| # task-definition: ${{ env.ECS_TASK_DEFINITION }} | |
| # container-name: ${{ env.CONTAINER_NAME }} | |
| # image: ${{ needs.build-and-push.outputs.image }} | |
| # - name: Deploy new Task Definition to ECS | |
| # uses: aws-actions/amazon-ecs-deploy-task-definition@v2 | |
| # with: | |
| # task-definition: ${{ steps.render-task-def.outputs.task-definition }} | |
| # service: ${{ env.ECS_SERVICE }} | |
| # cluster: ${{ env.ECS_CLUSTER }} | |
| # wait-for-service-stability: true |