-
Notifications
You must be signed in to change notification settings - Fork 0
439 lines (380 loc) · 13.9 KB
/
deploy-ecr.yaml
File metadata and controls
439 lines (380 loc) · 13.9 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
name: Deploy (ECR)
on:
push:
branches: [main]
pull_request:
branches: [main]
workflow_dispatch:
inputs:
image_tag:
description: "Image tag to push/deploy (default: git sha)"
required: false
type: string
permissions:
contents: read
deployments: write
pull-requests: write
concurrency:
group: deploy-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
# In PRs plan only if tofu files changed
check-infra-changes:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
outputs:
changed: ${{ steps.changes.outputs.tofu }}
steps:
- name: Checkout
uses: actions/checkout@v6
- name: Check for tofu changes
id: changes
uses: dorny/paths-filter@v3
with:
filters: |
tofu:
- 'tofu/**'
plan-dc:
needs: check-infra-changes
if: needs.check-infra-changes.outputs.changed == 'true'
uses: ./.github/workflows/plan.yaml
with:
environment: dev-dc
config: dev-dc
image_tag: ${{ github.sha }}
secrets: inherit
plan-co:
needs: check-infra-changes
if: needs.check-infra-changes.outputs.changed == 'true'
uses: ./.github/workflows/plan.yaml
with:
environment: dev-co
config: dev-co
image_tag: ${{ github.sha }}
secrets: inherit
plan-comment:
needs: [plan-dc, plan-co]
if: always() && (needs.plan-dc.outputs.plan || needs.plan-co.outputs.plan)
runs-on: ubuntu-latest
steps:
- name: Post plan to PR
uses: actions/github-script@v8
with:
script: |
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const botComment = comments.find(comment => {
return comment.user.type === 'Bot' && comment.body.includes('## Plan output');
});
let output = '';
const dcPlan = `${{ needs.plan-dc.outputs.plan }}`;
const coPlan = `${{ needs.plan-co.outputs.plan }}`;
if (dcPlan) {
output += `## Plan output — dev-dc\n\n\`\`\`\n${dcPlan}\n\`\`\`\n\n`;
}
if (coPlan) {
output += `## Plan output — dev-co\n\n\`\`\`\n${coPlan}\n\`\`\``;
}
if (botComment) {
github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: output,
});
} else {
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: output,
});
}
# Build and deploy only on push to main or manual trigger, not on PRs
build:
if: github.event_name != 'pull_request'
runs-on: ubuntu-latest
outputs:
image_tag: ${{ steps.tag.outputs.tag }}
steps:
- name: Checkout
uses: actions/checkout@v6
- name: Compute image tag
id: tag
run: |
if [ -n "${{ inputs.image_tag }}" ]; then
echo "tag=${{ inputs.image_tag }}" >> "$GITHUB_OUTPUT"
else
echo "tag=${GITHUB_SHA}" >> "$GITHUB_OUTPUT"
fi
- name: Setup .NET
uses: actions/setup-dotnet@v5.2.0
with:
dotnet-version: "10.0.200"
- name: Checkout state-connector
uses: actions/checkout@v6
with:
repository: codeforamerica/sebt-self-service-portal-state-connector
token: ${{ secrets.SEBT_CONNECTOR_REPOSITORY_PAT }}
path: state-connector
- name: Checkout dc-connector
uses: actions/checkout@v6
with:
repository: codeforamerica/sebt-self-service-portal-dc-connector
token: ${{ secrets.SEBT_CONNECTOR_REPOSITORY_PAT }}
path: dc-connector
- name: Checkout co-connector
uses: actions/checkout@v6
with:
repository: codeforamerica/sebt-self-service-portal-co-connector
token: ${{ secrets.SEBT_CONNECTOR_REPOSITORY_PAT }}
path: co-connector
- name: Build plugin interfaces (NuGet package)
run: |
dotnet build state-connector/src/SEBT.Portal.StatesPlugins.Interfaces/SEBT.Portal.StatesPlugins.Interfaces.csproj \
-c Release
- name: Publish DC plugin
run: |
dotnet publish dc-connector/src/SEBT.Portal.StatePlugins.DC/SEBT.Portal.StatePlugins.DC.csproj \
-c Release \
-p:CopyPlugins=false
- name: Publish CO plugin
run: |
dotnet publish co-connector/src/SEBT.Portal.StatePlugins.CO/SEBT.Portal.StatePlugins.CO.csproj \
-c Release \
-p:CopyPlugins=false
- name: Stage plugin artifacts for Docker build
run: |
mkdir -p nuget-store
cp -r ~/nuget-store/* nuget-store/
mkdir -p src/SEBT.Portal.Api/plugins-dc
cp dc-connector/src/SEBT.Portal.StatePlugins.DC/bin/Release/net10.0/publish/*.dll \
src/SEBT.Portal.Api/plugins-dc/
mkdir -p src/SEBT.Portal.Api/plugins-co
cp co-connector/src/SEBT.Portal.StatePlugins.CO/bin/Release/net10.0/publish/*.dll \
src/SEBT.Portal.Api/plugins-co/
- name: Build API image
run: |
docker build \
--platform linux/amd64 \
-f src/SEBT.Portal.Api/Dockerfile \
-t sebt-portal-api:${{ steps.tag.outputs.tag }} \
.
- name: Build Web DC image
run: |
docker build \
--platform linux/amd64 \
-f src/SEBT.Portal.Web/Dockerfile \
--build-arg STATE=dc \
-t sebt-portal-web-dc:${{ steps.tag.outputs.tag }} \
.
- name: Build Web CO image
run: |
docker build \
--platform linux/amd64 \
-f src/SEBT.Portal.Web/Dockerfile \
--build-arg STATE=co \
-t sebt-portal-web-co:${{ steps.tag.outputs.tag }} \
.
- name: Save Docker images
run: |
docker save \
sebt-portal-api:${{ steps.tag.outputs.tag }} \
sebt-portal-web-dc:${{ steps.tag.outputs.tag }} \
sebt-portal-web-co:${{ steps.tag.outputs.tag }} \
-o /tmp/docker-images.tar
- name: Upload Docker images
uses: actions/upload-artifact@v6
with:
name: docker-images
path: /tmp/docker-images.tar
retention-days: 1
deploy-dc:
if: github.event_name != 'pull_request'
needs: build
runs-on: ubuntu-latest
environment: dev-dc
outputs:
web_url: ${{ steps.deploy-url.outputs.url }}
env:
AWS_REGION: us-east-1
TF_VAR_domain: ${{ vars.DOMAIN }}
TF_VAR_sender_email: ${{ vars.SENDER_EMAIL }}
TF_VAR_vpc_cidr: ${{ vars.VPC_CIDR }}
TF_VAR_private_subnets: ${{ vars.PRIVATE_SUBNETS }}
TF_VAR_public_subnets: ${{ vars.PUBLIC_SUBNETS }}
steps:
- name: Checkout
uses: actions/checkout@v6
- name: Download Docker images
uses: actions/download-artifact@v7
with:
name: docker-images
path: /tmp
- name: Load Docker images
run: docker load -i /tmp/docker-images.tar
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v6
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Push images to ECR
run: |
IMAGE_TAG="${{ needs.build.outputs.image_tag }}"
API_REPO="${{ secrets.ECR_API_REPOSITORY_URL }}"
WEB_REPO="${{ secrets.ECR_WEB_REPOSITORY_URL }}"
docker tag sebt-portal-api:${IMAGE_TAG} ${API_REPO}:${IMAGE_TAG}
docker tag sebt-portal-web-dc:${IMAGE_TAG} ${WEB_REPO}:${IMAGE_TAG}
docker push ${API_REPO}:${IMAGE_TAG}
docker push ${WEB_REPO}:${IMAGE_TAG}
- name: Install OpenTofu
uses: opentofu/setup-opentofu@v1
with:
tofu_version: "1.10.0"
- name: OpenTofu Init
working-directory: tofu/config/dev-dc
run: tofu init -upgrade
- name: OpenTofu Apply
working-directory: tofu/config/dev-dc
run: |
tofu apply -auto-approve \
-var="image_tag=${{ needs.build.outputs.image_tag }}"
- name: Get deployment URL
id: deploy-url
working-directory: tofu/config/dev-dc
run: |
WEB_URL=$(tofu output -raw web_endpoint_url 2>/dev/null || echo "")
echo "url=$WEB_URL" >> "$GITHUB_OUTPUT"
echo "Deployment URL: $WEB_URL"
deploy-co:
if: github.event_name != 'pull_request'
needs: build
runs-on: ubuntu-latest
environment: dev-co
outputs:
web_url: ${{ steps.deploy-url.outputs.url }}
env:
AWS_REGION: us-east-1
TF_VAR_domain: ${{ vars.DOMAIN }}
TF_VAR_sender_email: ${{ vars.SENDER_EMAIL }}
TF_VAR_vpc_cidr: ${{ vars.VPC_CIDR }}
TF_VAR_private_subnets: ${{ vars.PRIVATE_SUBNETS }}
TF_VAR_public_subnets: ${{ vars.PUBLIC_SUBNETS }}
steps:
- name: Checkout
uses: actions/checkout@v6
- name: Download Docker images
uses: actions/download-artifact@v7
with:
name: docker-images
path: /tmp
- name: Load Docker images
run: docker load -i /tmp/docker-images.tar
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v6
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Push images to ECR
run: |
IMAGE_TAG="${{ needs.build.outputs.image_tag }}"
API_REPO="${{ secrets.ECR_API_REPOSITORY_URL }}"
WEB_REPO="${{ secrets.ECR_WEB_REPOSITORY_URL }}"
docker tag sebt-portal-api:${IMAGE_TAG} ${API_REPO}:${IMAGE_TAG}
docker tag sebt-portal-web-co:${IMAGE_TAG} ${WEB_REPO}:${IMAGE_TAG}
docker push ${API_REPO}:${IMAGE_TAG}
docker push ${WEB_REPO}:${IMAGE_TAG}
- name: Install OpenTofu
uses: opentofu/setup-opentofu@v1
with:
tofu_version: "1.10.0"
- name: OpenTofu Init
working-directory: tofu/config/dev-co
run: tofu init -upgrade
- name: OpenTofu Apply
working-directory: tofu/config/dev-co
run: |
tofu apply -auto-approve \
-var="image_tag=${{ needs.build.outputs.image_tag }}"
- name: Get deployment URL
id: deploy-url
working-directory: tofu/config/dev-co
run: |
WEB_URL=$(tofu output -raw web_endpoint_url 2>/dev/null || echo "")
echo "url=$WEB_URL" >> "$GITHUB_OUTPUT"
echo "Deployment URL: $WEB_URL"
create-deployment-record:
needs: [deploy-dc, deploy-co]
if: always() && (needs.deploy-dc.result == 'success' || needs.deploy-co.result == 'success')
runs-on: ubuntu-latest
permissions:
deployments: write
contents: read
steps:
- name: Create GitHub deployment for DC
if: needs.deploy-dc.result == 'success'
uses: actions/github-script@v8
with:
script: |
const rawUrl = '${{ needs.deploy-dc.outputs.web_url }}';
const webUrl = rawUrl && !rawUrl.startsWith('http') ? `https://${rawUrl}` : rawUrl;
const ref = context.sha;
const deployment = await github.rest.repos.createDeployment({
owner: context.repo.owner,
repo: context.repo.repo,
ref,
environment: 'dev-dc',
auto_merge: false,
required_contexts: [],
description: `Deploy dev-dc (${ref.slice(0, 7)})`
});
const deploymentId = deployment.data.id;
if (deploymentId) {
await github.rest.repos.createDeploymentStatus({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: deploymentId,
state: 'success',
environment_url: webUrl || undefined,
description: 'Deployment finished successfully'
});
}
- name: Create GitHub deployment for CO
if: needs.deploy-co.result == 'success'
uses: actions/github-script@v8
with:
script: |
const rawUrl = '${{ needs.deploy-co.outputs.web_url }}';
const webUrl = rawUrl && !rawUrl.startsWith('http') ? `https://${rawUrl}` : rawUrl;
const ref = context.sha;
const deployment = await github.rest.repos.createDeployment({
owner: context.repo.owner,
repo: context.repo.repo,
ref,
environment: 'dev-co',
auto_merge: false,
required_contexts: [],
description: `Deploy dev-co (${ref.slice(0, 7)})`
});
const deploymentId = deployment.data.id;
if (deploymentId) {
await github.rest.repos.createDeploymentStatus({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: deploymentId,
state: 'success',
environment_url: webUrl || undefined,
description: 'Deployment finished successfully'
});
}