From 8e17de481d796a469f17e7c3d9c7b0001a9677fb Mon Sep 17 00:00:00 2001 From: hen715 Date: Mon, 31 Mar 2025 15:58:10 +0900 Subject: [PATCH 1/3] =?UTF-8?q?ci/cd=20:=20ecs=20=EB=B0=B0=ED=8F=AC=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ecsCd.yml | 85 +++++++++++++++++++++++++++++++++++++ Dockerfile | 10 +++++ ecs-task-def.json | 29 +++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 .github/workflows/ecsCd.yml create mode 100644 Dockerfile create mode 100644 ecs-task-def.json diff --git a/.github/workflows/ecsCd.yml b/.github/workflows/ecsCd.yml new file mode 100644 index 00000000..75d3b692 --- /dev/null +++ b/.github/workflows/ecsCd.yml @@ -0,0 +1,85 @@ +name: Deploy Spring Boot to ECS (Fargate) + +on: + push: + branches: + - main + +env: + AWS_REGION: ap-northeast-2 + ECR_REPOSITORY: spring-app + ECS_CLUSTER: prod-cluster + ECS_SERVICE: spring-service + IMAGE_TAG: latest + CONTAINER_NAME: spring-container + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Create resources folder if not exists + run: mkdir -p ./src/main/resources + + - name: Create application.yml from GitHub Secret + run: echo "${{ secrets.APPLICATION_YML }}" > ./src/main/resources/application.yml + + - name: Set up JDK 23 + uses: actions/setup-java@v3 + with: + java-version: '23' + + - name: Build Spring Boot app + run: ./gradlew clean build -x test + + - 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: ${{ env.AWS_REGION }} + + - name: Log in to Amazon ECR + uses: aws-actions/amazon-ecr-login@v1 + + - name: Get AWS Account ID + id: aws-account + run: | + ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text) + echo "ACCOUNT_ID=$ACCOUNT_ID" >> $GITHUB_ENV + echo "::set-output name=account_id::$ACCOUNT_ID" + + - name: Build and push Docker image to ECR + run: | + IMAGE_URI=${{ steps.aws-account.outputs.account_id }}.dkr.ecr.${{ env.AWS_REGION }}.amazonaws.com/${{ env.ECR_REPOSITORY }} + docker build -t $IMAGE_URI:$IMAGE_TAG . + docker push $IMAGE_URI:$IMAGE_TAG + echo "IMAGE_URI=$IMAGE_URI:$IMAGE_TAG" >> $GITHUB_ENV + echo "::set-output name=image_uri::$IMAGE_URI:$IMAGE_TAG" + + - name: Replace placeholders in ecs-task-def.json + run: | + sed -i "s||${{ steps.aws-account.outputs.account_id }}|g" ecs-task-def.json + sed -i "s||${{ steps.build-and-push.outputs.image_uri }}|g" ecs-task-def.json + cat ecs-task-def.json + + - name: Register new ECS task definition + id: register-task + run: | + TASK_DEF_ARN=$(aws ecs register-task-definition \ + --cli-input-json file://ecs-task-def.json \ + --query "taskDefinition.taskDefinitionArn" \ + --output text) + echo "TASK_DEF_ARN=$TASK_DEF_ARN" + echo "::set-output name=task_definition_arn::$TASK_DEF_ARN" + + - name: Deploy to ECS (Fargate) + uses: aws-actions/amazon-ecs-deploy-task-definition@v1 + with: + cluster: ${{ env.ECS_CLUSTER }} + service: ${{ env.ECS_SERVICE }} + task-definition: ${{ steps.register-task.outputs.task_definition_arn }} + wait-for-service-stability: true diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..fc9e50f9 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM eclipse-temurin:23-jdk-jammy as build +WORKDIR /app +COPY . . +RUN ./gradlew clean build -x test +FROM eclipse-temurin:23-jre-jammy +WORKDIR /app +COPY --from=build /app/build/libs/*.jar app.jar +COPY src/main/resources/application.yml ./src/main/resources/application.yml +EXPOSE 8080 +ENTRYPOINT ["java", "-jar", "app.jar"] diff --git a/ecs-task-def.json b/ecs-task-def.json new file mode 100644 index 00000000..8de411b3 --- /dev/null +++ b/ecs-task-def.json @@ -0,0 +1,29 @@ +{ + "family": "spring-app", + "networkMode": "awsvpc", + "requiresCompatibilities": ["FARGATE"], + "cpu": "1024", + "memory": "2048", + "executionRoleArn": "arn:aws:iam:::role/ecsTaskExecutionRole", + "containerDefinitions": [ + { + "name": "spring-container", + "image": "", + "essential": true, + "portMappings": [ + { + "containerPort": 8080, + "protocol": "tcp" + } + ], + "logConfiguration": { + "logDriver": "awslogs", + "options": { + "awslogs-group": "/ecs/spring-app", + "awslogs-region": "ap-northeast-2", + "awslogs-stream-prefix": "ecs" + } + } + } + ] +} From 477d792a0ecd5205a2bc1dbf5bad334faa5e5929 Mon Sep 17 00:00:00 2001 From: hen715 Date: Mon, 31 Mar 2025 15:59:25 +0900 Subject: [PATCH 2/3] =?UTF-8?q?ci/cd=20:=20ecs=20=EB=B0=B0=ED=8F=AC=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ecsCd.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ecsCd.yml b/.github/workflows/ecsCd.yml index 75d3b692..41e36faa 100644 --- a/.github/workflows/ecsCd.yml +++ b/.github/workflows/ecsCd.yml @@ -3,7 +3,7 @@ name: Deploy Spring Boot to ECS (Fargate) on: push: branches: - - main + env: AWS_REGION: ap-northeast-2 From b5d83dcf7b0a382985119889300ceab9a900358e Mon Sep 17 00:00:00 2001 From: hen715 Date: Mon, 31 Mar 2025 16:03:49 +0900 Subject: [PATCH 3/3] =?UTF-8?q?ci/cd=20:=20ecs=20=EB=B0=B0=ED=8F=AC=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ecsCd.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ecsCd.yml b/.github/workflows/ecsCd.yml index 41e36faa..287a3358 100644 --- a/.github/workflows/ecsCd.yml +++ b/.github/workflows/ecsCd.yml @@ -3,7 +3,7 @@ name: Deploy Spring Boot to ECS (Fargate) on: push: branches: - + - mainDeploy env: AWS_REGION: ap-northeast-2