# Tests, image build and deployment on every push to main. # # 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. # # 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; deployment is skipped when unset # vars.DEPLOY_KNOWN_HOSTS `ssh-keyscan ` output # vars.DEPLOY_PATH directory on the server (default /opt/sorsfonalak) name: CI on: push: branches: [main] 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) build() { image="$IMAGE_PREFIX-$1"; shift docker build --label sorsfonalak.ci=1 -t "$image:$tag" -t "$image:latest" "$@" docker push -q "$image:$tag" docker push -q "$image:latest" } 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: vars.DEPLOY_HOST != '' runs-on: linux steps: - 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"