-
Notifications
You must be signed in to change notification settings - Fork 1
299 lines (276 loc) · 12.9 KB
/
Copy pathdeploy-images.yml
File metadata and controls
299 lines (276 loc) · 12.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
# 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
# Shared serialization group with scheduled-runs.yml (SAME group name — that is the point). A deploy
# replaces a prod container via a rolling cutover Lightsail can't connection-drain, so deploying while
# a scheduled agent run is in flight severs its long-lived lik-mcp MCP session and fails the sync
# mid-run. Sharing the group makes a deploy wait for any running scan to finish first (and a scan wait
# for a running deploy). Conservative: this also serializes lik-ui-only deploys, which don't affect a
# sync — kept simple over a per-service group. cancel-in-progress: false so a queued deploy waits
# rather than being dropped.
concurrency:
group: lik-prod-mutations
cancel-in-progress: false
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 }}
# Boot the freshly built image and confirm it serves its health endpoint BEFORE pushing.
# This catches an import/boot break — like the mcp-2.0 crash that removed a module imported
# at startup — in CI, so a broken image never consumes a Lightsail deploy/rollback cycle.
# Neither app needs a reachable DB to answer its health probe (the connection pool opens
# lazily in the background), so a standalone container is sufficient — no DB sidecar.
- name: Smoke boot
if: steps.gate.outputs.skip != 'true'
run: |
set -euo pipefail
name="${{ matrix.name }}"
img="${name}:${{ github.sha }}"
cname="smoke-${name}"
if [ "$name" = "lik-mcp" ]; then
# Boot in the prod auth mode the deploy uses; dummy OAuth values satisfy the
# fail-closed startup guard. /mcp answers 401 under auth — the Lightsail health
# check treats 200-499 as alive (see infra/lik_mcp.tf), so mirror that range.
docker run -d --name "$cname" -p 8000:8000 \
-e LIK_OAUTH_CLIENT_ID=smoke \
-e LIK_RESOURCE_SERVER_URL=https://mcp.example.test/mcp \
-e LIK_HTTP_ALLOWED_HOSTS="localhost,localhost:*,127.0.0.1,127.0.0.1:*,0.0.0.0,0.0.0.0:*" \
"$img"
url="http://127.0.0.1:8000/mcp"; lo=200; hi=499
else
docker run -d --name "$cname" -p 8001:8001 \
-e LIK_UI_ENV=local \
-e LIK_UI_HTTP_ALLOWED_HOSTS="localhost,localhost:*,127.0.0.1,127.0.0.1:*" \
"$img"
url="http://127.0.0.1:8001/healthz"; lo=200; hi=200
fi
for i in $(seq 1 30); do
# `|| code=000` (assignment, not echo) keeps $code a clean 3-digit value even when
# curl hits connection-refused during the boot window — avoids a set -e arithmetic abort.
code=$(curl -s -o /dev/null -w '%{http_code}' "$url" 2>/dev/null) || code=000
if [ "$code" -ge "$lo" ] && [ "$code" -le "$hi" ]; then
echo "healthy: $url -> $code (after ${i} probe(s))"
exit 0
fi
sleep 2
done
echo "::error::${name} image did not become healthy ($url, last HTTP code=$code) — see container logs below"
docker logs "$cname" 2>&1 | tail -50
exit 1
- name: Smoke boot teardown
if: always() && steps.gate.outputs.skip != 'true'
run: docker rm -f "smoke-${{ matrix.name }}" >/dev/null 2>&1 || true
- 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 }}
# Custom-domain URLs come from the prod GitHub environment (Settings → Environments →
# prod → Variables). They MUST be set, or the empty Terraform default detaches the live
# domain — see infra/lik_ui.tf. infra/tf.sh supplies the same values for local applies.
MCP_CUSTOM_DOMAIN_URL: ${{ vars.MCP_CUSTOM_DOMAIN_URL }}
UI_CUSTOM_DOMAIN_URL: ${{ vars.UI_CUSTOM_DOMAIN_URL }}
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: Require custom-domain URLs
run: |
# Unset GitHub vars resolve to "", which Terraform reads as "detach the live domain".
# Fail loud here rather than let a routine image swap silently drop the custom domain.
: "${MCP_CUSTOM_DOMAIN_URL:?set vars.MCP_CUSTOM_DOMAIN_URL on the prod GitHub environment}"
: "${UI_CUSTOM_DOMAIN_URL:?set vars.UI_CUSTOM_DOMAIN_URL on the prod GitHub environment}"
- 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