# `test` runs on every push; `deploy` runs for main only after it passes. # 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: self-hosted env: # Via env so a branch name is never parsed as shell. SHA: ${{ github.sha }} REF: ${{ github.ref }} REMOTE: ssh://git@git.adam-french.co.uk:2222/adamf/stpweb.git GIT_SSH_COMMAND: "ssh -o StrictHostKeyChecking=no" # Kept between runs so fetches are incremental. CACHE: /home/adamf/.cache/stpweb-ci/repo.git SRC: /home/adamf/.cache/stpweb-ci/src-${{ github.sha }} 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. 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