-
Notifications
You must be signed in to change notification settings - Fork 0
585 lines (493 loc) · 17.6 KB
/
Copy pathci-cd.yml
File metadata and controls
585 lines (493 loc) · 17.6 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
name: CI/CD Pipeline
on:
push:
branches: [ main, master, develop ]
pull_request:
branches: [ main, master ]
workflow_dispatch:
inputs:
environment:
description: 'Deployment environment'
required: true
default: 'dev'
type: choice
options:
- dev
- staging
- prod
env:
PYTHON_VERSION: '3.11'
NODE_VERSION: '18'
AWS_DEFAULT_REGION: us-east-1
jobs:
# Security and code quality checks
security-scan:
name: Security & Code Quality
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0 # Full history for better analysis
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: 1.6.1
virtualenvs-create: true
virtualenvs-in-project: true
- name: Cache Poetry dependencies
uses: actions/cache@v3
with:
path: .venv
key: venv-${{ runner.os }}-${{ env.PYTHON_VERSION }}-${{ hashFiles('**/poetry.lock') }}
- name: Install dependencies
run: poetry install --with dev,test
- name: Run budget guardrail (fail-fast)
if: matrix.python-version == '3.11'
env:
# Expose repo variables for all environments; the script will pick the right one via --env
BUDGET: ${{ vars.BUDGET }}
BUDGET_DEV: ${{ vars.BUDGET_DEV }}
BUDGET_STAGING: ${{ vars.BUDGET_STAGING }}
BUDGET_PROD: ${{ vars.BUDGET_PROD }}
# Determine target environment based on PR base branch or push branch
GUARDRAIL_TARGET: ${{ (github.event_name == 'pull_request' && (github.event.pull_request.base.ref == 'develop') && 'dev') || (github.event_name == 'pull_request' && (github.event.pull_request.base.ref == 'main' || github.event.pull_request.base.ref == 'master') && 'prod') || (github.ref == 'refs/heads/develop' && 'dev') || (github.ref == 'refs/heads/main' && 'prod') || (github.ref == 'refs/heads/master' && 'prod') || 'staging' }}
run: |
echo "Guardrail target environment: $GUARDRAIL_TARGET"
python scripts/section11_budget_guardrail.py --predicted-json dashboards/predicted_cost.json --budget 1000 --env $GUARDRAIL_TARGET --block-exit
- name: Run security checks with bandit
run: |
poetry run bandit -r src/ -f json -o bandit-report.json
poetry run bandit -r src/ --severity-level medium
- name: Run dependency vulnerability check
run: |
poetry run safety check --json --output safety-report.json
poetry run safety check
- name: Code quality with ruff
run: |
poetry run ruff check src/ tests/ --output-format=github
poetry run ruff format --check src/ tests/
- name: Type checking with mypy
run: poetry run mypy src/ --ignore-missing-imports
- name: Upload security reports
uses: actions/upload-artifact@v3
if: always()
with:
name: security-reports
path: |
bandit-report.json
safety-report.json
# Unit and integration tests
test:
name: Tests
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.11', '3.12']
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: 1.6.1
virtualenvs-create: true
virtualenvs-in-project: true
- name: Cache Poetry dependencies
uses: actions/cache@v3
with:
path: .venv
key: venv-${{ runner.os }}-${{ matrix.python-version }}-${{ hashFiles('**/poetry.lock') }}
- name: Install dependencies
run: poetry install --with dev,test
- name: Run unit tests (fast fail for new tests)
run: |
poetry run pytest tests/test_retrain_lambda.py tests/test_shutdown_lambda.py -q
- name: Run full unit tests
run: |
poetry run pytest tests/ \
--cov=src \
--cov-report=xml \
--cov-report=html \
--cov-report=term \
--junitxml=test-results.xml \
--tb=short \
-v
- name: Upload test results
uses: actions/upload-artifact@v3
if: always()
with:
name: test-results-${{ matrix.python-version }}
path: |
test-results.xml
htmlcov/
.coverage
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
if: matrix.python-version == '3.11'
with:
file: ./coverage.xml
flags: unittests
name: codecov-umbrella
# Package model/inference artifacts and optionally upload to S3
package-artifacts:
name: Package Artifacts
runs-on: ubuntu-latest
needs: [test]
if: >-
github.event_name == 'workflow_dispatch' ||
github.ref == 'refs/heads/main' ||
github.ref == 'refs/heads/master' ||
github.ref == 'refs/heads/develop'
env:
PYTHON_VERSION: ${{ env.PYTHON_VERSION }}
S3_MODEL_ARTIFACTS_BUCKET: ${{ vars.S3_MODEL_ARTIFACTS_BUCKET }}
DISPATCH_ENV: ${{ github.event.inputs.environment }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: 1.6.1
virtualenvs-create: true
virtualenvs-in-project: true
- name: Cache Poetry deps
uses: actions/cache@v3
with:
path: .venv
key: venv-${{ runner.os }}-${{ env.PYTHON_VERSION }}-${{ hashFiles('**/poetry.lock') }}
- name: Install dependencies (main)
run: poetry install --only=main
- name: Determine target environment
run: |
if [ -n "${DISPATCH_ENV}" ]; then
ENV_NAME="${DISPATCH_ENV}"
elif [ "${GITHUB_REF}" = "refs/heads/develop" ]; then
ENV_NAME="dev"
else
# Default to staging for main/master or others
ENV_NAME="staging"
fi
echo "ENV_NAME=${ENV_NAME}" >> $GITHUB_ENV
echo "Target environment: ${ENV_NAME}"
- name: Ensure boto3 if uploading
if: env.S3_MODEL_ARTIFACTS_BUCKET != ''
run: |
poetry run python - <<'PY'
try:
import boto3 # noqa: F401
print('boto3 present')
except Exception:
import sys, subprocess
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'boto3'])
PY
- name: Package artifacts
run: |
EXTRA_ARGS=""
if [ -n "${S3_MODEL_ARTIFACTS_BUCKET}" ]; then
EXTRA_ARGS="--s3-bucket ${S3_MODEL_ARTIFACTS_BUCKET}"
fi
poetry run python scripts/package_forecaster_artifacts.py --env "${ENV_NAME}" ${EXTRA_ARGS}
- name: Upload packaged artifacts
uses: actions/upload-artifact@v3
with:
name: model-package-${{ env.ENV_NAME }}
path: |
artifacts/${{ env.ENV_NAME }}/model_package-*.zip
artifacts/${{ env.ENV_NAME }}/model_package-*.sha256
# Infrastructure validation
infrastructure-check:
name: Infrastructure Validation
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Install CDK
run: npm install -g aws-cdk@2.100.0
- name: Install Poetry
uses: snok/install-poetry@v1
- name: Install dependencies
run: poetry install
- name: CDK Synth (validate templates)
run: |
cd infra
poetry run cdk synth --all --no-staging > /dev/null
- name: Validate CloudFormation templates
run: |
cd infra/cdk.out
for template in *.template.json; do
if [ -f "$template" ]; then
echo "Validating $template"
aws cloudformation validate-template \
--template-body file://$template \
--region ${{ env.AWS_DEFAULT_REGION }} || true
fi
done
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
- name: Security check with cdk-nag
run: |
cd infra
npm install cdk-nag
# Add cdk-nag checks to CDK app
echo "# CDK NAG security checks would run here"
- name: Infrastructure cost estimation
run: |
# Placeholder for cost estimation
echo "Infrastructure cost estimation would run here"
echo "Consider integrating with Infracost or AWS Pricing Calculator"
# Build and package
build:
name: Build & Package
runs-on: ubuntu-latest
needs: [security-scan, test, infrastructure-check]
if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master' || github.event_name == 'workflow_dispatch'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Install Poetry
uses: snok/install-poetry@v1
- name: Install dependencies
run: poetry install --only=main
- name: Build package
run: |
poetry build
ls -la dist/
- name: Create deployment package
run: |
mkdir -p deployment-package
# Copy source code
cp -r src/ deployment-package/
# Copy configuration
cp -r config/ deployment-package/
# Copy infrastructure
cp -r infra/ deployment-package/
# Copy requirements
poetry export -f requirements.txt --output deployment-package/requirements.txt
# Create version file
echo "BUILD_ID=${{ github.run_number }}" > deployment-package/version.txt
echo "COMMIT_SHA=${{ github.sha }}" >> deployment-package/version.txt
echo "BUILD_DATE=$(date -u +%Y-%m-%dT%H:%M:%SZ)" >> deployment-package/version.txt
- name: Upload build artifacts
uses: actions/upload-artifact@v3
with:
name: deployment-package
path: deployment-package/
retention-days: 30
# Deploy to development
deploy-dev:
name: Deploy to Development
runs-on: ubuntu-latest
needs: build
if: github.ref == 'refs/heads/develop' || (github.event_name == 'workflow_dispatch' && github.event.inputs.environment == 'dev')
environment: development
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Download build artifacts
uses: actions/download-artifact@v3
with:
name: deployment-package
path: deployment-package/
- name: Set up AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_DEFAULT_REGION }}
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Install CDK
run: npm install -g aws-cdk@2.100.0
- name: Install Poetry
uses: snok/install-poetry@v1
- name: Install dependencies
run: poetry install
- name: Deploy infrastructure
run: |
cd infra
poetry run cdk deploy ResourceForecaster-Dev \
--require-approval never \
--context environment=dev \
--outputs-file dev-outputs.json
- name: Run post-deployment tests
run: |
poetry run pytest tests/integration/ \
--environment=dev \
--tb=short \
-v
- name: Upload deployment outputs
uses: actions/upload-artifact@v3
with:
name: dev-deployment-outputs
path: infra/dev-outputs.json
# Deploy to staging
deploy-staging:
name: Deploy to Staging
runs-on: ubuntu-latest
needs: [build, deploy-dev]
if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master' || (github.event_name == 'workflow_dispatch' && github.event.inputs.environment == 'staging')
environment: staging
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Download build artifacts
uses: actions/download-artifact@v3
with:
name: deployment-package
path: deployment-package/
- name: Set up AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_STAGING }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_STAGING }}
aws-region: ${{ env.AWS_DEFAULT_REGION }}
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Install CDK
run: npm install -g aws-cdk@2.100.0
- name: Install Poetry
uses: snok/install-poetry@v1
- name: Install dependencies
run: poetry install
- name: Deploy infrastructure
run: |
cd infra
poetry run cdk deploy ResourceForecaster-Staging \
--require-approval never \
--context environment=staging \
--outputs-file staging-outputs.json
- name: Run integration tests
run: |
poetry run pytest tests/integration/ \
--environment=staging \
--tb=short \
-v
- name: Run performance tests
run: |
poetry run pytest tests/performance/ \
--environment=staging \
--tb=short \
-v
- name: Upload deployment outputs
uses: actions/upload-artifact@v3
with:
name: staging-deployment-outputs
path: infra/staging-outputs.json
# Deploy to production (manual approval required)
deploy-prod:
name: Deploy to Production
runs-on: ubuntu-latest
needs: [build, deploy-staging]
if: github.event_name == 'workflow_dispatch' && github.event.inputs.environment == 'prod'
environment: production
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Download build artifacts
uses: actions/download-artifact@v3
with:
name: deployment-package
path: deployment-package/
- name: Set up AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_PROD }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_PROD }}
aws-region: ${{ env.AWS_DEFAULT_REGION }}
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Install CDK
run: npm install -g aws-cdk@2.100.0
- name: Install Poetry
uses: snok/install-poetry@v1
- name: Install dependencies
run: poetry install
- name: Create backup snapshot
run: |
echo "Creating backup of current production state..."
# Add backup logic here
- name: Deploy infrastructure
run: |
cd infra
poetry run cdk deploy ResourceForecaster-Prod \
--require-approval never \
--context environment=prod \
--outputs-file prod-outputs.json
- name: Run smoke tests
run: |
poetry run pytest tests/smoke/ \
--environment=prod \
--tb=short \
-v
- name: Upload deployment outputs
uses: actions/upload-artifact@v3
with:
name: prod-deployment-outputs
path: infra/prod-outputs.json
- name: Notify deployment success
run: |
echo "✅ Production deployment completed successfully!"
echo "Build ID: ${{ github.run_number }}"
echo "Commit: ${{ github.sha }}"
# Cleanup and notifications
cleanup:
name: Cleanup & Notifications
runs-on: ubuntu-latest
needs: [deploy-dev, deploy-staging, deploy-prod]
if: always()
steps:
- name: Clean up old artifacts
run: |
echo "Cleaning up old build artifacts and deployments..."
- name: Send notifications
run: |
echo "Sending deployment notifications..."
# Add Slack/Teams/Email notifications here
- name: Update deployment status
run: |
echo "Updating deployment dashboard/status page..."