Align CI/CD with the platform kit: reusable node-ci, job-network sidecars, and capped bake/migrate CD.
This commit is contained in:
+152
-62
@@ -1,10 +1,11 @@
|
||||
# Phase 36 Step 12 — build images, migrate, deploy on version tags.
|
||||
# Requires act_runner with docker, curl, ssh, scp, bash and label ubuntu-latest.
|
||||
# Build images, migrate, deploy on version tags.
|
||||
# Cap buildx + one bake target at a time (docs/CICD). GITEA_TOKEN cannot push —
|
||||
# REGISTRY_TOKEN must be a PAT.
|
||||
#
|
||||
# Secrets: REGISTRY_USERNAME, REGISTRY_TOKEN, PG_CONNECTION_DDL,
|
||||
# DEPLOY_HOST, DEPLOY_USER, DEPLOY_SSH_KEY
|
||||
# Variables: PROD_API_URL (e.g. https://api.vigilcare-clinical.vectur45.com),
|
||||
# REGISTRY (optional; defaults below)
|
||||
# Variables: REGISTRY (optional), PROD_API_URL (baked into dashboard),
|
||||
# MIGRATE_DOCKER_NETWORK (default shared-services)
|
||||
name: CD
|
||||
|
||||
on:
|
||||
@@ -17,7 +18,7 @@ on:
|
||||
required: false
|
||||
|
||||
env:
|
||||
REGISTRY: gitea.example.com/vigilcare
|
||||
REGISTRY: git.vectur45.com/Trent/vigilcare-clinical
|
||||
|
||||
jobs:
|
||||
build-and-push:
|
||||
@@ -48,58 +49,110 @@ jobs:
|
||||
| docker login "${REGISTRY%%/*}" \
|
||||
-u "${{ secrets.REGISTRY_USERNAME }}" --password-stdin
|
||||
|
||||
- name: Build and push clinical-api
|
||||
# Context MUST be repo root — ClinicalContracts is a sibling ProjectReference.
|
||||
- name: Bake and push images
|
||||
env:
|
||||
TAG: ${{ steps.meta.outputs.tag }}
|
||||
VITE_API_URL: ${{ vars.PROD_API_URL }}
|
||||
VITE_SHOW_OPS: "false"
|
||||
run: |
|
||||
docker build -f VigilCareClinicalAPI/Dockerfile \
|
||||
-t "${REGISTRY}/clinical-api:${{ steps.meta.outputs.tag }}" \
|
||||
-t "${REGISTRY}/clinical-api:latest" .
|
||||
docker push "${REGISTRY}/clinical-api:${{ steps.meta.outputs.tag }}"
|
||||
docker push "${REGISTRY}/clinical-api:latest"
|
||||
|
||||
- name: Build and push ward-gateway
|
||||
# Same repo-root context as docker-compose.yml's ward-gateway-api service.
|
||||
run: |
|
||||
docker build -f VigilCare.WardGateway/Dockerfile \
|
||||
-t "${REGISTRY}/ward-gateway:${{ steps.meta.outputs.tag }}" \
|
||||
-t "${REGISTRY}/ward-gateway:latest" .
|
||||
docker push "${REGISTRY}/ward-gateway:${{ steps.meta.outputs.tag }}"
|
||||
docker push "${REGISTRY}/ward-gateway:latest"
|
||||
|
||||
- name: Build and push dashboard
|
||||
# Context is vigilcare-dashboard/ — package.json and nginx.conf live there.
|
||||
run: |
|
||||
docker build -f vigilcare-dashboard/Dockerfile \
|
||||
--build-arg VITE_API_URL="${{ vars.PROD_API_URL }}" \
|
||||
-t "${REGISTRY}/dashboard:${{ steps.meta.outputs.tag }}" \
|
||||
-t "${REGISTRY}/dashboard:latest" vigilcare-dashboard
|
||||
docker push "${REGISTRY}/dashboard:${{ steps.meta.outputs.tag }}"
|
||||
docker push "${REGISTRY}/dashboard:latest"
|
||||
|
||||
migrate:
|
||||
needs: build-and-push
|
||||
runs-on: ubuntu-latest
|
||||
# Checkout must run on the job host (Node). Do not use job-level container:.
|
||||
# Build the EF bundle via Dockerfile --target migrate (context upload), not
|
||||
# docker run -v — under act_runner bind mounts resolve on the Docker host.
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Build migration bundle
|
||||
run: |
|
||||
docker build -f VigilCareClinicalAPI/Dockerfile --target migrate \
|
||||
-t vigilcare-migrate-bundle:local .
|
||||
cid=$(docker create vigilcare-migrate-bundle:local)
|
||||
bash scripts/cap-buildx-builder.sh vigilcare-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 gateway --push --set gateway.output=type=image,push=true
|
||||
sleep 8
|
||||
if [ -n "${VITE_API_URL}" ]; then
|
||||
bake_one dashboard --push --set dashboard.output=type=image,push=true
|
||||
sleep 8
|
||||
else
|
||||
echo "PROD_API_URL unset — skipping dashboard bake" >&2
|
||||
exit 1
|
||||
fi
|
||||
bake_one migrate --set migrate.output=type=docker
|
||||
cid=$(docker create vigilcare-migrate-bundle:local /out/migrate-api)
|
||||
docker cp "$cid:/out/migrate-api" ./migrate-api
|
||||
docker rm "$cid"
|
||||
chmod +x ./migrate-api
|
||||
|
||||
# Runs while the previous release is still serving traffic, so every
|
||||
# migration must be backwards-compatible with the outgoing image.
|
||||
# See Step 6 — expand-then-contract.
|
||||
# Self-contained linux-x64 binary — runs on the job host.
|
||||
- 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
|
||||
steps:
|
||||
- name: Download migration bundle
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
name: migrate-api
|
||||
path: .
|
||||
|
||||
- name: Configure SSH
|
||||
run: |
|
||||
mkdir -p ~/.ssh
|
||||
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
|
||||
|
||||
# Expand-then-contract: migrate while the previous release still serves.
|
||||
- name: Apply migrations
|
||||
run: ./migrate-api --connection "${{ secrets.PG_CONNECTION_DDL }}"
|
||||
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-migrate-api"
|
||||
umask 077
|
||||
printf '%s' "$PG_CONNECTION_DDL" > /tmp/vigilcare-pg-ddl
|
||||
scp -i ~/.ssh/id_ed25519 /tmp/vigilcare-pg-ddl \
|
||||
"$REMOTE:/tmp/vigilcare-pg-ddl"
|
||||
rm -f /tmp/vigilcare-pg-ddl
|
||||
ssh -i ~/.ssh/id_ed25519 "$REMOTE" \
|
||||
NETWORK="$NETWORK" \
|
||||
bash -euo pipefail <<'EOF'
|
||||
chmod +x /tmp/vigilcare-migrate-api
|
||||
chmod 600 /tmp/vigilcare-pg-ddl
|
||||
|
||||
PG_CONNECTION_DDL="$(tr -d '\r\n' < /tmp/vigilcare-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-pg-ddl
|
||||
|
||||
docker run --rm \
|
||||
--network "${NETWORK}" \
|
||||
-v /tmp/vigilcare-migrate-api:/migrate-api:ro \
|
||||
-v /tmp/vigilcare-pg-ddl:/run/pg-ddl:ro \
|
||||
--entrypoint sh \
|
||||
mcr.microsoft.com/dotnet/runtime-deps:8.0 \
|
||||
-c 'exec /migrate-api --connection "$(tr -d "\r\n" < /run/pg-ddl)"'
|
||||
rm -f /tmp/vigilcare-migrate-api /tmp/vigilcare-pg-ddl
|
||||
EOF
|
||||
|
||||
deploy:
|
||||
needs: [build-and-push, migrate]
|
||||
@@ -122,15 +175,44 @@ 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-clinical"
|
||||
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
|
||||
|
||||
# Record the currently deployed tag so a rollback has a target.
|
||||
grep '^IMAGE_TAG=' .env > .env.previous || true
|
||||
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/.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
|
||||
|
||||
grep '^IMAGE_TAG=' .env > .env.previous || true
|
||||
if grep -q '^IMAGE_TAG=' .env; then
|
||||
sed -i "s|^IMAGE_TAG=.*|IMAGE_TAG=${IMAGE_TAG}|" .env
|
||||
else
|
||||
@@ -140,6 +222,7 @@ jobs:
|
||||
docker compose -f docker-compose.prod.yml --env-file .env pull
|
||||
docker compose -f docker-compose.prod.yml --env-file .env up -d --remove-orphans
|
||||
docker image prune -f
|
||||
docker compose -f docker-compose.prod.yml --env-file .env ps -a
|
||||
EOF
|
||||
|
||||
- name: Smoke test
|
||||
@@ -147,24 +230,31 @@ jobs:
|
||||
ssh -i ~/.ssh/id_ed25519 \
|
||||
"${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}" bash -euo pipefail <<'EOF'
|
||||
cd /opt/vigilcare
|
||||
# Do not `source` .env — compose env files are not bash (semicolons,
|
||||
# spaces in "SSL Mode=...", CRLF). Read only the host ports we need.
|
||||
env_val() { sed -n "s/^${1}=//p" .env | tail -n1 | tr -d '\r'; }
|
||||
API_PORT="$(env_val API_PORT)"; API_PORT="${API_PORT:-5270}"
|
||||
GATEWAY_PORT="$(env_val GATEWAY_PORT)"; GATEWAY_PORT="${GATEWAY_PORT:-5081}"
|
||||
DASHBOARD_PORT="$(env_val DASHBOARD_PORT)"; DASHBOARD_PORT="${DASHBOARD_PORT:-8080}"
|
||||
|
||||
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:${GATEWAY_PORT}/health/live" >/dev/null && echo "Gateway live."
|
||||
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
|
||||
echo "Ready check never passed"
|
||||
docker compose -f docker-compose.prod.yml --env-file .env logs --tail 150 api || true
|
||||
exit 1
|
||||
EOF
|
||||
|
||||
@@ -174,8 +264,8 @@ jobs:
|
||||
ssh -i ~/.ssh/id_ed25519 \
|
||||
"${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}" bash -euo pipefail <<'EOF'
|
||||
cd /opt/vigilcare
|
||||
# Restores the previous image tag only. Schema changes are NOT
|
||||
# reverted — this is why migrations must be backwards-compatible.
|
||||
# Restores the previous image tag only. Schema is NOT reverted —
|
||||
# migrations must be backwards-compatible (expand-then-contract).
|
||||
if [ -f .env.previous ]; then
|
||||
PREV=$(cut -d= -f2 .env.previous)
|
||||
sed -i "s|^IMAGE_TAG=.*|IMAGE_TAG=${PREV}|" .env
|
||||
|
||||
Reference in New Issue
Block a user