diff --git a/.github/workflows/aws.yml b/.github/workflows/aws.yml index 8f5c3ed..6735a7d 100644 --- a/.github/workflows/aws.yml +++ b/.github/workflows/aws.yml @@ -1,4 +1,4 @@ -name: Deploy to Amazon ECS +name: Deploy to AWS on: push: @@ -31,18 +31,18 @@ jobs: - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - - name: Configure AWS credentials + - 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: ${{ secrets.AWS_REGION }} - - name: Login to Amazon ECR + - name: Login to AWS ECR id: login-ecr uses: aws-actions/amazon-ecr-login@v2 - - name: Build and push new Docker image to Amazon ECR + - name: Build and Push New Docker Image to Amazon ECR id: build-image env: ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} @@ -56,7 +56,35 @@ jobs: docker buildx build --platform linux/amd64 --push -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT - - name: Register ECS Task Definition + - name: Check for task definition changes + id: check-task-def + run: | + # Enable error handling + set -e + set -o pipefail + + # Fetch the latest task definition + LATEST_TASK_DEF=$(aws ecs describe-task-definition \ + --task-definition ${{ env.ECS_SERVICE }} \ + --query 'taskDefinition') + + # Strip out fields that change with every revision or are not part of the input JSON + STRIPPED_LATEST_TASK_DEF=$(echo "$LATEST_TASK_DEF" | jq 'del(.taskDefinitionArn, .revision, .status, .requiresAttributes, .registeredAt, .registeredBy)') + + # Load the current task definition from the file + FILE_TASK_DEF=$(jq 'del(.family, .executionRoleArn, .revision, .status)' $ECS_TASK_DEFINITION) + + # Compare the current task definition with the latest one + if [ "$STRIPPED_LATEST_TASK_DEF" = "$FILE_TASK_DEF" ]; then + echo "No changes to task definition, skipping registration." + echo "SKIP_REGISTRATION=true" >> $GITHUB_ENV + else + echo "Changes detected in task definition, registering new revision." + echo "SKIP_REGISTRATION=false" >> $GITHUB_ENV + fi + + - name: Register ECS task definition + if: env.SKIP_REGISTRATION != 'true' id: register-task-def run: | # Enable error handling @@ -64,24 +92,33 @@ jobs: set -o pipefail # Register the task definition with ECS and capture the revision number - TASK_DEFINITION_ARN=$(aws ecs register-task-definition - --cli-input-json file://$ECS_TASK_DEFINITION - --query 'taskDefinition.taskDefinitionArn' --output text) + TASK_DEFINITION_ARN=$(aws ecs register-task-definition --cli-input-json file://$ECS_TASK_DEFINITION --query 'taskDefinition.taskDefinitionArn' --output text) echo "TASK_DEFINITION_ARN=$TASK_DEFINITION_ARN" >> $GITHUB_ENV - - name: Deploy to Amazon ECS + - name: Deploy to AWS ECS run: | # Enable error handling set -e set -o pipefail + # Use the latest task definition ARN or skip if registration was not necessary + if [ -z "${{ env.TASK_DEFINITION_ARN }}" ]; then + TASK_DEFINITION_ARN=$(aws ecs describe-services \ + --cluster ${{ env.ECS_CLUSTER }} \ + --services ${{ env.ECS_SERVICE }} \ + --query 'services[0].taskDefinition' --output text) + fi + # Update the ECS service to use the latest task definition revision - aws ecs update-service - --cluster ${{ env.ECS_CLUSTER }} - --service ${{ env.ECS_SERVICE }} - --task-definition ${{ env.TASK_DEFINITION_ARN }} - --force-new-deployment + aws ecs update-service \ + --cluster ${{ env.ECS_CLUSTER }} \ + --service ${{ env.ECS_SERVICE }} \ + --task-definition $TASK_DEFINITION_ARN \ + --force-new-deployment \ --deployment-configuration minimumHealthyPercent=0,maximumPercent=100 - --cluster ${{ env.ECS_CLUSTER }} + + # Wait until the service has stabilized + aws ecs wait services-stable \ + --cluster ${{ env.ECS_CLUSTER }} \ --services ${{ env.ECS_SERVICE }}