env-apply was removed from web_server, so its run/lock no longer exists and the roll-out step failed with "cannot open lock file". Take .compose.lock, which web_server's deploy and maintenance workflows hold. The DOCKER_API_VERSION=1.41 pin was for the Pi's Docker 20.10; the host now runs Docker 29. Co-Authored-By: Claude Opus 5.5 <[email protected]>
147 lines
6.5 KiB
YAML
147 lines
6.5 KiB
YAML
# =============================================================================
|
|
# ci.yaml — test the site (site/) on every push that changes it; publish an
|
|
# image for such pushes to main that pass. Copied from job-tracker's ci.yaml,
|
|
# with the same runner and constraints: a self-hosted act_runner on the web_server
|
|
# host (stpvm) in HOST mode, with git and docker and no other toolchain. So Go and Node run
|
|
# in `docker run` containers, and the source is fetched with plain git rather
|
|
# than actions/checkout (which needs Node on the host).
|
|
#
|
|
# PATHS
|
|
# Most pushes here are problems ("Solve moocast", from the CLI or from the
|
|
# site itself) and must not rebuild anything: the running site picks those
|
|
# up through the Gitea webhook. Only changes to site/ or this workflow run
|
|
# it.
|
|
#
|
|
# TWO JOBS
|
|
# test go vet + go test, and a vite build of site/web. The same checks
|
|
# as `make check` in site/.
|
|
# publish main only, after test: builds the image, pushes it to this
|
|
# Gitea's container registry as :<sha> and :latest, then pulls it
|
|
# into the web_server stack (its `usaco` service) and waits for it
|
|
# to be healthy — see "Deploying" in site/README.md.
|
|
#
|
|
# SECRETS (repo settings -> Actions -> Secrets)
|
|
# REGISTRY_USER a Gitea user that can write packages for adamf
|
|
# REGISTRY_TOKEN that user's access token with the write:package scope
|
|
# =============================================================================
|
|
name: Test and publish the site
|
|
|
|
on:
|
|
push:
|
|
paths:
|
|
- "site/**"
|
|
- ".gitea/workflows/**"
|
|
|
|
env:
|
|
IMAGE: adam-french.co.uk/adamf/usaco
|
|
|
|
jobs:
|
|
test:
|
|
runs-on: self-hosted
|
|
env:
|
|
SHA: ${{ github.sha }}
|
|
REF: ${{ github.ref }}
|
|
REMOTE: ssh://git@localhost:2222/adamf/usaco.git
|
|
GIT_SSH_COMMAND: "ssh -o StrictHostKeyChecking=no"
|
|
CACHE: /home/adamf/.cache/usaco-ci/repo.git
|
|
SRC: /home/adamf/.cache/usaco-ci/src-${{ github.sha }}
|
|
steps:
|
|
- name: Export the pushed commit's site/ to a scratch directory
|
|
shell: bash
|
|
run: |
|
|
set -eu
|
|
mkdir -p "$(dirname "$CACHE")"
|
|
[ -d "$CACHE" ] || git init --quiet --bare "$CACHE"
|
|
git --git-dir="$CACHE" fetch --quiet --no-tags "$REMOTE" "$REF"
|
|
rm -rf "$SRC"
|
|
mkdir -p "$SRC"
|
|
git --git-dir="$CACHE" archive --format=tar "$SHA" site | tar -x -C "$SRC"
|
|
test -f "$SRC/site/go.mod"
|
|
|
|
# Read-only source, and named volumes for the module and build
|
|
# caches, for the reasons given in web_server's deploy.yaml. git
|
|
# is installed because internal/repo and internal/index test
|
|
# against real repositories.
|
|
- name: Go - vet and test
|
|
shell: bash
|
|
run: |
|
|
set -eu
|
|
docker run --rm \
|
|
-v "$SRC/site":/src:ro -w /src \
|
|
-v usaco_ci_gomod:/go/pkg/mod \
|
|
-v usaco_ci_gobuild:/root/.cache/go-build \
|
|
golang:1.26 \
|
|
sh -c 'go vet ./... && go test ./...'
|
|
|
|
# Built in the container's own copy: node_modules and dist must not
|
|
# be written into $SRC, which is mounted read-only.
|
|
- name: Web - vite build
|
|
shell: bash
|
|
run: |
|
|
set -eu
|
|
docker run --rm \
|
|
-v "$SRC/site/web":/src:ro \
|
|
-v usaco_ci_npm:/root/.npm \
|
|
node:22-alpine \
|
|
sh -c 'cp -r /src /build && cd /build && npm ci --no-audit --no-fund && npx vite build'
|
|
|
|
- name: Remove the scratch directory
|
|
if: always()
|
|
shell: bash
|
|
run: rm -rf "$SRC"
|
|
|
|
publish:
|
|
needs: test
|
|
if: github.ref == 'refs/heads/main'
|
|
runs-on: self-hosted
|
|
env:
|
|
SHA: ${{ github.sha }}
|
|
CACHE: /home/adamf/.cache/usaco-ci/repo.git
|
|
SRC: /home/adamf/.cache/usaco-ci/publish-${{ github.sha }}
|
|
REGISTRY_USER: ${{ secrets.REGISTRY_USER }}
|
|
REGISTRY_TOKEN: ${{ secrets.REGISTRY_TOKEN }}
|
|
# The Dockerfile's `RUN --mount=type=cache` lines need BuildKit.
|
|
# It is the default from Docker 23 (the host runs 29); this was
|
|
# needed on the Pi's Docker 20.10, whose plain `docker build` used
|
|
# the legacy builder, and is kept for any older daemon. This switches on BuildKit.
|
|
DOCKER_BUILDKIT: "1"
|
|
steps:
|
|
- name: Build and push the image
|
|
shell: bash
|
|
run: |
|
|
set -eu
|
|
rm -rf "$SRC" && mkdir -p "$SRC"
|
|
git --git-dir="$CACHE" archive --format=tar "$SHA" site | tar -x -C "$SRC"
|
|
printf '%s' "$REGISTRY_TOKEN" | docker login adam-french.co.uk -u "$REGISTRY_USER" --password-stdin
|
|
# Built natively on the host, so the image is amd64 — the only
|
|
# platform it runs on.
|
|
docker build -t "$IMAGE:$SHA" -t "$IMAGE:latest" "$SRC/site"
|
|
docker push "$IMAGE:$SHA"
|
|
docker push "$IMAGE:latest"
|
|
|
|
- name: Remove the scratch directory
|
|
if: always()
|
|
shell: bash
|
|
run: rm -rf "$SRC"
|
|
|
|
# Put the new image live, exactly as job-tracker's workflow does
|
|
# for `jobs`: pull + up of the one service, under .compose.lock, which
|
|
# web_server's deploy.yaml and maintenance.yaml hold, then wait for the
|
|
# image's HEALTHCHECK (process up, repository cloned and indexed).
|
|
# Pinning USACO_IMAGE_TAG in the site's .env makes this a no-op.
|
|
- name: Roll the site onto the new image
|
|
working-directory: /home/adamf/deploy/web_server
|
|
shell: bash
|
|
run: |
|
|
set -eu
|
|
flock .compose.lock sh -c 'docker compose pull usaco && docker compose up -d --no-deps usaco'
|
|
for i in $(seq 1 30); do
|
|
status=$(docker inspect -f '{{.State.Health.Status}}' usaco)
|
|
echo "usaco: $status"
|
|
[ "$status" = healthy ] && exit 0
|
|
[ "$status" = unhealthy ] && break
|
|
sleep 5
|
|
done
|
|
docker logs --tail 50 usaco
|
|
exit 1
|