diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index f1038b3..572201a 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -8,6 +8,8 @@ name: Docker on: push: tags: ["v*"] + pull_request: + branches: [main] workflow_dispatch: permissions: @@ -23,7 +25,70 @@ env: IMAGE_NAME: kolektiv/kalendee jobs: + pack-check: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v7 + + - name: Set up pnpm + uses: pnpm/action-setup@v4 + with: + version: 11.25.0 + + - name: Set up Node + uses: actions/setup-node@v4 + with: + node-version: 22 + cache: pnpm + cache-dependency-path: server/pack/pnpm-lock.yaml + + - name: Install pack dependencies + run: pnpm --dir server/pack install --frozen-lockfile + + - name: Check page context bindings + run: node scripts/check-page-context.mjs + + flake-check: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v7 + + - name: Install Nix + uses: cachix/install-nix-action@v31 + + - name: Check flake + run: scripts/check-flake.sh + + smoke-image: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v7 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Build runtime image + uses: docker/build-push-action@v6 + with: + context: . + target: runtime + platforms: linux/amd64 + load: true + push: false + tags: kalendee-smoke:ci + build-args: VERSION=ci + cache-from: type=gha + cache-to: type=gha,mode=max + + - name: Smoke test the image + run: IMAGE_TAG=kalendee-smoke:ci scripts/smoke-image.sh --no-build + docker: + if: github.event_name != 'pull_request' + needs: [pack-check, flake-check, smoke-image] runs-on: ubuntu-latest steps: - name: Checkout diff --git a/DEVELOPING.md b/DEVELOPING.md index 27b5750..5c21d3a 100644 --- a/DEVELOPING.md +++ b/DEVELOPING.md @@ -20,6 +20,7 @@ This is a Kotlin Multiplatform project targeting Android, iOS, Desktop (JVM), an - [Demo data and screenshots](#demo-data-and-screenshots) - [Docker](#docker) - [NixOS](#nixos) +- [Pre-publish verification](#pre-publish-verification) - [Releasing](#releasing) - [Running tests](#running-tests) - [Troubleshooting](#troubleshooting) @@ -318,6 +319,16 @@ Configuration is HOCON-file-first there too: `configFile` mounts an operator-man Options: `enable` (bool), `image` (`docker.yuri.capital/kolektiv/kalendee`), `imageTag` (`latest`; pin a release for reproducibility), `port` (8080; host port to container 8080), `publicUrl` (exported as `KALENDEE_PUBLIC_URL`), `configFile` (HOCON file mounted read-only at `/config/application.conf` and exported as `KALENDEE_CONFIG`; it is copied into the Nix store, so never put secrets in it — start from [`application.conf.example`](./application.conf.example)), `environment` (extra non-secret variables), `environmentFile` (runtime secrets file; never put secrets in the Nix store), `volumes` (extra mounts; a named volume `kalendee-data` is always mounted at `/data`), `extraOptions` (podman/docker flags), and `openFirewall`. Podman is the default container backend; set `virtualisation.oci-containers.backend = "docker";` for Docker. Plain-HTTP deployments need `KALENDEE_COOKIE_SECURE = "false"` via `environment`. +## Pre-publish verification + +Run `scripts/pre-publish-checks.sh` before publishing. It runs, in order: + +- `scripts/check-page-context.mjs` — static guard for the v0.2.0 `+page.svelte` bug class: a template that uses `ctx` while the script never binds the `page<...>()` return value. `pnpm typecheck` only covers `.ts` files and `vite build` happily bundles the broken page, so this inspects the page templates directly. +- `scripts/check-flake.sh` — `nix flake check --no-build` then `nix flake show`; requires Nix (install it or skip this check). +- `scripts/smoke-image.sh` — builds the Dockerfile's `runtime` stage, boots it with a disposable PostgreSQL, then checks `/api/v1/health`, the `/login` HTML shell, the served `bootstrap.js` and `kalendee.login.js` pack modules, an end-to-end admin login, and an authenticated `/api/v1/calendars` fetch. Prerequisites: docker and curl. `--no-build` reuses `IMAGE_TAG` (default `kalendee-smoke:local`) instead of building; `--keep` leaves the containers and network up on failure for debugging; `HOST_PORT` (default `18080`) and `PG_IMAGE` (default `postgres:17-alpine`) are configurable. + +`.github/workflows/docker.yml` runs all three gates (`pack-check`, `flake-check`, `smoke-image`) before the multi-arch build and push, so nothing is published unless they pass. + ## Releasing Pushing a `v*` tag runs both release workflows; the version is the tag without the leading `v`: diff --git a/scripts/check-flake.sh b/scripts/check-flake.sh new file mode 100755 index 0000000..cd7ed66 --- /dev/null +++ b/scripts/check-flake.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash +# Evaluate the Nix flake the way a consumer would: check it, then show it. +# +# Prerequisites: nix with flakes enabled (install Nix or skip this check). +set -euo pipefail + +repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +cd "$repo_root" + +if ! command -v nix >/dev/null 2>&1; then + echo "check-flake: nix is not installed; install Nix or skip this check" >&2 + exit 1 +fi + +echo "==> nix flake check --no-build" +nix flake check --no-build + +echo "==> nix flake show" +nix flake show diff --git a/scripts/check-page-context.mjs b/scripts/check-page-context.mjs new file mode 100755 index 0000000..46f11bd --- /dev/null +++ b/scripts/check-page-context.mjs @@ -0,0 +1,55 @@ +#!/usr/bin/env node +// Guard against the v0.2.0 `/login` crash class: a page component that uses +// `ctx` in its template but never binds the `page<...>()` return value, so the +// rendered page throws at runtime. `pnpm typecheck` only runs tsc over `.ts` +// files, and `vite build` happily bundles the broken component, so this check +// walks every `+page.svelte` template itself. +// +// Run from anywhere: `node scripts/check-page-context.mjs`. + +import { readdirSync, readFileSync, statSync } from "node:fs" +import { dirname, join, relative, resolve } from "node:path" +import { fileURLToPath } from "node:url" + +const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..") +const pagesRoot = join(repoRoot, "server/pack/src/pages") + +const templateUsesCtx = /(?") + const script = scriptEnd === -1 ? "" : source.slice(0, scriptEnd) + const template = scriptEnd === -1 ? source : source.slice(scriptEnd + "".length) + if (!templateUsesCtx.test(template)) continue + if (!scriptBindsCtx.test(script)) offenders.push(relative(repoRoot, file)) +} + +if (offenders.length > 0) { + console.error("page-context guard failed:") + for (const file of offenders) { + console.error(`${file}: template uses ctx but the script does not bind the page() return value`) + } + process.exit(1) +} + +console.log(`page-context guard: ${pages.length} page components checked; every ctx reference is bound`) diff --git a/scripts/pre-publish-checks.sh b/scripts/pre-publish-checks.sh new file mode 100755 index 0000000..056c1ab --- /dev/null +++ b/scripts/pre-publish-checks.sh @@ -0,0 +1,40 @@ +#!/usr/bin/env bash +# Run every pre-publish gate in order. Used locally and by the Docker workflow +# before the runtime image is pushed. +# +# Usage: +# scripts/pre-publish-checks.sh [--no-build] +# +# --no-build forwarded to scripts/smoke-image.sh (reuse an existing image) +# SMOKE_ARGS extra arguments forwarded to scripts/smoke-image.sh +set -euo pipefail + +repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +cd "$repo_root" + +smoke_args=() +for arg in "$@"; do + case "$arg" in + --no-build) smoke_args+=("$arg") ;; + *) + echo "pre-publish-checks: unknown argument: $arg (expected --no-build)" >&2 + exit 2 + ;; + esac +done +if [ -n "${SMOKE_ARGS:-}" ]; then + # Deliberately word-split: SMOKE_ARGS is a space-separated list of flags. + read -r -a extra_args <<<"$SMOKE_ARGS" + smoke_args+=("${extra_args[@]}") +fi + +echo "==> pack page-context guard" +node scripts/check-page-context.mjs + +echo "==> flake check" +scripts/check-flake.sh + +echo "==> runtime image smoke test" +scripts/smoke-image.sh ${smoke_args[@]+"${smoke_args[@]}"} + +echo "pre-publish-checks: all gates passed" diff --git a/scripts/smoke-image.sh b/scripts/smoke-image.sh new file mode 100755 index 0000000..e68239f --- /dev/null +++ b/scripts/smoke-image.sh @@ -0,0 +1,217 @@ +#!/usr/bin/env bash +# Smoke test the published runtime image: build (or reuse) the Dockerfile's +# final `runtime` stage, boot it next to a disposable PostgreSQL, and exercise +# the paths that only exist once the pack is baked into the image — the Keel +# page shell, the real login page module, and an end-to-end login. +# +# Prerequisites: docker (daemon running) and curl. +# +# Usage: +# scripts/smoke-image.sh [--no-build] [--keep] +# +# Env: +# IMAGE_TAG image to build and run (default: kalendee-smoke:local) +# HOST_PORT localhost port to publish (default: 18080) +# PG_IMAGE postgres image (default: postgres:17-alpine) +# +# Flags: +# --no-build skip `docker build` and use IMAGE_TAG as-is +# --keep leave the containers/network up on failure for debugging +set -euo pipefail + +repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +cd "$repo_root" + +IMAGE_TAG="${IMAGE_TAG:-kalendee-smoke:local}" +HOST_PORT="${HOST_PORT:-18080}" +PG_IMAGE="${PG_IMAGE:-postgres:17-alpine}" + +build=1 +keep=0 +for arg in "$@"; do + case "$arg" in + --no-build) build=0 ;; + --keep) keep=1 ;; + *) + echo "smoke-image: unknown argument: $arg (expected --no-build or --keep)" >&2 + exit 2 + ;; + esac +done + +for tool in docker curl; do + if ! command -v "$tool" >/dev/null 2>&1; then + echo "smoke-image: $tool is required" >&2 + exit 1 + fi +done +if ! docker info >/dev/null 2>&1; then + echo "smoke-image: docker daemon is not reachable" >&2 + exit 1 +fi + +suffix="$$-$RANDOM" +network="kalendee-smoke-net-$suffix" +pg="kalendee-smoke-pg-$suffix" +app="kalendee-smoke-app-$suffix" +cookie_jar="$(mktemp)" +body="$(mktemp)" +base_url="http://127.0.0.1:${HOST_PORT}" + +cleanup() { + status=$? + rm -f "$cookie_jar" "$body" + if [ "$keep" -eq 1 ] && [ "$status" -ne 0 ]; then + echo "smoke-image: --keep set, leaving $app / $pg / $network up for debugging" >&2 + return + fi + docker rm -f "$app" >/dev/null 2>&1 || true + docker rm -f "$pg" >/dev/null 2>&1 || true + docker network rm "$network" >/dev/null 2>&1 || true +} +trap cleanup EXIT + +app_running() { + [ "$(docker inspect -f '{{.State.Running}}' "$app" 2>/dev/null || echo false)" = "true" ] +} + +dump_app_logs() { + echo "--- docker logs $app ---" >&2 + docker logs "$app" >&2 2>&1 || true + echo "--- end docker logs $app ---" >&2 +} + +fail() { + echo "FAIL: $1" >&2 + echo "--- failing response ---" >&2 + cat "$body" >&2 || true + echo >&2 + dump_app_logs + exit 1 +} + +pass() { + echo "PASS: $1" +} + +if [ "$build" -eq 1 ]; then + echo "==> docker build --target runtime --build-arg VERSION=smoke -t $IMAGE_TAG ." + docker build --target runtime --build-arg VERSION=smoke -t "$IMAGE_TAG" . +fi + +echo "==> creating network $network and postgres container $pg ($PG_IMAGE)" +docker network create "$network" >/dev/null +docker run -d --name "$pg" --network "$network" \ + -e POSTGRES_DB=kalendee \ + -e POSTGRES_USER=kalendee \ + -e POSTGRES_PASSWORD=smoke \ + "$PG_IMAGE" >/dev/null + +echo -n "==> waiting for postgres" +deadline=$((SECONDS + 60)) +until docker exec "$pg" pg_isready -U kalendee -d kalendee >/dev/null 2>&1; do + if [ "$(docker inspect -f '{{.State.Running}}' "$pg" 2>/dev/null || echo false)" != "true" ]; then + echo + docker logs "$pg" >&2 || true + echo "smoke-image: postgres container exited before becoming ready" >&2 + exit 1 + fi + if [ "$SECONDS" -ge "$deadline" ]; then + echo + docker logs "$pg" >&2 || true + echo "smoke-image: postgres did not become ready within 60s" >&2 + exit 1 + fi + sleep 1 + echo -n . +done +echo " ready" + +echo "==> starting app container $app (127.0.0.1:$HOST_PORT -> 8080)" +docker run -d --name "$app" --network "$network" \ + -p "127.0.0.1:${HOST_PORT}:8080" \ + -e "KALENDEE_DATABASE_URL=jdbc:postgresql://${pg}:5432/kalendee" \ + -e KALENDEE_DATABASE_USER=kalendee \ + -e KALENDEE_DATABASE_PASSWORD=smoke \ + -e KALENDEE_ADMIN_USERNAME=admin \ + -e KALENDEE_ADMIN_PASSWORD=smoke-admin \ + -e "KALENDEE_PUBLIC_URL=${base_url}" \ + -e KALENDEE_COOKIE_SECURE=false \ + "$IMAGE_TAG" >/dev/null + +echo -n "==> waiting for ${base_url}/api/v1/health" +deadline=$((SECONDS + 120)) +ready=0 +until [ "$ready" -eq 1 ]; do + if ! app_running; then + echo + dump_app_logs + echo "smoke-image: app container exited before becoming ready" >&2 + exit 1 + fi + code="$(curl -sS -o "$body" -w '%{http_code}' --max-time 5 "$base_url/api/v1/health" 2>/dev/null || true)" + if [ "$code" = "200" ]; then + ready=1 + break + fi + if [ "$SECONDS" -ge "$deadline" ]; then + echo + echo "smoke-image: app did not become ready within 120s (last HTTP status: ${code:-none})" >&2 + dump_app_logs + exit 1 + fi + sleep 2 + echo -n . +done +echo " ready" + +if grep -q '"ok"' "$body"; then + pass "GET /api/v1/health body contains \"ok\"" +else + fail "GET /api/v1/health body does not contain \"ok\"" +fi + +code="$(curl -sS -o "$body" -w '%{http_code}' --max-time 15 "$base_url/login")" +if [ "$code" != "200" ]; then + fail "GET /login returned $code" +fi +if ! grep -q '