# main: tests and image build (tags: commit hash, `main`). # prod: the same, then deployment to the server (tags: commit hash, `prod`, `latest`). # # Runs on the self-hosted runner in ci/runner (label `linux`). Jobs use the # host's Docker daemon, so image builds share its BuildKit cache; a commit # already built on main is rebuilt from cache in seconds. # # Repository settings (Settings → Actions): # secrets.REGISTRY_TOKEN Gitea access token with package write access # secrets.DEPLOY_SSH_KEY private key of the deploy user on the server # vars.DEPLOY_HOST user@host of the server # vars.DEPLOY_KNOWN_HOSTS `ssh-keyscan ` output # vars.DEPLOY_PATH directory on the server (default /opt/sorsfonalak) name: CI on: push: branches: [main, prod] workflow_dispatch: env: REGISTRY: registry.sagifer.com IMAGE_PREFIX: registry.sagifer.com/sagifer/sorsfonalak defaults: run: shell: sh jobs: test: runs-on: linux container: image: rust:1.99-alpine # Kept between runs, so only changed crates are compiled again. volumes: - sorsfonalak-ci-cargo:/usr/local/cargo/registry - sorsfonalak-ci-target:/ci-target services: postgres: image: postgres:18-alpine env: POSTGRES_USER: sorsfonalak POSTGRES_PASSWORD: ci POSTGRES_DB: sorsfonalak env: CARGO_TARGET_DIR: /ci-target DATABASE_URL: postgres://sorsfonalak:ci@postgres:5432/sorsfonalak steps: - name: Checkout run: | apk add --no-cache -q git musl-dev git init -q . && git remote add origin "${{ github.server_url }}/${{ github.repository }}.git" git fetch -q --depth 1 origin "${{ github.sha }}" && git checkout -q FETCH_HEAD - name: Wait for Postgres run: | for i in $(seq 30); do nc -z postgres 5432 && exit 0; sleep 1; done echo "postgres did not start" >&2; exit 1 # astro-engine and horoscope-api need the Swiss Ephemeris sources and # data (scripts/fetch-deps.sh); their tests run locally. - name: Test run: cargo test --locked -p backend -p worker -p db -p horoscope-client -p service-kit -p analysis-prompt -p email-templates build: needs: test runs-on: linux steps: - name: Checkout run: | apk add --no-cache -q git git init -q . && git remote add origin "${{ github.server_url }}/${{ github.repository }}.git" git fetch -q --depth 1 origin "${{ github.sha }}" && git checkout -q FETCH_HEAD - name: Log in to the registry env: TOKEN: ${{ secrets.REGISTRY_TOKEN }} run: printf '%s' "$TOKEN" | docker login "$REGISTRY" -u "${{ github.repository_owner }}" --password-stdin # The frontend build also type-checks the TypeScript code. - name: Build and push images run: | set -eu tag=$(echo "${{ github.sha }}" | cut -c1-12) extra="${{ github.ref_name }}" [ "$extra" = prod ] && extra="prod latest" build() { image="$IMAGE_PREFIX-$1"; shift docker build --label sorsfonalak.ci=1 -t "$image:$tag" "$@" docker push -q "$image:$tag" for t in $extra; do docker tag "$image:$tag" "$image:$t" docker push -q "$image:$t" done } build horoscope-api -f services/horoscope-api/Dockerfile . build backend -f services/backend/Dockerfile . build worker -f services/worker/Dockerfile . build frontend services/frontend - name: Clean up if: always() run: | docker logout "$REGISTRY" || true docker image prune -f --filter label=sorsfonalak.ci=1 >/dev/null deploy: needs: build if: github.ref == 'refs/heads/prod' runs-on: linux steps: - name: Check settings run: | [ -n "${{ vars.DEPLOY_HOST }}" ] || { echo "vars.DEPLOY_HOST is not set (Settings → Actions → Variables)" >&2; exit 1; } - name: Checkout run: | apk add --no-cache -q git openssh-client git init -q . && git remote add origin "${{ github.server_url }}/${{ github.repository }}.git" git fetch -q --depth 1 origin "${{ github.sha }}" && git checkout -q FETCH_HEAD - name: SSH key env: KEY: ${{ secrets.DEPLOY_SSH_KEY }} KNOWN_HOSTS: ${{ vars.DEPLOY_KNOWN_HOSTS }} run: | mkdir -p ~/.ssh && chmod 700 ~/.ssh (umask 077 && printf '%s\n' "$KEY" > ~/.ssh/id_ed25519) printf '%s\n' "$KNOWN_HOSTS" > ~/.ssh/known_hosts # Copies the compose files, pins TAG in the server's .env and restarts # the changed services. The database migrations run when the backend starts. - name: Deploy env: HOST: ${{ vars.DEPLOY_HOST }} DIR: ${{ vars.DEPLOY_PATH || '/opt/sorsfonalak' }} run: | set -eu tag=$(echo "${{ github.sha }}" | cut -c1-12) scp -q deploy/docker-compose.yml deploy/Caddyfile "$HOST:$DIR/" ssh "$HOST" "cd '$DIR' && \ (grep -q '^TAG=' .env && sed -i 's/^TAG=.*/TAG=$tag/' .env || echo 'TAG=$tag' >> .env) && \ docker compose pull -q && docker compose up -d --remove-orphans --wait && \ docker compose exec -T caddy caddy reload --config /etc/caddy/Caddyfile --adapter caddyfile && \ docker image prune -f >/dev/null" # A release (and its tag, e.g. v2026.11.17-163612e) for every deployment, # listing the commits since the previous one. - name: Release env: TOKEN: ${{ secrets.GITEA_TOKEN }} API: ${{ github.server_url }}/api/v1/repos/${{ github.repository }} run: | set -eu apk add --no-cache -q curl jq git fetch -q --unshallow --tags origin short=$(echo "${{ github.sha }}" | cut -c1-7) name="v$(date -u +%Y.%m.%d)-$short" if curl -fsS -o /dev/null -H "Authorization: token $TOKEN" "$API/releases/tags/$name" 2>/dev/null; then echo "release $name already exists (redeploy)"; exit 0 fi prev=$(git describe --tags --abbrev=0 --match 'v*' HEAD 2>/dev/null || true) if [ -n "$prev" ]; then range="$prev..HEAD"; since="since $prev"; else range="-20 HEAD"; since="(last 20 commits)"; fi changes=$(git log --no-merges --pretty='- %s (%h)' $range) body=$(printf 'Deployed to production on %s UTC.\nImages: `registry.sagifer.com/sagifer/sorsfonalak-*:%s`\n\nChanges %s:\n%s\n' \ "$(date -u '+%Y-%m-%d %H:%M')" "$(echo "${{ github.sha }}" | cut -c1-12)" "$since" "$changes") jq -n --arg tag "$name" --arg sha "${{ github.sha }}" --arg body "$body" \ '{tag_name: $tag, target_commitish: $sha, name: $tag, body: $body}' \ | curl -fsS -X POST -H "Authorization: token $TOKEN" -H 'Content-Type: application/json' -d @- "$API/releases" \ | jq -r '"created release " + .name + ": " + .html_url'