@@ -152,3 +152,87 @@ jobs:
152152 tags : |
153153 ${{ steps.login-ecr.outputs.registry }}/cloudpulse-frontend:${{ github.sha }}
154154 ${{ steps.login-ecr.outputs.registry }}/cloudpulse-frontend:latest
155+
156+ deploy-ecs :
157+ name : Deploy to ECS (Register Task Def + Update Service)
158+ runs-on : ubuntu-latest
159+ if : github.event_name == 'push' && github.ref == 'refs/heads/main'
160+ needs : [backend-ecr-push, frontend-ecr-push]
161+
162+ steps :
163+ - name : Configure AWS credentials (OIDC)
164+ uses : aws-actions/configure-aws-credentials@v4
165+ with :
166+ role-to-assume : ${{ secrets.AWS_ROLE_ARN }}
167+ aws-region : ${{ secrets.AWS_REGION }}
168+
169+ - name : Install jq
170+ run : |
171+ sudo apt-get update
172+ sudo apt-get install -y jq
173+
174+ - name : Register new task definition revision and update service
175+ shell : bash
176+ run : |
177+ set -euo pipefail
178+
179+ CLUSTER="cloudpulse-dev-cluster"
180+ SERVICE="cloudpulse-dev-app"
181+ REGION="${{ secrets.AWS_REGION }}"
182+ ACCOUNT_ID="413576439231"
183+ ECR_BASE="${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com"
184+
185+ BACKEND_IMAGE="${ECR_BASE}/cloudpulse-backend:${{ github.sha }}"
186+ FRONTEND_IMAGE="${ECR_BASE}/cloudpulse-frontend:${{ github.sha }}"
187+
188+ CURRENT_TASK_DEF_ARN=$(aws ecs describe-services \
189+ --cluster "$CLUSTER" \
190+ --services "$SERVICE" \
191+ --region "$REGION" \
192+ --query "services[0].taskDefinition" \
193+ --output text)
194+
195+ aws ecs describe-task-definition \
196+ --task-definition "$CURRENT_TASK_DEF_ARN" \
197+ --region "$REGION" \
198+ --query "taskDefinition" \
199+ --output json > taskdef.json
200+
201+ jq --arg BACKEND_IMAGE "$BACKEND_IMAGE" \
202+ --arg FRONTEND_IMAGE "$FRONTEND_IMAGE" \
203+ '
204+ del(
205+ .taskDefinitionArn,
206+ .revision,
207+ .status,
208+ .requiresAttributes,
209+ .compatibilities,
210+ .registeredAt,
211+ .registeredBy
212+ )
213+ | .containerDefinitions |= map(
214+ if .name == "backend" then .image = $BACKEND_IMAGE
215+ elif .name == "frontend" then .image = $FRONTEND_IMAGE
216+ else .
217+ end
218+ )
219+ ' taskdef.json > taskdef-new.json
220+
221+ NEW_TASK_DEF_ARN=$(aws ecs register-task-definition \
222+ --region "$REGION" \
223+ --cli-input-json file://taskdef-new.json \
224+ --query "taskDefinition.taskDefinitionArn" \
225+ --output text)
226+
227+ aws ecs update-service \
228+ --cluster "$CLUSTER" \
229+ --service "$SERVICE" \
230+ --task-definition "$NEW_TASK_DEF_ARN" \
231+ --region "$REGION"
232+
233+ aws ecs wait services-stable \
234+ --cluster "$CLUSTER" \
235+ --services "$SERVICE" \
236+ --region "$REGION"
237+
238+ echo "Deployment complete: $NEW_TASK_DEF_ARN"
0 commit comments