# `test` runs on every push, on the build VM (stpbuild, label `build`); # `deploy` runs for main only after it passes, on stpvm (`self-hosted`), # since it recreates the stack there. # The runner is in host mode with only git and docker on the host, so Go and # Python run in containers pinned to the same images as their Dockerfiles # (bump together), and checkout is plain git rather than actions/checkout. # The Vue build isn't tested here: a failed image build in `up --build` # aborts before any container is replaced. name: Test and deploy on: push: jobs: test: runs-on: build env: # Via env so a branch name is never parsed as shell. 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/stpweb.git GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }} # Kept between runs so fetches are incremental. CACHE: /home/adamf/.cache/stpweb-ci/repo.git # Per run, not just per commit: the build runner takes two jobs at # once, and the same commit pushed to two branches runs twice. SRC: /home/adamf/.cache/stpweb-ci/src-${{ github.sha }}-${{ github.run_id }} steps: - name: Export the pushed commit to a scratch directory shell: bash run: | set -eu mkdir -p "$(dirname "$CACHE")" [ -d "$CACHE" ] || git init --quiet --bare "$CACHE" # $SHA, not FETCH_HEAD: test this run's commit even if the # branch has moved on. # 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" | tar -x -C "$SRC" # In case the shell lacks pipefail. test -f "$SRC/backend/go.mod" test -f "$SRC/python/requirements-dev.txt" echo "Exported $SHA ($REF) to $SRC" # Read-only so root in the container can't leave files the # clean-up step can't delete. The named volumes are Go caches. - name: Backend - go vet and go test shell: bash run: | set -eu docker run --rm \ -v "$SRC":/src:ro \ -w /src/backend \ -v stpweb_ci_gomod:/go/pkg/mod \ -v stpweb_ci_gobuild:/root/.cache/go-build \ golang:1.26 \ sh -c 'go vet ./... && go test ./...' # Read-only source, so bytecode and the pytest cache are off. - name: Python - pytest shell: bash run: | set -eu docker run --rm \ -v "$SRC":/src:ro \ -w /src/python \ -v stpweb_ci_pip:/root/.cache/pip \ -e PYTHONDONTWRITEBYTECODE=1 \ python:3.12-slim \ sh -c 'pip install --quiet --disable-pip-version-check --root-user-action=ignore -r requirements-dev.txt && python -m pytest tests -q -p no:cacheprovider' - name: Remove the scratch directory if: always() shell: bash run: rm -rf "$SRC" deploy: needs: test if: github.ref == 'refs/heads/main' runs-on: self-hosted steps: - name: Pull changes working-directory: /home/adamf/deploy/stpweb env: GIT_SSH_COMMAND: "ssh -o StrictHostKeyChecking=no" SHA: ${{ github.sha }} shell: bash run: | set -eu git config --global --add safe.directory /home/adamf/deploy/stpweb git fetch ssh://git@git.adam-french.co.uk:2222/adamf/stpweb.git main # Deploy only the tested commit; a newer main gets its own # run. This also stops a re-run rolling the site back. if [ "$(git rev-parse FETCH_HEAD)" != "$SHA" ]; then echo "main is at $(git rev-parse FETCH_HEAD), but this run tested $SHA." echo "Not deploying: the run for the newer commit will." exit 1 fi git reset --hard FETCH_HEAD - name: Run docker compose up working-directory: /home/adamf/deploy/stpweb env: COMPOSE_BAKE: "false" # Shared with maintenance.yaml so the two never run compose at once. run: flock .compose.lock docker compose up -d --build --remove-orphans - name: Prune unused Docker resources run: docker image prune -f