Files
sorsfonalak/.gitea/workflows/ci.yml
T
fecoandClaude Opus 5.5 24f9024d24
CI / test (push) Successful in 39s
CI / build (push) Successful in 12s
CI / deploy (push) Skipped
CI: deploy only from the prod branch
main builds images tagged with the commit hash and `main`; prod also tags
`prod` and `latest` and deploys. A prod run without DEPLOY_HOST now fails
instead of skipping silently.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
2026-11-17 06:06:02 +00:00

138 lines
5.3 KiB
YAML

# 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 <host>` 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
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 image prune -f >/dev/null"