Skip to content

Commit 4f5443d

Browse files
Fixing CI-CD pipeline. Adding ECS deployment
1 parent 3600f4e commit 4f5443d

File tree

3 files changed

+235
-115
lines changed

3 files changed

+235
-115
lines changed
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
name: west-discovery CI/CD
2+
3+
on:
4+
push:
5+
branches:
6+
- add-ci-cd
7+
- integration
8+
- main
9+
pull_request:
10+
branches:
11+
- main
12+
13+
env:
14+
AWS_REGION: us-east-1
15+
ECR_REPOSITORY: west-discovery
16+
CONTAINER_NAME: app
17+
ECS_TASK_DEFINITION: ecs/task-definition.json
18+
19+
permissions:
20+
id-token: write
21+
contents: read
22+
23+
jobs:
24+
# =========================
25+
# BUILD (always safe)
26+
# =========================
27+
build:
28+
runs-on: ubuntu-latest
29+
30+
outputs:
31+
image-uri: ${{ steps.export.outputs.image-uri }}
32+
33+
if: |
34+
github.event_name == 'push' &&
35+
(
36+
github.ref == 'refs/heads/add-ci-cd' ||
37+
github.ref == 'refs/heads/integration' ||
38+
github.ref == 'refs/heads/main'
39+
)
40+
41+
steps:
42+
- uses: actions/checkout@v4
43+
44+
- name: Configure AWS credentials (OIDC)
45+
uses: aws-actions/configure-aws-credentials@v4
46+
with:
47+
aws-region: ${{ env.AWS_REGION }}
48+
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
49+
50+
- name: Login to Amazon ECR
51+
id: ecr
52+
uses: aws-actions/amazon-ecr-login@v2
53+
54+
- name: Docker metadata
55+
id: meta
56+
uses: docker/metadata-action@v5
57+
with:
58+
images: |
59+
${{ steps.ecr.outputs.registry }}/${{ env.ECR_REPOSITORY }}
60+
tags: |
61+
type=ref,event=branch
62+
type=ref,event=pr
63+
type=sha,prefix={{branch}}-
64+
65+
- name: Build & push image
66+
uses: docker/build-push-action@v6
67+
with:
68+
context: .
69+
file: ./Dockerfile
70+
platforms: linux/x86_64
71+
push: true
72+
tags: ${{ steps.meta.outputs.tags }}
73+
labels: ${{ steps.meta.outputs.labels }}
74+
cache-from: type=gha
75+
cache-to: type=gha,mode=max
76+
77+
- name: Export image URI
78+
id: export
79+
run: |
80+
IMAGE_URI=$(echo "${{ steps.meta.outputs.tags }}" | head -n 1)
81+
echo "image-uri=$IMAGE_URI" >> $GITHUB_OUTPUT
82+
echo "$IMAGE_URI" > image-uri.txt
83+
84+
- name: Upload image artifact
85+
uses: actions/upload-artifact@v4
86+
with:
87+
name: image-uri
88+
path: image-uri.txt
89+
90+
# =========================
91+
# DEPLOY → INTEGRATION
92+
# =========================
93+
deploy-integration:
94+
runs-on: ubuntu-latest
95+
needs: build
96+
environment: integration
97+
98+
if: |
99+
(
100+
github.ref == 'refs/heads/add-ci-cd' ||
101+
github.ref == 'refs/heads/integration'
102+
)
103+
104+
env:
105+
ECS_CLUSTER: west-discovery-integration
106+
ECS_SERVICE: west-discovery
107+
108+
steps:
109+
- uses: actions/checkout@v4
110+
111+
- name: Configure AWS credentials (OIDC)
112+
uses: aws-actions/configure-aws-credentials@v4
113+
with:
114+
aws-region: ${{ env.AWS_REGION }}
115+
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
116+
117+
- uses: actions/download-artifact@v4
118+
with:
119+
name: image-uri
120+
121+
- run: echo "IMAGE_URI=$(cat image-uri.txt)" >> $GITHUB_ENV
122+
123+
- name: Render task definition
124+
id: render
125+
uses: aws-actions/amazon-ecs-render-task-definition@v1
126+
with:
127+
task-definition: ${{ env.ECS_TASK_DEFINITION }}
128+
container-name: ${{ env.CONTAINER_NAME }}
129+
image: ${{ env.IMAGE_URI }}
130+
131+
- name: Deploy to Integration
132+
uses: aws-actions/amazon-ecs-deploy-task-definition@v2
133+
with:
134+
cluster: ${{ env.ECS_CLUSTER }}
135+
service: ${{ env.ECS_SERVICE }}
136+
task-definition: ${{ steps.render.outputs.task-definition }}
137+
wait-for-service-stability: true
138+
139+
# =========================
140+
# DEPLOY → PRODUCTION
141+
# =========================
142+
deploy-production:
143+
runs-on: ubuntu-latest
144+
needs: build
145+
environment: production
146+
147+
# Only after PR is merged into main
148+
if: |
149+
github.event_name == 'push' &&
150+
github.event.pull_request.merged == true &&
151+
github.ref == 'refs/heads/main'
152+
153+
env:
154+
ECS_CLUSTER: west-discovery-production
155+
ECS_SERVICE: west-discovery
156+
157+
steps:
158+
- uses: actions/checkout@v4
159+
160+
- name: Configure AWS credentials (OIDC)
161+
uses: aws-actions/configure-aws-credentials@v4
162+
with:
163+
aws-region: ${{ env.AWS_REGION }}
164+
role-to-assume: ${{ secrets.AWS_PROD_ROLE_TO_ASSUME }}
165+
166+
- uses: actions/download-artifact@v4
167+
with:
168+
name: image-uri
169+
170+
- run: echo "IMAGE_URI=$(cat image-uri.txt)" >> $GITHUB_ENV
171+
172+
- name: Render task definition
173+
id: render
174+
uses: aws-actions/amazon-ecs-render-task-definition@v1
175+
with:
176+
task-definition: ${{ env.ECS_TASK_DEFINITION }}
177+
container-name: ${{ env.CONTAINER_NAME }}
178+
image: ${{ env.IMAGE_URI }}
179+
180+
- name: Deploy to Production
181+
uses: aws-actions/amazon-ecs-deploy-task-definition@v2
182+
with:
183+
cluster: ${{ env.ECS_CLUSTER }}
184+
service: ${{ env.ECS_SERVICE }}
185+
task-definition: ${{ steps.render.outputs.task-definition }}
186+
wait-for-service-stability: true

.github/workflows/build-and-push.yml

Lines changed: 0 additions & 115 deletions
This file was deleted.

ecs/task-definition.json

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"family": "west-discovery",
3+
"networkMode": "awsvpc",
4+
"requiresCompatibilities": ["FARGATE"],
5+
"cpu": "512",
6+
"memory": "1024",
7+
"runtimePlatform": {
8+
"cpuArchitecture": "X86_64",
9+
"operatingSystemFamily": "LINUX"
10+
},
11+
"taskRoleArn": "arn:aws:iam::730335463484:role/ecsTaskExecutionRole",
12+
"executionRoleArn": "arn:aws:iam::730335463484:role/ecsTaskExecutionRole",
13+
"containerDefinitions": [
14+
{
15+
"name": "app",
16+
"image": "REPLACED_BY_CI",
17+
"essential": true,
18+
"portMappings": [
19+
{
20+
"containerPort": 8000,
21+
"protocol": "tcp",
22+
"appProtocol": "http"
23+
}
24+
],
25+
"environment": [
26+
{ "name": "APP_ENV", "value": "integration" },
27+
{ "name": "LOG_LEVEL", "value": "info" }
28+
],
29+
"logConfiguration": {
30+
"logDriver": "awslogs",
31+
"options": {
32+
"awslogs-group": "/ecs/west-discovery",
33+
"awslogs-region": "us-east-1",
34+
"awslogs-stream-prefix": "ecs"
35+
}
36+
},
37+
"healthCheck": {
38+
"command": [
39+
"CMD-SHELL",
40+
"curl -f http://localhost:8000/health || exit 1"
41+
],
42+
"interval": 30,
43+
"timeout": 5,
44+
"retries": 3,
45+
"startPeriod": 15
46+
}
47+
}
48+
]
49+
}

0 commit comments

Comments
 (0)