-
Notifications
You must be signed in to change notification settings - Fork 1
233 lines (214 loc) · 9.13 KB
/
Copy pathdeploy-images.yml
File metadata and controls
233 lines (214 loc) · 9.13 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
# Build each service image, push it to its Lightsail registry, then deploy it.
# The build/push runs in CI so the large image upload does not run from a maintainer's
# (slow) uplink. The `apply` job then runs `terraform apply` — but ONLY when the plan is a
# clean image swap (`Plan: N to add, 0 to change, N to destroy.`). Any other plan (config
# drift, anything with "to change") is left for the maintainer to review and apply locally
# with `./tf.sh apply` (the step summary echoes the exact -var image refs to use).
#
# Prerequisites (set once in repo Settings):
# - Variable AWS_DEPLOY_ROLE_ARN = the github_image_push_role_arn Terraform output.
# - Variable AWS_APPLY_ROLE_ARN = the github_apply_role_arn Terraform output.
# - Variable AWS_REGION = us-east-1.
# Auth is GitHub OIDC (no stored AWS keys). Both IAM roles' trust is scoped to this repo's
# `prod` environment (see infra/iam_github_oidc.tf).
name: Build and deploy images
run-name: Deploy ${{ inputs.service }} @ ${{ github.sha }} by @${{ github.actor }}
on:
workflow_dispatch:
inputs:
service:
description: Which service(s) to build and push
type: choice
options: [lik-ui, lik-mcp, both]
default: lik-ui
permissions:
id-token: write # required for OIDC
contents: read
jobs:
push:
runs-on: ubuntu-latest
# The prod environment scopes AWS_DEPLOY_ROLE_ARN / AWS_REGION (and enables optional
# deployment protection rules). Env-scoped vars only resolve when the job declares this.
environment: prod
strategy:
matrix:
include:
- name: lik-mcp
service: lik-mcp-prod
- name: lik-ui
service: lik-ui-prod
steps:
- name: Skip services not selected
id: gate
run: |
if [ "${{ inputs.service }}" != "both" ] && [ "${{ inputs.service }}" != "${{ matrix.name }}" ]; then
echo "skip=true" >> "$GITHUB_OUTPUT"
fi
- uses: actions/checkout@v6
if: steps.gate.outputs.skip != 'true'
- name: Configure AWS credentials (OIDC)
if: steps.gate.outputs.skip != 'true'
uses: aws-actions/configure-aws-credentials@v6
with:
role-to-assume: ${{ vars.AWS_DEPLOY_ROLE_ARN }}
aws-region: ${{ vars.AWS_REGION }}
- name: Install lightsailctl (required by push-container-image)
if: steps.gate.outputs.skip != 'true'
run: |
sudo curl -sL "https://s3.us-west-2.amazonaws.com/lightsailctl/latest/linux-amd64/lightsailctl" \
-o /usr/local/bin/lightsailctl
sudo chmod +x /usr/local/bin/lightsailctl
- name: Build image
if: steps.gate.outputs.skip != 'true'
run: docker build -t ${{ matrix.name }}:${{ github.sha }} ./${{ matrix.name }}
- name: Push to Lightsail registry
if: steps.gate.outputs.skip != 'true'
run: |
out=$(aws lightsail push-container-image \
--region "${{ vars.AWS_REGION }}" \
--service-name "${{ matrix.service }}" \
--label app \
--image "${{ matrix.name }}:${{ github.sha }}")
echo "$out"
# push-container-image prints: Refer to this image as ":svc.app.N" in deployments.
ref=$(echo "$out" | grep -oE '":[^"]+"' | tr -d '"' | tail -1)
# Hand the ref to the apply job via an artifact (matrix job outputs collide).
mkdir -p image-ref
printf '%s' "$ref" > "image-ref/${{ matrix.name }}.txt"
{
echo "### ${{ matrix.name }} pushed"
echo ""
echo "Image ref for \`terraform apply\`:"
echo ""
echo '```'
echo "$ref"
echo '```'
} >> "$GITHUB_STEP_SUMMARY"
- name: Upload image ref
if: steps.gate.outputs.skip != 'true'
uses: actions/upload-artifact@v7
with:
name: image-ref-${{ matrix.name }}
path: image-ref/${{ matrix.name }}.txt
if-no-files-found: error
# Deploy the pushed image(s) — but auto-apply only for a routine image swap. The gate is
# the plan summary: a per-service deployment_version replacement reads as "N to add,
# 0 to change, N to destroy" (see infra/lik_mcp.tf / lik_ui.tf). Anything else is left
# for the maintainer to review and apply locally.
apply:
needs: push
runs-on: ubuntu-latest
environment: prod
env:
AWS_REGION: ${{ vars.AWS_REGION }}
# Single deployment environment, so the custom-domain URLs are hardcoded here (they
# must be passed, or the empty default detaches the live domain — see infra/lik_ui.tf).
MCP_CUSTOM_DOMAIN_URL: https://mcp.lik.navapbc.com
UI_CUSTOM_DOMAIN_URL: https://ui.lik.navapbc.com
steps:
- uses: actions/checkout@v6
- name: Show deployed commit
run: |
{
echo "### Commit"
echo ""
echo "\`${{ github.sha }}\`"
echo ""
git log -1 --pretty='format:%s%n%n%b' "${{ github.sha }}"
} >> "$GITHUB_STEP_SUMMARY"
- name: Configure AWS credentials (OIDC)
uses: aws-actions/configure-aws-credentials@v6
with:
role-to-assume: ${{ vars.AWS_APPLY_ROLE_ARN }}
aws-region: ${{ vars.AWS_REGION }}
- uses: hashicorp/setup-terraform@v3
with:
terraform_version: "1.10.5"
terraform_wrapper: false
- name: Download image refs
uses: actions/download-artifact@v8
with:
pattern: image-ref-*
path: image-refs
merge-multiple: true
- name: Resolve image refs (built this run, else currently deployed)
run: |
set -euo pipefail
# For each service: use the ref just pushed (artifact) if present, otherwise read
# the currently-deployed ref from Lightsail. Both must be non-empty — passing an
# empty image var would DESTROY that service's deployment (count guard).
resolve() {
local name="$1" svc="$2" file ref
file="image-refs/${name}.txt"
if [ -f "$file" ]; then
ref=$(cat "$file")
else
ref=$(aws lightsail get-container-services --region "$AWS_REGION" \
--service-name "$svc" \
--query "containerServices[0].currentDeployment.containers.\"${name}\".image" \
--output text)
fi
if [ -z "$ref" ] || [ "$ref" = "None" ]; then
echo "::error::could not resolve image ref for ${name} (service ${svc})"
exit 1
fi
printf '%s' "$ref"
}
MCP_REF=$(resolve lik-mcp lik-mcp-prod)
UI_REF=$(resolve lik-ui lik-ui-prod)
echo "MCP_REF=$MCP_REF" >> "$GITHUB_ENV"
echo "UI_REF=$UI_REF" >> "$GITHUB_ENV"
echo "Resolved refs: lik-mcp=$MCP_REF lik-ui=$UI_REF"
- name: Terraform init
run: terraform -chdir=infra init -input=false
- name: Terraform plan
run: |
set -o pipefail # else `| tee` masks a plan failure and it leaks into the gate
terraform -chdir=infra plan -input=false -no-color -out=tfplan \
-var "lik_mcp_image=$MCP_REF" \
-var "lik_ui_image=$UI_REF" \
-var "mcp_custom_domain_url=$MCP_CUSTOM_DOMAIN_URL" \
-var "ui_custom_domain_url=$UI_CUSTOM_DOMAIN_URL" | tee plan.out
- name: Apply only a clean image swap
run: |
set -euo pipefail
summary=$(grep -E '^(Plan:|No changes\.)' plan.out | tail -1 || true)
echo "Plan summary: ${summary:-<none>}"
case "$summary" in
"Plan: 1 to add, 0 to change, 1 to destroy."|"Plan: 2 to add, 0 to change, 2 to destroy.")
echo "Clean image swap — applying."
terraform -chdir=infra apply -input=false tfplan
{
echo "### ✅ Deployed"
echo ""
echo "- lik-mcp: \`$MCP_REF\`"
echo "- lik-ui: \`$UI_REF\`"
} >> "$GITHUB_STEP_SUMMARY"
;;
"No changes."*)
{
echo "### Nothing to deploy"
echo ""
echo "Terraform reports no changes — the running deployment already matches."
} >> "$GITHUB_STEP_SUMMARY"
;;
*)
{
echo "### ⚠️ Not a clean image swap — apply skipped"
echo ""
echo "The plan below was not a routine image swap, so it was left for review:"
echo ""
echo '```'
echo "${summary:-see the Terraform plan step log}"
echo '```'
echo ""
echo "Review and apply locally with the image refs from this run:"
echo ""
echo '```'
echo "cd infra && ./tf.sh apply -var \"lik_mcp_image=$MCP_REF\" -var \"lik_ui_image=$UI_REF\""
echo '```'
} >> "$GITHUB_STEP_SUMMARY"
echo "::error::Plan is not a clean image swap — apply skipped, review and apply locally."
exit 1
;;
esac