Skip to content

Commit 5de6003

Browse files
author
colto
committed
Fix CI/CD
1 parent c3c8fd0 commit 5de6003

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

.github/workflows/ci.yml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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"

gh-actions-cloudpulse-policy.json

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"Version": "2012-10-17",
3+
"Statement": [
4+
{
5+
"Sid": "ECRAuth",
6+
"Effect": "Allow",
7+
"Action": [
8+
"ecr:GetAuthorizationToken"
9+
],
10+
"Resource": "*"
11+
},
12+
{
13+
"Sid": "ECRPushPull",
14+
"Effect": "Allow",
15+
"Action": [
16+
"ecr:BatchCheckLayerAvailability",
17+
"ecr:BatchGetImage",
18+
"ecr:CompleteLayerUpload",
19+
"ecr:GetDownloadUrlForLayer",
20+
"ecr:InitiateLayerUpload",
21+
"ecr:PutImage",
22+
"ecr:UploadLayerPart"
23+
],
24+
"Resource": [
25+
"arn:aws:ecr:us-east-1:413576439231:repository/cloudpulse-backend",
26+
"arn:aws:ecr:us-east-1:413576439231:repository/cloudpulse-frontend"
27+
]
28+
},
29+
{
30+
"Sid": "ECSDeploy",
31+
"Effect": "Allow",
32+
"Action": [
33+
"ecs:DescribeServices",
34+
"ecs:DescribeTaskDefinition",
35+
"ecs:RegisterTaskDefinition",
36+
"ecs:UpdateService"
37+
],
38+
"Resource": "*"
39+
},
40+
{
41+
"Sid": "PassTaskExecRole",
42+
"Effect": "Allow",
43+
"Action": "iam:PassRole",
44+
"Resource": "arn:aws:iam::413576439231:role/cloudpulse-dev-task-exec-role"
45+
}
46+
]
47+
}

trust-policy.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"Version": "2012-10-17",
3+
"Statement": [
4+
{
5+
"Effect": "Allow",
6+
"Principal": {
7+
"Federated": "arn:aws:iam::413576439231:oidc-provider/token.actions.githubusercontent.com"
8+
},
9+
"Action": "sts:AssumeRoleWithWebIdentity",
10+
"Condition": {
11+
"StringEquals": {
12+
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com"
13+
},
14+
"StringLike": {
15+
"token.actions.githubusercontent.com:sub": "repo:colecodesdev/cloudpulse:*"
16+
}
17+
}
18+
}
19+
]
20+
}

0 commit comments

Comments
 (0)