# ============================================================================= # 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. # test and publish run on the build VM's act_runner (stpbuild, label # `build`); deploy runs on the stpweb host's (stpvm, `self-hosted`), the only # one with the stack. Both are 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. # # THREE 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 two images, the site # (Dockerfile) and its code runner (Dockerfile.runner), # smoke-tests the runner image, and pushes each to this Gitea's # container registry as : and :latest. It reuses test's git # cache, so both must run on the same runner. # deploy after publish, on stpvm: pulls them into the stpweb stack # (services `usaco-runner`, then `usaco`) and waits for each to # be healthy — see "Deploying" in site/README.md. The runner goes # first, so the new site never starts against an old runner. # # 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 RUNNER_IMAGE: adam-french.co.uk/adamf/usaco-runner jobs: test: runs-on: build env: SHA: ${{ github.sha }} REF: ${{ github.ref }} # Over HTTP with the job's own token (read access to this repo while # the job runs), so the runner needs no SSH key or Gitea user. REMOTE: http://git.adam-french.co.uk:3000/adamf/usaco.git GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }} 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" # The job's token as a Basic auth header, passed via env rather # than -c so it never shows in the process list. auth=$(printf 'x-access-token:%s' "$GITEA_TOKEN" | base64 -w0) GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=http.extraHeader \ GIT_CONFIG_VALUE_0="Authorization: Basic $auth" \ 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 stpweb's deploy.yaml. git # is installed because internal/repo and internal/index test # against real repositories. internal/runner's tests compile and # run C++ and Python with the image's g++ and python3 (it runs as # root, which the rlimits do not mind, and its sh is dash, whose # ulimit they are written for); clang-tidy and ruff are not in # it, so those tests skip, and the publish job's smoke test # covers them in the runner image. - 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: build 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 the images 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 build -f "$SRC/site/Dockerfile.runner" -t "$RUNNER_IMAGE:$SHA" -t "$RUNNER_IMAGE:latest" "$SRC/site" # The runner image is mostly other people's toolchains (g++, # python3, clang-tidy, ruff on musl), which `go test` never sees. # So before anything is pushed, start it with the production # hardening and run one C++ program and one lint through it, # from inside the container: the host has no curl, and busybox # wget is enough. - name: Smoke-test the runner image shell: bash run: | set -eu name="usaco-runner-ci-$SHA" docker run -d --name "$name" --read-only --tmpfs /tmp:size=256m,exec \ --memory 1g --pids-limit 128 --cap-drop ALL --security-opt no-new-privileges \ "$RUNNER_IMAGE:$SHA" >/dev/null trap 'docker rm -f "$name" >/dev/null' EXIT for i in $(seq 1 10); do docker exec "$name" wget -qO- http://localhost:8081/health && break sleep 1 done post() { docker exec "$name" wget -qO- --header 'Content-Type: application/json' --post-data "$2" "http://localhost:8081/$1"; } run=$(post run '{"lang":"cpp","source":"#include \nint main(){int a,b;std::cin>>a>>b;std::cout<\nint main(){int a=1,b=2,c=3;return a==b==c;}"}') echo "$lint" echo "$lint" | grep -q '"source":"clang-tidy"' lint=$(post lint '{"lang":"python","source":"import os\n"}') echo "$lint" echo "$lint" | grep -q '"rule":"F401"' - name: Push the images shell: bash run: | set -eu docker push "$IMAGE:$SHA" docker push "$IMAGE:latest" docker push "$RUNNER_IMAGE:$SHA" docker push "$RUNNER_IMAGE:latest" - name: Remove the scratch directory if: always() shell: bash run: rm -rf "$SRC" deploy: needs: publish runs-on: self-hosted steps: # The runner first, the same way as the site below, so the new # site never talks to an old runner. Pinning # USACO_RUNNER_IMAGE_TAG in the site's .env makes this a no-op. - name: Roll the runner onto the new image working-directory: /home/adamf/deploy/stpweb shell: bash run: | set -eu flock .compose.lock sh -c 'docker compose pull usaco-runner && docker compose up -d --no-deps usaco-runner' for i in $(seq 1 30); do status=$(docker inspect -f '{{.State.Health.Status}}' usaco-runner) echo "usaco-runner: $status" [ "$status" = healthy ] && exit 0 [ "$status" = unhealthy ] && break sleep 5 done docker logs --tail 50 usaco-runner exit 1 # Put the new image live, exactly as job-tracker's workflow does # for `jobs`: pull + up of the one service, under .compose.lock, which # stpweb'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/stpweb 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