From 05b1666f812677cdd1b1c550b2a077ebd3aba69d Mon Sep 17 00:00:00 2001 From: Tom Date: Sat, 10 Oct 2026 00:37:42 +0200 Subject: [PATCH] Deploy the stack from the repo's compose.yaml via Gitea Actions - deploy.yml gets a deploy job after the build: pull, compose up on Unraid through the runner's Docker socket, then a health gate on eb-blazor /health from inside proxynet; docs-only pushes trigger nothing, manual runs allowed - STRATO_DYNDNS_PASSWORD comes from a Gitea secret instead of the Compose Manager env file - restart: always, because Unraid stops containers explicitly on shutdown and unless-stopped would leave the stack down after a reboot; the dev overlay turns it off - Docs describe the automatic rollout and the one-time switch away from the Compose Manager stack Co-Authored-By: Claude Opus 5.5 --- .gitea/workflows/deploy.yml | 89 ++++++++++++++++++++++--- compose.dev.yaml | 4 ++ compose.yaml | 14 +++- docs/deployment.md | 122 +++++++++++++--------------------- docs/unraid-infrastructure.md | 22 +++--- 5 files changed, 152 insertions(+), 99 deletions(-) diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index 9efc232..425c791 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -1,25 +1,41 @@ -# Builds the production image on every push to main and pushes it to the Gitea -# registry, tagged with :latest AND the short commit SHA (for rollback) -- the -# same tagging scheme as scripts/release.sh, but run by the act_runner instead -# of by hand. +# Builds the production image on every push to main, pushes it to the Gitea +# registry (tagged :latest AND the short commit SHA, for rollback) and deploys the +# stack on Unraid from the repo's compose.yaml -- the repo is the only copy of the +# compose file; there is no duplicate in the Compose Manager. # -# It does NOT deploy: pulling and starting the new :latest on Unraid stays a -# manual, deliberate step (Compose Down/Up). Auto-deploy via Watchtower is an -# open item in docs/deployment.md and needs a health gate first, so a broken -# page can never go live unnoticed. +# The deploy works because the act_runner runs ON Unraid and hands its Docker +# socket to the job: every `docker` command here talks to the Unraid daemon, so +# absolute paths in compose.yaml (the pb_data bind mount) are host paths. +# +# `docker compose up -d` only recreates containers whose image or config changed: +# a new image restarts eb-blazor for a few seconds, eb-pocketbase and eb-ddns keep +# running. Content lives in PocketBase and is never touched by a deploy. # # Required repository secrets (Gitea -> repo -> Settings -> Actions -> Secrets): -# REGISTRY_USER the registry username (e.g. Tom) -# REGISTRY_TOKEN a Gitea access token with package:write -- NOT the password +# REGISTRY_USER the registry username (e.g. Tom) +# REGISTRY_TOKEN a Gitea access token with package:write -- NOT the password +# STRATO_DYNDNS_PASSWORD the DynDNS password for eb-ddns (docs/deployment.md) -name: Build and push image +name: Build and deploy on: push: branches: [main] + # Docs alone change neither the image nor the stack. + paths-ignore: + - "docs/**" + - "**/*.md" + # Manual run from Gitea (Actions tab), e.g. to redeploy after a server restart. + workflow_dispatch: + +# Two quick pushes must not deploy over each other; the later one waits. +concurrency: + group: deploy + cancel-in-progress: false env: IMAGE: gitea.anticarnist.de/tom/elternbeirat + COMPOSE_VERSION: v2.39.4 jobs: build: @@ -52,3 +68,54 @@ jobs: tags: | ${{ env.IMAGE }}:latest ${{ env.IMAGE }}:${{ steps.sha.outputs.short }} + + deploy: + needs: build + runs-on: ubuntu-latest + steps: + - name: Check out the source + uses: actions/checkout@v4 + + # The job image ships the Docker CLI (the build needs it) but not + # necessarily the compose plugin; install a pinned one if it is missing. + - name: Ensure docker compose + run: | + docker compose version && exit 0 + mkdir -p ~/.docker/cli-plugins + curl -fsSL -o ~/.docker/cli-plugins/docker-compose \ + "https://github.com/docker/compose/releases/download/${COMPOSE_VERSION}/docker-compose-linux-x86_64" + chmod +x ~/.docker/cli-plugins/docker-compose + docker compose version + + # The image is private; the pull below runs with this job's login. + - name: Log in to the Gitea registry + uses: docker/login-action@v3 + with: + registry: gitea.anticarnist.de + username: ${{ secrets.REGISTRY_USER }} + password: ${{ secrets.REGISTRY_TOKEN }} + + # An explicit pull: `up` alone keeps a local :latest and would not notice + # the image the build job just pushed. + - name: Pull and start the stack + env: + STRATO_DYNDNS_PASSWORD: ${{ secrets.STRATO_DYNDNS_PASSWORD }} + run: | + docker compose -f compose.yaml pull + docker compose -f compose.yaml up -d --wait --remove-orphans + + # /health answers 200 only when the app runs AND reaches PocketBase. It is + # checked from inside proxynet, the same way NPM reaches the app. A failure + # marks the run red; the rollback stays a deliberate step (docs/deployment.md). + - name: Health gate + run: | + for attempt in $(seq 1 30); do + if docker run --rm --network proxynet busybox:1.37 \ + wget -qO- http://eb-blazor:8080/health; then + exit 0 + fi + sleep 2 + done + echo "eb-blazor did not report healthy within 60 seconds." >&2 + docker logs --tail 50 eb-blazor >&2 + exit 1 diff --git a/compose.dev.yaml b/compose.dev.yaml index 0dd5bd4..cf03ded 100644 --- a/compose.dev.yaml +++ b/compose.dev.yaml @@ -10,6 +10,9 @@ services: eb-pocketbase: + # The base's `restart: always` is for Unraid; locally the stack should not come + # back on its own after a reboot of the dev machine. + restart: "no" ports: - "8090:8090" # PocketBase admin UI at http://localhost:8090/_/ environment: @@ -36,6 +39,7 @@ services: # changes show up without a push+CI round-trip. image: "" clears the inherited # tag from the base, which forces compose to use the build below. image: "" + restart: "no" build: context: . dockerfile: Dockerfile diff --git a/compose.yaml b/compose.yaml index 72f013b..87576bb 100644 --- a/compose.yaml +++ b/compose.yaml @@ -7,6 +7,14 @@ # web app publishes no host port. The `ports:` block on PocketBase is TEMPORARY: it # is the editors' way onto the admin UI until they reach it through an NPM subdomain # (#9). Unraid setup: docs/unraid-infrastructure.md. +# +# On Unraid this file is deployed as is by .gitea/workflows/deploy.yml (project +# eb-stack), not pasted into the Compose Manager. +# +# `restart: always`, not `unless-stopped`: Unraid stops every container explicitly +# when Docker or the array goes down, and `unless-stopped` treats that as a manual +# stop -- the stack would stay down after a reboot. Without the Compose Manager +# there is no autostart toggle to bring it back. # For local development use the dev overlay on top, which adds host ports and builds # the web image from source: @@ -25,7 +33,7 @@ services: eb-pocketbase: image: ghcr.io/muchobien/pocketbase:0.40.4 container_name: eb-pocketbase - restart: unless-stopped + restart: always environment: TZ: Europe/Berlin volumes: @@ -47,7 +55,7 @@ services: eb-web: image: gitea.anticarnist.de/tom/elternbeirat:latest container_name: eb-blazor - restart: unless-stopped + restart: always depends_on: eb-pocketbase: condition: service_healthy @@ -71,7 +79,7 @@ services: # tests quiet, where the service is disabled. Setup: docs/deployment.md. image: qmcgaw/ddns-updater:2.10 container_name: eb-ddns - restart: unless-stopped + restart: always environment: TZ: Europe/Berlin PERIOD: 5m diff --git a/docs/deployment.md b/docs/deployment.md index 7d5173e..a202326 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -1,8 +1,9 @@ # Deployment -How a new version of the website ends up on Unraid. Current state: -**manual** — build the image locally, push it to the Gitea registry, pull it on -Unraid. Automation via Gitea Actions comes later. +How a new version of the website ends up on Unraid: a merge to `main` runs +`.gitea/workflows/deploy.yml`, which builds the image, pushes it to the Gitea +registry and starts the stack on Unraid from the repo's `compose.yaml`. Nothing is +copied or pasted by hand. The site runs as a **three-container stack** (project `eb-stack`): the Blazor app (`eb-blazor`), a PocketBase container (`eb-pocketbase`) that holds the content, @@ -52,16 +53,11 @@ DynDNS password. 2. **Strato password:** DynDNS accepts the package password **or** a separate DynDNS password, set under **Security → Set passwords** (not under Dynamic DNS). Use the separate one — the package password also opens the webspace. -3. **Compose Manager Plus** → stack **eb-stack** → env file: add - - ```ini - STRATO_DYNDNS_PASSWORD= - ``` - - **No quotes** — Compose Manager Plus passed them on as part of the password, - which Strato answered with `badauth`. The password ends up inside JSON, so it - must not contain `"` or `\`; avoid `$` and `#` as well. -4. **Compose Down**, then **Compose Up** (a plain restart keeps the old env). +3. **Gitea** → repo → **Settings** → **Actions** → **Secrets**: add + `STRATO_DYNDNS_PASSWORD`. The deploy hands it to compose, which fills it into + `CONFIG`. The password ends up inside JSON, so it must not contain `"` or `\`; + avoid `$` and `#` as well. +4. Run the workflow (**Actions** → **Build and deploy** → **Run workflow**). `docker logs eb-ddns` shows `Updating record … to use ` with no `ERROR` after it. 5. Check: the Strato nameservers answer with the public IP of the connection @@ -79,8 +75,8 @@ domain or password are wrong: curl -u elternbeirat-igmh.info "https://dyndns.strato.com/nic/update?hostname=elternbeirat-igmh.info&myip=" ``` -> **The DynDNS password is a secret** like the registry token: only in the stack's -> env on the server, never in Git. A missing value does not stop the stack; it +> **The DynDNS password is a secret** like the registry token: only as a Gitea +> secret, never in Git. A missing value does not stop the stack; it > shows up as a failed update in `docker logs eb-ddns`. The dev overlay disables > `eb-ddns`, so a local `up` never touches the public DNS. @@ -125,67 +121,48 @@ dev stack (`entwicklung.md`) — it builds the web image from source and pushes nothing, so a development state can never accidentally land in the registry as `:latest`. -## Rolling out a new version (manual) +## Rolling out a new version -Prerequisite: logged in to the registry once (see below). Then, **on main** and -with a clean working directory: +Merge the pull request into `main`. The workflow then: -```bash -scripts/release.sh -``` +1. builds the image and pushes it as `:latest` **and** `:` (rollback), +2. pulls it on Unraid and runs `docker compose up -d` with the repo's + `compose.yaml` — only containers whose image or config changed are recreated, +3. checks `http://eb-blazor:8080/health` from inside `proxynet` (health gate). A + red run means the new version is live but not healthy: roll back (below). -The script builds the image, tags it with `:latest` **and** the short commit SHA -(for rollback), and pushes both. It **aborts** if you are not on `main` or have -uncommitted changes, and warns about unpushed commits. +Pushes that only touch `docs/` or Markdown files trigger nothing. A manual run +(**Actions** → **Build and deploy** → **Run workflow**) redeploys the current +`main`. > The image path `gitea.anticarnist.de/tom/elternbeirat` is **lowercase** — > container registries require that in the path, even though the user (`Tom`) and > the repo (`Elternbeirat`) are capitalized. -### Logging Unraid in to the registry once +`scripts/release.sh` still builds and pushes by hand from `main`, as a fallback +when the runner is down. It does not deploy. -The image is **private**, so Unraid has to log in once before it can pull. The -**Compose Manager Plus** plugin has no UI field for this — the login runs through -the Unraid terminal (`>_` symbol at the top right of the web interface, prompt -`root@Cube:~#`): +### One-time switch from the Compose Manager -```bash -docker login gitea.anticarnist.de -# Username: Tom -# Password: -``` +Before the workflow took over, the stack was a Compose Manager stack +(`elternbeirat`) with a pasted copy of `compose.yaml`. The workflow runs the stack +as project `eb-stack`; the fixed `container_name`s would clash with the old +containers, so the old stack goes first: -The login stays stored; it only has to be repeated when the token changes. Two -pitfalls learned from experience: +1. Add the secrets in Gitea (see "Automation" below). +2. Compose Manager → stack `elternbeirat` → **Compose Down**, then delete the + stack. `pb_data` is a bind mount and stays. +3. Run the workflow by hand. The site is down from step 2 until it finishes. -- **Don't confuse it with PowerShell/laptop.** The login has to happen in the - *Unraid* terminal (`root@Cube`), not in Windows PowerShell (`PS C:\`). The laptop - needs the login only to *push*, Unraid to *pull*. -- **A wrong username gets stuck.** If the login reports "Stored credentials - invalid or expired" and does *not* ask for the name, first `docker logout - gitea.anticarnist.de`, then log in again — otherwise a nonsense username gets - stored by accident. -- **Plain-text warning.** Docker stores the token unencrypted in - `/root/.docker/config.json`. On your own server, okay for a start. - *Cleaner later:* set up a credential helper (→ open item below). - -### Pull and start again on Unraid - -In the **Compose Manager Plus** plugin (Unraid web interface): - -- **Docker** tab → **Compose** section → stack **eb-stack** → - **Compose Down**, then **Compose Up** (or "Pull" + "Up", depending on the plugin - version), so that the new `:latest` is pulled. Only the `eb-blazor` image - changes on a deploy; `eb-pocketbase` and its `pb_data` volume stay as they are. - -> `docker compose up` does **not** automatically re-pull a `:latest` if an image of -> the same name is already present locally. When in doubt, explicitly "Pull" first. +From then on, a click on **Compose Up** in the Compose Manager is never needed — +and there is no stale copy left there that could be started by accident. --- ## The compose files -`compose.yaml` is the **production** base and is the file Unraid uses. It defines +`compose.yaml` is the **production** base; the workflow deploys it to Unraid as +is. It defines all services. `eb-blazor` publishes no host port and joins the external network `proxynet`, so NPM is the only path to the web app. `eb-pocketbase` still publishes `8090` for the editors until they reach it through an NPM subdomain (see #9). @@ -224,10 +201,9 @@ web app is needed — the site comes back by itself. ## Rollback -`scripts/release.sh` additionally tags each release with the short commit SHA, -which — unlike the moving `:latest` — never shifts. To roll back, replace -`:latest` in the Unraid `compose.yaml` with the `:` of the last working state -and bring it back up: +Every image is also tagged with the short commit SHA, which — unlike the moving +`:latest` — never shifts. To roll back, pin that SHA in `compose.yaml` on a branch, +merge it and let the workflow deploy it: ```yaml image: gitea.anticarnist.de/tom/elternbeirat:99dd052 # instead of :latest @@ -240,25 +216,23 @@ Which SHA tags are in the registry is shown by Gitea under ## Automation -### Build and push (done) +### Build and deploy (done) -`.gitea/workflows/deploy.yml` builds the image on every push to `main` and pushes -it to the registry, tagged `:latest` **and** `:` — the same scheme as -`scripts/release.sh`, just run by the `act_runner` instead of by hand. It does -**not** deploy; pulling and starting the new `:latest` on Unraid stays a manual, -deliberate step (see "Pull and start again on Unraid" above). +`.gitea/workflows/deploy.yml` has two jobs: `build` (image → registry) and +`deploy` (compose up on Unraid + health gate). The `deploy` job works because the +`act_runner` runs on Unraid and passes its Docker socket into the job, so every +`docker` command there talks to the Unraid daemon. -Requires two repository secrets (Gitea → repo → **Settings** → **Actions** → +Requires three repository secrets (Gitea → repo → **Settings** → **Actions** → **Secrets**): - `REGISTRY_USER` — the registry username (e.g. `Tom`). - `REGISTRY_TOKEN` — a Gitea access token with **`package: write`**, *not* the login password. +- `STRATO_DYNDNS_PASSWORD` — the DynDNS password for `eb-ddns` (see above). -The workflow currently only builds and pushes — it does **not** run the tests -yet, and it does **not** deploy. Those open steps (tests in the workflow, -auto-deploy with a health gate via Watchtower, encrypting the registry token on -Unraid) are tracked as Gitea issues under the "Elternbeirat-Website" milestone. +Still open: the workflow does not run the tests yet, and a failed health gate +does not roll back on its own. Two known pitfalls: the `act_runner` needs Docker socket access to build, and it must offer the `ubuntu-latest` label the workflow asks for. To check the runner: diff --git a/docs/unraid-infrastructure.md b/docs/unraid-infrastructure.md index eaec6d2..e1b7f85 100644 --- a/docs/unraid-infrastructure.md +++ b/docs/unraid-infrastructure.md @@ -10,7 +10,7 @@ What runs on the Unraid host and how the website stack fits in. This is the | LAN IP | `192.168.1.14` | | Web UI | `https://unraid.server:44302` | | Version | Unraid OS 7.0.1 | -| Stack management | Compose Manager plugin, stack `elternbeirat` | +| Stack management | Gitea Actions (`.gitea/workflows/deploy.yml`), no Compose Manager | | Docker network for NPM | `proxynet` (created with `docker network create proxynet`) | **Settings → Docker → Advanced View → Preserve user defined networks: Yes.** @@ -19,17 +19,17 @@ attached to it fails to start. ## Website stack -The stack is created in the Compose Manager under the name `elternbeirat`. Its -compose content is a copy of `compose.yaml` **without the comments**, pasted -into the stack's config in the Unraid UI. +The stack is deployed by `.gitea/workflows/deploy.yml` as project `eb-stack`, +straight from the repo's `compose.yaml` (`deployment.md`). It is **not** a Compose +Manager stack: there is no copy of the compose file on the server. It shows up +on the Docker tab like any other container. -> **The project name comes from the stack folder, not from `name:`.** The -> Compose Manager starts the stack with `-p elternbeirat`, which overrides -> `name: eb-stack`. Renaming the stack in the UI does not change it. That is why -> `compose.yaml` gives the stack's network the fixed name `eb-internal` instead of -> the derived `elternbeirat_default`. Changing a network name needs **Compose Down** -> and then **Compose Up**; a plain Up fails because the existing containers still -> point at the old network. +The `Gitea-Runner` container runs the deploy. It has `/var/run/docker.sock` mounted, +so the workflow's `docker compose` talks to the Unraid daemon directly. + +> **Network names.** The stack's own network has the fixed name `eb-internal`. +> Changing a network name needs `docker compose down` and then `up`; a plain `up` +> fails because the existing containers still point at the old network. | Container | Network | Container port | Host port | |---|---|---|---|