diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 00000000..fe63c434 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,111 @@ +name: 🚀 Deploy to AWS ECS (Monew) + +on: + push: + branches: + - main + +permissions: + contents: read + id-token: write + +env: + AWS_REGION: ap-northeast-2 + ECR_REPOSITORY: monew-repo + ECS_CLUSTER: monew-cluster + ECS_SERVICE: monew-service + ECS_TASK_DEFINITION: monew-task + CONTAINER_NAME: monew-app + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + - name: 📦 Checkout repository + uses: actions/checkout@v4 + + - name: 🔑 Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v4 + with: + aws-region: ${{ env.AWS_REGION }} + role-to-assume: ${{ secrets.AWS_ROLE_ARN }} + role-session-name: github-actions + + - name: 🔐 Login to Amazon ECR + id: login-ecr + uses: aws-actions/amazon-ecr-login@v2 + + - name: 🛠️ Build and push Docker image + env: + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} + run: | + echo "🔨 Building Docker image..." + docker build -t $ECR_REGISTRY/${{ env.ECR_REPOSITORY }}:latest . + docker push $ECR_REGISTRY/${{ env.ECR_REPOSITORY }}:latest + + - name: 📄 Download current ECS Task Definition + run: | + aws ecs describe-task-definition \ + --task-definition "$ECS_TASK_DEFINITION" \ + --query taskDefinition \ + --output json > task-definition.json + + - name: 🧩 Update Task Definition with new image + run: | + NEW_IMAGE="${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY }}:latest" + echo "✅ Updating container image to $NEW_IMAGE" + jq --arg IMAGE "$NEW_IMAGE" \ + '.containerDefinitions[0].image = $IMAGE' \ + task-definition.json > new-task-definition.json + + - name: 📝 Register new ECS Task Definition + id: register-task + run: | + NEW_TASK_DEF_ARN=$(aws ecs register-task-definition \ + --cli-input-json file://new-task-definition.json \ + --query "taskDefinition.taskDefinitionArn" \ + --output text) + echo "✅ New Task Definition ARN: $NEW_TASK_DEF_ARN" + echo "NEW_TASK_DEF_ARN=$NEW_TASK_DEF_ARN" >> $GITHUB_ENV + + - name: 🚀 Deploy new Task Definition to ECS Service + run: | + echo "Deploying new Task Definition to ECS..." + aws ecs update-service \ + --cluster $ECS_CLUSTER \ + --service $ECS_SERVICE \ + --task-definition $NEW_TASK_DEF_ARN + aws ecs wait services-stable \ + --cluster $ECS_CLUSTER \ + --services $ECS_SERVICE + echo "✅ ECS Service update triggered successfully!" + + - name: ✅ Confirm Deployment (with validation) + run: | + echo "🔍 Checking ECS deployment status..." + + STATUS_JSON=$(aws ecs describe-services \ + --cluster $ECS_CLUSTER \ + --services $ECS_SERVICE) + + PRIMARY_DEPLOYMENT=$(echo "$STATUS_JSON" | jq -r '.services[0].deployments[] | select(.status=="PRIMARY")') + + DESIRED_COUNT=$(echo "$PRIMARY_DEPLOYMENT" | jq -r '.desiredCount') + RUNNING_COUNT=$(echo "$PRIMARY_DEPLOYMENT" | jq -r '.runningCount') + FAILED_COUNT=$(echo "$PRIMARY_DEPLOYMENT" | jq -r '.failedTasks') + + echo "Desired: $DESIRED_COUNT, Running: $RUNNING_COUNT, Failed: $FAILED_COUNT" + + if [ "$RUNNING_COUNT" -lt "$DESIRED_COUNT" ]; then + echo "❌ Deployment not yet stable — runningCount < desiredCount" + exit 1 + fi + + if [ "$FAILED_COUNT" != "0" ]; then + echo "🚨 Deployment has failed tasks!" + exit 1 + fi + + echo "✅ Deployment verified successfully!" + diff --git a/monew/Dockerfile b/monew/Dockerfile index db11ac17..89288a25 100644 --- a/monew/Dockerfile +++ b/monew/Dockerfile @@ -22,7 +22,7 @@ WORKDIR /app RUN yum update -y && yum install -y curl && yum clean all ENV SERVER_PORT=8080 \ - SPRING_PROFILES_ACTIVE=docker \ + SPRING_PROFILES_ACTIVE=local \ PROJECT_NAME=sb05-monew-team7 \ PROJECT_VERSION=1.0.0 diff --git a/monew/docker-compose.yml b/monew/docker-compose.yml index 8e57ca98..e0683450 100644 --- a/monew/docker-compose.yml +++ b/monew/docker-compose.yml @@ -82,6 +82,11 @@ services: test: ["CMD-SHELL", "curl -f http://localhost:8080/actuator/health || exit 1"] interval: 10s retries: 5 + deploy: + resources: + limits: + cpus: "2.0" + memory: 4g ports: - "8080:8080" - "1327:1327" diff --git a/monew/src/main/java/com/spring/monew/interest/repository/impl/InterestRepositoryCustomImpl.java b/monew/src/main/java/com/spring/monew/interest/repository/impl/InterestRepositoryCustomImpl.java index 64f4e0ce..13aa7bd1 100644 --- a/monew/src/main/java/com/spring/monew/interest/repository/impl/InterestRepositoryCustomImpl.java +++ b/monew/src/main/java/com/spring/monew/interest/repository/impl/InterestRepositoryCustomImpl.java @@ -169,7 +169,7 @@ public List findSimilarNames(String name, double threshold) { private OrderSpecifier getOrderSpecifier(String orderBy, String direction) { Order order = "DESC".equalsIgnoreCase(direction) ? Order.DESC : Order.ASC; if ("subscriberCount".equals(orderBy)) { - new OrderSpecifier<>(order, InterestRepositoryCustomImpl.interest.subscriptionsCount); + return new OrderSpecifier<>(order, InterestRepositoryCustomImpl.interest.subscriptionsCount); } return new OrderSpecifier<>(order, InterestRepositoryCustomImpl.interest.name); } diff --git a/monew/src/main/resources/application.yml b/monew/src/main/resources/application.yml index 1ad9af97..576034c9 100644 --- a/monew/src/main/resources/application.yml +++ b/monew/src/main/resources/application.yml @@ -33,6 +33,7 @@ spring: redis: host: ${SPRING_DATA_REDIS_HOST:localhost} port: ${SPRING_DATA_REDIS_PORT:6379} + password: ${SPRING_DATA_REDIS_PASSWORD} batch: job: