Pull MinIO from the Gitea-mirrored pgsty image after upstream Hub/Quay removal.
CI / changes (push) Successful in 47s
CI / frontend (push) Skipped
CI / backend (push) Successful in 43m10s

This commit is contained in:
2026-09-30 23:09:01 +08:00
parent 01e1724d6a
commit 1c1065938f
27 changed files with 3533 additions and 3 deletions
+281
View File
@@ -0,0 +1,281 @@
# Kit template — copy to .gitea/workflows/cd.yml and fill placeholders.
# Copy templates/docker-bake.hcl to the repo root; copy scripts/cap-buildx-builder.sh
# next to CD (until infra/ci-actions owns capped bake).
#
# 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), UI_* bake vars if you ship a SPA image,
# MIGRATE_DOCKER_NETWORK (default shared-services)
#
# Substitute every <owner>, <repo-slug>, /opt/<repo-slug>, image names.
name: CD
on:
push:
tags: ["v*"]
workflow_dispatch:
inputs:
image_tag:
description: "Image tag to deploy (defaults to the pushed tag)"
required: false
env:
REGISTRY: git.vectur45.com/<owner>/<repo-slug>
jobs:
build-and-push:
runs-on: ubuntu-latest
outputs:
tag: ${{ steps.meta.outputs.tag }}
steps:
- uses: actions/checkout@v4
- name: Resolve tag and registry
id: meta
run: |
if [ -n "${{ vars.REGISTRY }}" ]; then
echo "REGISTRY=${{ vars.REGISTRY }}" >> "$GITHUB_ENV"
fi
if [ -n "${{ inputs.image_tag }}" ]; then
echo "tag=${{ inputs.image_tag }}" >> "$GITHUB_OUTPUT"
elif [[ "${GITHUB_REF}" == refs/tags/* ]]; then
echo "tag=${GITHUB_REF_NAME}" >> "$GITHUB_OUTPUT"
else
echo "image_tag input is required for workflow_dispatch without a tag" >&2
exit 1
fi
- name: Log in to the Gitea registry
run: |
echo "${{ secrets.REGISTRY_TOKEN }}" \
| docker login "${REGISTRY%%/*}" \
-u "${{ secrets.REGISTRY_USERNAME }}" --password-stdin
- name: Bake and push images
env:
TAG: ${{ steps.meta.outputs.tag }}
VITE_API_BASE_URL: ${{ vars.UI_API_BASE_URL }}
VITE_RELEASE: ${{ steps.meta.outputs.tag }}
VITE_ENVIRONMENT: production
run: |
# 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 <repo-slug>-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
# Omit ui bake if the project has no SPA image.
if [ -n "${VITE_API_BASE_URL}" ]; then
bake_one ui --push --set ui.output=type=image,push=true
sleep 8
fi
bake_one migrate --set migrate.output=type=docker
# scratch migrate-bundle needs an explicit command (or image CMD).
cid=$(docker create <repo-slug>-migrate-bundle:local /out/migrate-api)
docker cp "$cid:/out/migrate-api" ./migrate-api
docker rm "$cid"
chmod +x ./migrate-api
- 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
# 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:
- 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.
# 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/myapp-migrate-api"
umask 077
printf '%s' "$PG_CONNECTION_DDL" > /tmp/myapp-pg-ddl
scp -i ~/.ssh/id_ed25519 /tmp/myapp-pg-ddl \
"$REMOTE:/tmp/myapp-pg-ddl"
rm -f /tmp/myapp-pg-ddl
ssh -i ~/.ssh/id_ed25519 "$REMOTE" \
NETWORK="$NETWORK" \
bash -euo pipefail <<'EOF'
chmod +x /tmp/myapp-migrate-api
chmod 600 /tmp/myapp-pg-ddl
PG_CONNECTION_DDL="$(tr -d '\r\n' < /tmp/myapp-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/myapp-pg-ddl
docker run --rm \
--network "${NETWORK}" \
-v /tmp/myapp-migrate-api:/migrate-api:ro \
-v /tmp/myapp-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/myapp-migrate-api /tmp/myapp-pg-ddl
EOF
deploy:
needs: [build-and-push, migrate]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- 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
- name: Copy compose file
run: |
scp -i ~/.ssh/id_ed25519 docker-compose.prod.yml \
"${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}:/opt/<repo-slug>/docker-compose.prod.yml"
- 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/<owner>/<repo-slug>"
fi
REGISTRY_HOST="${REGISTRY%%/*}"
ssh -i ~/.ssh/id_ed25519 \
"${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}" \
IMAGE_TAG="$IMAGE_TAG" \
REGISTRY_HOST="$REGISTRY_HOST" \
REGISTRY_USERNAME="$REGISTRY_USERNAME" \
REGISTRY_TOKEN="$REGISTRY_TOKEN" \
bash -euo pipefail <<'EOF'
cd /opt/<repo-slug>
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/<repo-slug>/.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
echo "IMAGE_TAG=${IMAGE_TAG}" >> .env
fi
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
run: |
ssh -i ~/.ssh/id_ed25519 \
"${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}" bash -euo pipefail <<'EOF'
cd /opt/<repo-slug>
env_val() { sed -n "s/^${1}=//p" .env | tail -n1 | tr -d '\r'; }
API_PORT="$(env_val API_PORT)"; API_PORT="${API_PORT:-8100}"
for i in $(seq 1 30); do
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})."
exit 0
fi
echo "Attempt ${i}/30: HTTP ${http_code}"
sleep 5
done
echo "Ready check never passed"
docker compose -f docker-compose.prod.yml --env-file .env logs --tail 150 api || true
exit 1
EOF
- name: Roll back on failure
if: failure()
run: |
ssh -i ~/.ssh/id_ed25519 \
"${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}" bash -euo pipefail <<'EOF'
cd /opt/<repo-slug>
# 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
docker compose -f docker-compose.prod.yml --env-file .env up -d
echo "Rolled back to ${PREV}"
fi
EOF
+115
View File
@@ -0,0 +1,115 @@
# Kit template — copy to .gitea/workflows/ci.yml and fill placeholders.
# Pin @v1 (or @v1.1 once post_build_command is tagged on infra/ci-workflows).
# Never a branch. Path filter + changes job stay local (repo-specific).
#
# node-ci phases: lint → extra_check (NO dist) → build → post_build (MAY use dist).
# Do not put check:bundle / Playwright / anything that needs dist/ in extra_check.
#
# If tests still use Testcontainers, the workflow's Postgres/Redis services may sit
# idle — prefer extending infra/ci-workflows over forking steps.
name: CI
on:
push:
branches: [main, master]
paths:
- "api/**"
- "web/**"
- "tests/**"
- "MyApp.sln"
- ".gitea/workflows/**"
pull_request:
branches: [main, master]
paths:
- "api/**"
- "web/**"
- "tests/**"
- "MyApp.sln"
- ".gitea/workflows/**"
workflow_dispatch:
jobs:
# Stays local: repo-specific path map; needs fetch-depth: 0.
# Keep actions/checkout@v4 here until builtin:checkout is proven with fetch-depth: 0.
changes:
runs-on: ubuntu-latest
outputs:
backend: ${{ steps.filter.outputs.backend }}
frontend: ${{ steps.filter.outputs.frontend }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- id: filter
name: Detect stack changes
run: |
set -euo pipefail
backend=false
frontend=false
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
backend=true
frontend=true
else
if [ "${{ github.event_name }}" = "pull_request" ]; then
base="${{ github.event.pull_request.base.sha }}"
else
base="${{ github.event.before }}"
fi
if [ -z "${base:-}" ] || [ "$base" = "0000000000000000000000000000000000000000" ]; then
backend=true
frontend=true
else
files="$(git diff --name-only "$base" "${{ github.sha }}")"
printf '%s\n' "$files"
if printf '%s\n' "$files" | grep -qE '^(api/|tests/|MyApp\.sln|\.gitea/workflows/)'; then
backend=true
fi
if printf '%s\n' "$files" | grep -qE '^(web/|\.gitea/workflows/)'; then
frontend=true
fi
fi
fi
echo "backend=${backend}" >> "$GITHUB_OUTPUT"
echo "frontend=${frontend}" >> "$GITHUB_OUTPUT"
backend:
needs: changes
if: needs.changes.outputs.backend == 'true'
uses: infra/ci-workflows/.gitea/workflows/dotnet8-ci.yml@v1
with:
working_directory: .
solution: MyApp.sln
postgres_db: myapp_test
# Prefer split projects: isolated (InMemory) first, then service-backed.
# If unit + integration share one .csproj, omit isolated_test_projects and
# list that project only under test_projects.
isolated_test_projects: |
tests/MyApp.UnitTests/MyApp.UnitTests.csproj
test_projects: |
tests/MyApp.IntegrationTests/MyApp.IntegrationTests.csproj
test_env: |
Auth__ApiKey=ci-test-api-key-at-least-32-bytes-long!!
frontend:
needs: changes
if: needs.changes.outputs.frontend == 'true'
# Use @v1.1 once infra/ci-workflows tags post_build_command; until then keep
# post-dist steps inside build_command after `npx vite build`.
uses: infra/ci-workflows/.gitea/workflows/node-ci.yml@v1
with:
working_directory: web
node_version: "22"
# Typecheck once here; build must not re-run tsc / vue-tsc.
lint_command: npm run lint && npm run typecheck
# Pre-dist only (audit, unit tests, source checks).
extra_check_command: npm audit --audit-level=high && npm run test -- --run --coverage
build_command: npx vite build
# Needs dist/ — bundle budget, Playwright, etc. (requires node-ci @v1.1+)
post_build_command: npm run check:bundle && npx playwright install --with-deps chromium && npx playwright test
build_env: |
VITE_API_BASE_URL=http://localhost:5067
VITE_RELEASE=ci
VITE_ENVIRONMENT=ci
CI=true
artifact_path: dist