Align CI/CD with shared infra/ci-workflows and drop compose-based CI.
CI / changes (push) Successful in 1m4s
CI / backend (push) Failing after 0s
CI / frontend (push) Failing after 0s

Replace hand-rolled jobs with @v1 callers, capped bake/migrate CD, and remove the docker-compose.ci.yml bridge workaround.
This commit is contained in:
2026-10-06 19:10:58 +08:00
parent 1124c1f079
commit 5a175e3fba
12 changed files with 397 additions and 197 deletions
+121 -52
View File
@@ -1,9 +1,13 @@
# Build images, migrate, deploy on version tags.
# Requires act_runner with docker, curl, ssh, scp, bash and label ubuntu-latest.
# Build images (capped bake), migrate, deploy on version tags.
# Copy of docs/CICD/templates/workflows/cd.yml adapted for this repo's paths.
#
# ubuntu-latest is a registered runner label, not a GitHub-hosted VM.
# Requires Docker-mode runner (host docker socket), curl, ssh, scp, bash.
# GITEA_TOKEN cannot push images — REGISTRY_TOKEN must be a PAT.
#
# Secrets: REGISTRY_USERNAME, REGISTRY_TOKEN, PG_CONNECTION_DDL,
# DEPLOY_HOST, DEPLOY_USER, DEPLOY_SSH_KEY
# Variables: REGISTRY (optional; defaults below)
# Variables: REGISTRY (optional), MIGRATE_DOCKER_NETWORK (default shared-services)
name: CD
on:
@@ -47,47 +51,53 @@ jobs:
| docker login "${REGISTRY%%/*}" \
-u "${{ secrets.REGISTRY_USERNAME }}" --password-stdin
- name: Build and push vigilcare-records-api
- name: Bake and push images
env:
TAG: ${{ steps.meta.outputs.tag }}
run: |
docker build -f VigilCareRecordsAPI/Dockerfile \
-t "${REGISTRY}/vigilcare-records-api:${{ steps.meta.outputs.tag }}" \
-t "${REGISTRY}/vigilcare-records-api:latest" .
docker push "${REGISTRY}/vigilcare-records-api:${{ steps.meta.outputs.tag }}"
docker push "${REGISTRY}/vigilcare-records-api:latest"
# Cap buildkitd cgroup, then bake one target at a time —
# BUILDKIT_STEP_MAX_PARALLELISM=1 alone does not leave Gitea a core.
bash scripts/cap-buildx-builder.sh vigilcare-records-builder
bake_one() {
local target="$1"
shift
echo "=== bake ${target} ==="
BUILDX_NO_DEFAULT_ATTESTATIONS=1 docker buildx bake \
--progress=plain \
"$target" \
"$@" \
-f docker-bake.hcl
}
bake_one api --push --set api.output=type=image,push=true
sleep 8
bake_one ui --push --set ui.output=type=image,push=true
sleep 8
bake_one migrate --set migrate.output=type=docker
# scratch migrate-bundle needs an explicit command (or image CMD).
cid=$(docker create vigilcare-records-migrate-bundle:local /out/migrate-api)
docker cp "$cid:/out/migrate-api" ./migrate-api
docker rm "$cid"
chmod +x ./migrate-api
- name: Build and push vigilcare-records-dashboard
# Context is vigilcare-records-web/ — package.json and nginx.conf live there.
run: |
docker build -f vigilcare-records-web/Dockerfile \
-t "${REGISTRY}/vigilcare-records-dashboard:${{ steps.meta.outputs.tag }}" \
-t "${REGISTRY}/vigilcare-records-dashboard:latest" vigilcare-records-web
docker push "${REGISTRY}/vigilcare-records-dashboard:${{ steps.meta.outputs.tag }}"
docker push "${REGISTRY}/vigilcare-records-dashboard:latest"
- name: Upload migration bundle
uses: actions/upload-artifact@v4
with:
name: migrate-api
path: migrate-api
if-no-files-found: error
migrate:
needs: build-and-push
runs-on: ubuntu-latest
# Checkout must run on the job host. Build the EF bundle via Dockerfile
# --target migrate (context upload), not docker run -v — under act_runner
# bind mounts resolve on the Docker host, not the job workspace.
# Postgres lives on the deploy host's shared-services network and is not
# reachable from act_runner, so the bundle is copied there and executed in
# a one-shot container joined to that network (Host=postgres resolves).
# NOTE: AppBootstrapHostedService also runs db.Database.MigrateAsync() after
# Kestrel listens, so this job is a defense-in-depth pre-deploy step using a
# DDL-privileged credential the API container never sees, not the only
# migration path.
# Bundle is an artefact from build-and-push — never docker build here.
# Bind mounts resolve on the Docker host, not the job workspace.
# Postgres is on the deploy host; copy the binary there.
steps:
- uses: actions/checkout@v4
- name: Build migration bundle
run: |
docker build -f VigilCareRecordsAPI/Dockerfile --target migrate \
-t vigilcare-records-migrate-bundle:local .
cid=$(docker create vigilcare-records-migrate-bundle:local)
docker cp "$cid:/out/migrate-api" ./migrate-api
docker rm "$cid"
chmod +x ./migrate-api
- name: Download migration bundle
uses: actions/download-artifact@v4
with:
name: migrate-api
path: .
- name: Configure SSH
run: |
@@ -95,34 +105,52 @@ jobs:
echo "${{ secrets.DEPLOY_SSH_KEY }}" > ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_ed25519
ssh-keyscan -H "${{ secrets.DEPLOY_HOST }}" >> ~/.ssh/known_hosts
chmod +x ./migrate-api
# Runs while the previous release is still serving traffic, so every
# migration must be backwards-compatible with the outgoing image
# (expand-then-contract). Connection strings contain `;` / spaces
# ("SSL Mode=...") — never pass them as a bare ssh remote argv; the
# remote shell splits on `;` and drops the variable (set -u → unbound).
# Expand-then-contract: migrate while the previous release still serves.
# PG_CONNECTION_DDL must be Npgsql key=value form, not a URI. No wrapping quotes.
- name: Apply migrations
env:
PG_CONNECTION_DDL: ${{ secrets.PG_CONNECTION_DDL }}
MIGRATE_DOCKER_NETWORK: ${{ vars.MIGRATE_DOCKER_NETWORK }}
run: |
REMOTE="${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}"
NETWORK="${MIGRATE_DOCKER_NETWORK:-shared-services}"
scp -i ~/.ssh/id_ed25519 ./migrate-api \
"$REMOTE:/tmp/vigilcare-records-migrate-api"
umask 077
printf '%s\n' "$PG_CONNECTION_DDL" > /tmp/vigilcare-records-pg-ddl
printf '%s' "$PG_CONNECTION_DDL" > /tmp/vigilcare-records-pg-ddl
scp -i ~/.ssh/id_ed25519 /tmp/vigilcare-records-pg-ddl \
"$REMOTE:/tmp/vigilcare-records-pg-ddl"
rm -f /tmp/vigilcare-records-pg-ddl
ssh -i ~/.ssh/id_ed25519 "$REMOTE" bash -euo pipefail <<'EOF'
ssh -i ~/.ssh/id_ed25519 "$REMOTE" \
NETWORK="$NETWORK" \
bash -euo pipefail <<'EOF'
chmod +x /tmp/vigilcare-records-migrate-api
chmod 600 /tmp/vigilcare-records-pg-ddl
PG_CONNECTION_DDL="$(cat /tmp/vigilcare-records-pg-ddl)"
PG_CONNECTION_DDL="$(tr -d '\r\n' < /tmp/vigilcare-records-pg-ddl)"
PG_CONNECTION_DDL="${PG_CONNECTION_DDL#\"}"
PG_CONNECTION_DDL="${PG_CONNECTION_DDL%\"}"
PG_CONNECTION_DDL="${PG_CONNECTION_DDL#\'}"
PG_CONNECTION_DDL="${PG_CONNECTION_DDL%\'}"
if [[ "$PG_CONNECTION_DDL" == postgresql://* || "$PG_CONNECTION_DDL" == postgres://* ]]; then
echo "PG_CONNECTION_DDL must be Npgsql key=value form, not a URI." >&2
exit 1
fi
if [[ "$PG_CONNECTION_DDL" != Host=* && "$PG_CONNECTION_DDL" != host=* ]]; then
PG_CONNECTION_DDL="Host=${PG_CONNECTION_DDL}"
fi
printf '%s' "$PG_CONNECTION_DDL" > /tmp/vigilcare-records-pg-ddl
docker run --rm \
--network shared-services \
--network "${NETWORK}" \
-v /tmp/vigilcare-records-migrate-api:/migrate-api:ro \
--entrypoint /migrate-api \
-v /tmp/vigilcare-records-pg-ddl:/run/pg-ddl:ro \
--entrypoint sh \
mcr.microsoft.com/dotnet/runtime-deps:8.0 \
--connection "$PG_CONNECTION_DDL"
-c 'exec /migrate-api --connection "$(tr -d "\r\n" < /run/pg-ddl)"'
rm -f /tmp/vigilcare-records-migrate-api /tmp/vigilcare-records-pg-ddl
EOF
@@ -147,12 +175,43 @@ jobs:
- name: Deploy
env:
IMAGE_TAG: ${{ needs.build-and-push.outputs.tag }}
REGISTRY_USERNAME: ${{ secrets.REGISTRY_USERNAME }}
REGISTRY_TOKEN: ${{ secrets.REGISTRY_TOKEN }}
run: |
if [ -n "${{ vars.REGISTRY }}" ]; then
REGISTRY="${{ vars.REGISTRY }}"
else
REGISTRY="git.vectur45.com/trent/vigilcare-records"
fi
REGISTRY_HOST="${REGISTRY%%/*}"
ssh -i ~/.ssh/id_ed25519 \
"${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}" \
IMAGE_TAG="$IMAGE_TAG" bash -euo pipefail <<'EOF'
IMAGE_TAG="$IMAGE_TAG" \
REGISTRY_HOST="$REGISTRY_HOST" \
REGISTRY_USERNAME="$REGISTRY_USERNAME" \
REGISTRY_TOKEN="$REGISTRY_TOKEN" \
bash -euo pipefail <<'EOF'
cd /opt/vigilcare-records
echo "${REGISTRY_TOKEN}" | docker login "${REGISTRY_HOST}" \
-u "${REGISTRY_USERNAME}" --password-stdin
env_val() { sed -n "s/^${1}=//p" .env | tail -n1 | tr -d '\r'; }
strip_quotes() {
local v="$1"
v="${v#\"}"; v="${v%\"}"; v="${v#\'}"; v="${v%\'}"
printf '%s' "$v"
}
PG_CONNECTION="$(strip_quotes "$(env_val PG_CONNECTION)")"
if [[ -z "$PG_CONNECTION" ]]; then
echo "PG_CONNECTION is empty in /opt/vigilcare-records/.env" >&2
exit 1
fi
if [[ "$PG_CONNECTION" != Host=* && "$PG_CONNECTION" != host=* ]]; then
echo "PG_CONNECTION must start with Host= (Npgsql key=value form)." >&2
exit 1
fi
# Record the currently deployed tag so a rollback has a target.
grep '^IMAGE_TAG=' .env > .env.previous || true
@@ -171,6 +230,7 @@ jobs:
exit 1
fi
docker image prune -f
docker compose -f docker-compose.prod.yml --env-file .env ps -a
EOF
- name: Smoke test
@@ -185,15 +245,24 @@ jobs:
DASHBOARD_PORT="$(env_val DASHBOARD_PORT)"; DASHBOARD_PORT="${DASHBOARD_PORT:-8089}"
for i in $(seq 1 30); do
if curl -fsS "http://localhost:${API_PORT}/health/ready" >/dev/null; then
echo "Ready check passed."
set +e
http_code=$(curl -s -o /dev/null -w '%{http_code}' --connect-timeout 2 \
"http://localhost:${API_PORT}/health/ready" 2>/dev/null)
curl_exit=$?
set -e
if [ "$curl_exit" -ne 0 ] || [ -z "$http_code" ]; then
http_code="000"
fi
if [ "$http_code" = "200" ]; then
echo "Ready check passed (attempt ${i})."
curl -fsS "http://localhost:${DASHBOARD_PORT}/" >/dev/null && echo "Dashboard serving."
exit 0
fi
echo "Attempt ${i}/30: HTTP ${http_code}"
sleep 5
done
echo "Ready check never passed — dumping API logs:"
docker compose -f docker-compose.prod.yml --env-file .env logs --tail 100 api
docker compose -f docker-compose.prod.yml --env-file .env logs --tail 150 api || true
exit 1
EOF