-
Notifications
You must be signed in to change notification settings - Fork 1
197 lines (170 loc) · 6.52 KB
/
deploy-training-container.yml
File metadata and controls
197 lines (170 loc) · 6.52 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
# Deploy Training Container (Batch only)
# Triggers on changes to backend/training/ or manual dispatch
name: Deploy Training Container
on:
push:
branches: [dev, main]
paths:
- 'backend/training/**'
- 'backend/requirements-dev.txt'
- '!backend/training/**/*.md'
- '.github/workflows/deploy-training-container.yml'
workflow_dispatch:
inputs:
environment:
description: 'Target environment'
required: true
type: choice
options: [dev, prod]
permissions:
id-token: write
contents: read
checks: write
pull-requests: write
env:
AWS_REGION: us-east-1
jobs:
# =============================================================================
# Test - Run training tests before deployment
# =============================================================================
test:
name: Run Training Tests
runs-on: ubuntu-latest
env:
AWS_REGION: us-east-1
AWS_ACCESS_KEY_ID: testing
AWS_SECRET_ACCESS_KEY: testing
S3_BUCKET_DATASETS: test-datasets-bucket
S3_BUCKET_MODELS: test-models-bucket
S3_BUCKET_REPORTS: test-reports-bucket
DYNAMODB_JOBS_TABLE: test-jobs-table
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
cache: 'pip'
cache-dependency-path: |
backend/training/requirements.txt
backend/requirements-dev.txt
- name: Install dependencies
run: |
cd backend
pip install --upgrade pip
pip install -r training/requirements.txt
pip install -r requirements-dev.txt
- name: Run training unit tests
run: |
cd backend
pytest tests/training \
-v \
--tb=short \
--junitxml=test-results/training-tests.xml \
--cov=training \
--cov-report=xml:coverage/training-coverage.xml \
--cov-report=term-missing
- name: Upload test results
uses: actions/upload-artifact@v4
if: always()
with:
name: training-test-results
path: backend/test-results/training-tests.xml
retention-days: 30
- name: Upload coverage report
uses: actions/upload-artifact@v4
if: always()
with:
name: training-coverage
path: backend/coverage/training-coverage.xml
retention-days: 30
- name: Publish Test Results
uses: dorny/test-reporter@v1
if: always()
with:
name: Training Tests
path: backend/test-results/training-tests.xml
reporter: java-junit
fail-on-error: true
- name: Code Coverage Summary
uses: irongut/CodeCoverageSummary@v1.3.0
if: always()
with:
filename: backend/coverage/training-coverage.xml
badge: true
format: markdown
output: both
thresholds: '30 50'
- name: Write Coverage to Step Summary
if: always()
run: cat code-coverage-results.md >> $GITHUB_STEP_SUMMARY
- name: Add Coverage PR Comment
uses: marocchino/sticky-pull-request-comment@v2
if: github.event_name == 'pull_request'
with:
header: training-coverage
path: code-coverage-results.md
# =============================================================================
# Deploy - Only runs after tests pass
# =============================================================================
deploy:
name: Build & Push to ${{ github.ref == 'refs/heads/main' && 'prod' || github.event.inputs.environment || 'dev' }}
runs-on: ubuntu-latest
needs: test
environment: ${{ github.ref == 'refs/heads/main' && 'prod' || '' }}
env:
ENVIRONMENT: ${{ github.ref == 'refs/heads/main' && 'prod' || github.event.inputs.environment || 'dev' }}
steps:
- uses: actions/checkout@v4
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_ARN }}
aws-region: ${{ env.AWS_REGION }}
- name: Login to Amazon ECR
uses: aws-actions/amazon-ecr-login@v2
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: 1.9.8
terraform_wrapper: false
- name: Get ECR Repository
id: ecr
run: |
cd infrastructure/terraform
terraform init
# Try to select workspace (will fail if doesn't exist)
if ! terraform workspace select ${{ env.ENVIRONMENT }} 2>/dev/null; then
echo "❌ Workspace '${{ env.ENVIRONMENT }}' does not exist." >> $GITHUB_STEP_SUMMARY
echo "Please run the 'Deploy Infrastructure' workflow first." >> $GITHUB_STEP_SUMMARY
exit 1
fi
# Check if state has resources (infrastructure deployed)
RESOURCE_COUNT=$(terraform state list 2>/dev/null | wc -l)
if [ "$RESOURCE_COUNT" -eq "0" ]; then
echo "❌ No resources found in workspace '${{ env.ENVIRONMENT }}'." >> $GITHUB_STEP_SUMMARY
echo "Infrastructure not deployed. Run 'Deploy Infrastructure' workflow first." >> $GITHUB_STEP_SUMMARY
exit 1
fi
# Get ECR URL
ECR_URL=$(terraform output -raw ecr_repository_url 2>&1)
if [ $? -eq 0 ] && [ -n "$ECR_URL" ] && [[ ! "$ECR_URL" =~ "Warning" ]] && [[ ! "$ECR_URL" =~ "Error" ]] && [[ ! "$ECR_URL" =~ "No outputs" ]]; then
echo "ecr_url=$ECR_URL" >> $GITHUB_OUTPUT
echo "**ECR Repository:** $ECR_URL" >> $GITHUB_STEP_SUMMARY
echo "✅ ECR repository found: $ECR_URL"
else
echo "❌ Could not retrieve ECR repository URL." >> $GITHUB_STEP_SUMMARY
echo "Error output: $ECR_URL" >> $GITHUB_STEP_SUMMARY
exit 1
fi
- name: Build and Push Container
env:
ECR_REGISTRY: ${{ steps.ecr.outputs.ecr_url }}
IMAGE_TAG: ${{ env.ENVIRONMENT }}-${{ github.sha }}
run: |
cd backend/training
docker build -t $ECR_REGISTRY:latest -t $ECR_REGISTRY:$IMAGE_TAG .
docker push $ECR_REGISTRY:latest
docker push $ECR_REGISTRY:$IMAGE_TAG
echo "✅ Container pushed: $ECR_REGISTRY:latest"
echo "**Image Tag:** \`$IMAGE_TAG\`" >> $GITHUB_STEP_SUMMARY