diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 21e9947c..66994f82 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -57,12 +57,7 @@ jobs: - name: Deploy to ECS if: ${{ inputs.environment == 'staging' }} - uses: aws-actions/amazon-ecs-deploy-task-definition@v2 - with: - service: terrateam-app-service - cluster: terrateam-app - image: ghcr.io/terrateamio/terrat-ee:{{ env.VERSION_TAG }} - wait-for-service-stability: true + run: ./scripts/deploy_ecs - name: Deploy to Fly.io if: ${{ inputs.environment == 'production' }} diff --git a/scripts/deploy_ecs b/scripts/deploy_ecs new file mode 100755 index 00000000..12586734 --- /dev/null +++ b/scripts/deploy_ecs @@ -0,0 +1,35 @@ +#!/usr/bin/env bash +set -euo pipefail + +CLUSTER_NAME="terrateam-app" +SERVICE_NAME="terrateam-app-service" +IMAGE_NAME="terrat-ee" +GHCR_IMAGE="ghcr.io/${GITHUB_REPOSITORY_OWNER}/${IMAGE_NAME}:${VERSION_TAG}" + +# Fetch the current service information +aws ecs describe-services --cluster "$CLUSTER_NAME" --services "$SERVICE_NAME" > service.json +TASK_DEF_ARN=$(jq -r '.services[0].taskDefinition' service.json) + +# Fetch the current task definition +aws ecs describe-task-definition --task-definition "$TASK_DEF_ARN" > task-def.json + +# Update the app container image +jq --arg newImage "$GHCR_IMAGE" ' + .taskDefinition.containerDefinitions |= map( + if .name == "app" then + .image = $newImage + else + . + end + ) +' task-def.json > task-def-updated.json + +# Register the new task definition +NEW_TASK_DEF=$(aws ecs register-task-definition --cli-input-json file://task-def-updated.json) +NEW_TASK_DEF_ARN=$(echo "$NEW_TASK_DEF" | jq -r '.taskDefinition.taskDefinitionArn') + +# Update the service to use the new task definition +aws ecs update-service --cluster "$CLUSTER_NAME" --service "$SERVICE_NAME" --task-definition "$NEW_TASK_DEF_ARN" + +# Wait for the service to stabilize +aws ecs wait services-stable --cluster "$CLUSTER_NAME" --services "$SERVICE_NAME"