-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskfile.yml
More file actions
252 lines (218 loc) · 6.99 KB
/
Taskfile.yml
File metadata and controls
252 lines (218 loc) · 6.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
version: "1"
name: cloud-app
description: "AWS deploy: Lambda + ECS + S3, multi-region, env_file per environment"
variables:
APP_NAME: cloud-app
AWS_ACCOUNT: "123456789012"
ECR_REPO: ${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/${APP_NAME}
S3_BUCKET: ${APP_NAME}-assets
LAMBDA_FUNCTION: ${APP_NAME}-processor
ECS_CLUSTER: ${APP_NAME}-cluster
ECS_SERVICE: ${APP_NAME}-api
TAG: latest
environments:
dev:
env_file: .env.dev
variables:
AWS_REGION: eu-west-1
AWS_PROFILE: dev
ECS_DESIRED_COUNT: "1"
LAMBDA_MEMORY: "256"
DOMAIN: dev.example.com
staging:
env_file: .env.staging
variables:
AWS_REGION: eu-west-1
AWS_PROFILE: staging
ECS_DESIRED_COUNT: "2"
LAMBDA_MEMORY: "512"
DOMAIN: staging.example.com
prod-eu:
env_file: .env.prod-eu
variables:
AWS_REGION: eu-west-1
AWS_PROFILE: prod
ECS_DESIRED_COUNT: "4"
LAMBDA_MEMORY: "1024"
DOMAIN: eu.example.com
prod-us:
env_file: .env.prod-us
variables:
AWS_REGION: us-east-1
AWS_PROFILE: prod-us
ECS_DESIRED_COUNT: "4"
LAMBDA_MEMORY: "1024"
DOMAIN: us.example.com
environment_groups:
all-prod:
members: [prod-eu, prod-us]
strategy: canary
canary_count: 1
global:
members: [staging, prod-eu, prod-us]
strategy: rolling
max_parallel: 1
# ─── Pipeline ─────────────────────────────────────────
pipeline:
secrets: [AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY]
stages:
- name: test
tasks: [lint, test]
- name: build
tasks: [build, ecr-push, lambda-package]
docker_in_docker: true
- name: deploy-staging
tasks: [ecs-deploy, lambda-deploy, s3-sync]
env: staging
when: "branch:main"
- name: deploy-prod
tasks: [ecs-deploy, lambda-deploy, s3-sync]
env: prod-eu
when: manual
tasks:
# ─── Build ──────────────────────────────────────────
lint:
desc: Lint code
stage: test
cmds:
- ruff check src/
ignore_errors: true
test:
desc: Run tests
stage: test
cmds:
- pytest tests/ -v
build:
desc: Build Docker image for ECS
stage: build
deps: [test]
cmds:
- docker build -t ${APP_NAME}:${TAG} .
# ─── ECR ────────────────────────────────────────────
ecr-login:
desc: Login to ECR
cmds:
- aws ecr get-login-password --region ${AWS_REGION} --profile ${AWS_PROFILE} | docker login --username AWS --password-stdin ${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com
ecr-push:
desc: Tag + push to ECR
stage: build
deps: [build, ecr-login]
cmds:
- docker tag ${APP_NAME}:${TAG} ${ECR_REPO}:${TAG}
- docker push ${ECR_REPO}:${TAG}
# ─── ECS ────────────────────────────────────────────
ecs-deploy:
desc: Force new ECS deployment
cmds:
- >-
aws ecs update-service
--cluster ${ECS_CLUSTER}
--service ${ECS_SERVICE}
--force-new-deployment
--desired-count ${ECS_DESIRED_COUNT}
--region ${AWS_REGION}
--profile ${AWS_PROFILE}
- >-
aws ecs wait services-stable
--cluster ${ECS_CLUSTER}
--services ${ECS_SERVICE}
--region ${AWS_REGION}
--profile ${AWS_PROFILE}
ecs-status:
desc: Show ECS service status
cmds:
- >-
aws ecs describe-services
--cluster ${ECS_CLUSTER}
--services ${ECS_SERVICE}
--region ${AWS_REGION}
--profile ${AWS_PROFILE}
--query 'services[0].{status:status,desired:desiredCount,running:runningCount,pending:pendingCount}'
ecs-logs:
desc: Tail ECS CloudWatch logs
cmds:
- >-
aws logs tail /ecs/${ECS_CLUSTER}/${ECS_SERVICE}
--follow --since 30m
--region ${AWS_REGION}
--profile ${AWS_PROFILE}
ecs-scale:
desc: Scale ECS service (--var ECS_DESIRED_COUNT=N)
cmds:
- >-
aws ecs update-service
--cluster ${ECS_CLUSTER}
--service ${ECS_SERVICE}
--desired-count ${ECS_DESIRED_COUNT}
--region ${AWS_REGION}
--profile ${AWS_PROFILE}
# ─── Lambda ─────────────────────────────────────────
lambda-package:
desc: Package Lambda function
stage: build
dir: lambda/
cmds:
- pip install -r requirements.txt -t package/
- cd package && zip -r ../function.zip .
- zip function.zip handler.py
lambda-deploy:
desc: Deploy Lambda function
dir: lambda/
cmds:
- >-
aws lambda update-function-code
--function-name ${LAMBDA_FUNCTION}
--zip-file fileb://function.zip
--region ${AWS_REGION}
--profile ${AWS_PROFILE}
- >-
aws lambda update-function-configuration
--function-name ${LAMBDA_FUNCTION}
--memory-size ${LAMBDA_MEMORY}
--region ${AWS_REGION}
--profile ${AWS_PROFILE}
lambda-invoke:
desc: Test invoke Lambda
cmds:
- >-
aws lambda invoke
--function-name ${LAMBDA_FUNCTION}
--payload '{"test": true}'
--region ${AWS_REGION}
--profile ${AWS_PROFILE}
/tmp/lambda-response.json
- cat /tmp/lambda-response.json
# ─── S3 ─────────────────────────────────────────────
s3-sync:
desc: Sync static assets to S3
cmds:
- aws s3 sync dist/static/ s3://${S3_BUCKET}/ --delete --region ${AWS_REGION} --profile ${AWS_PROFILE}
# ─── CDN ────────────────────────────────────────────
cdn-invalidate:
desc: Invalidate CloudFront cache
condition: "test -n \"${CF_DISTRIBUTION_ID}\""
cmds:
- >-
aws cloudfront create-invalidation
--distribution-id ${CF_DISTRIBUTION_ID}
--paths "/*"
--profile ${AWS_PROFILE}
# ─── Full release ───────────────────────────────────
release:
desc: Build → push → deploy ECS + Lambda + S3 + invalidate CDN
deps: [lint, test]
parallel: true
cmds:
- docker build -t ${APP_NAME}:${TAG} .
- docker tag ${APP_NAME}:${TAG} ${ECR_REPO}:${TAG}
- docker push ${ECR_REPO}:${TAG}
health:
desc: Check app health
silent: true
cmds:
- curl -sf https://${DOMAIN}/health || exit 1
clean:
desc: Remove build artifacts
cmds:
- rm -rf dist/ lambda/package/ lambda/function.zip
- docker rmi ${APP_NAME}:${TAG} ${ECR_REPO}:${TAG} 2>/dev/null || true