ci: publish image to the own registry on release tag #19

Merged
Noel merged 5 commits from feat/registry into main 2026-10-24 17:33:26 +00:00
5 changed files with 109 additions and 31 deletions
Showing only changes of commit eebd6ca697 - Show all commits

No files matched your search

+64 -21
View File
@@ -25,7 +25,14 @@ env:
jobs:
publish:
name: Build and push image
runs-on: ubuntu-latest
# Dedicated label, NOT ubuntu-latest: this job needs the host Docker
# socket, and `docker_host` is a runner-wide setting. Sharing a label
# with pr-checker.yml would hand PR code (from a branch) a root-
# equivalent socket on the host that also runs Gitea and k3s.
# Register a second act_runner with `--labels publish:docker://node:22-bookworm`
# and `container.docker_host: automount` in its config, before the first
# tag: an unmatched label leaves the job queued indefinitely.
runs-on: publish
# A hung build would hold the capacity-1 runner and, through it, every
# other job on this server. Gitea's instance default is not a contract.
timeout-minutes: 20
@@ -107,6 +114,50 @@ jobs:
} >> "$GITHUB_OUTPUT"
echo "Building ${version} from ${sha} (latest: ${latest})"
- name: Log in to the registry
env:
REGISTRY_USERNAME: ${{ secrets.REGISTRY_USERNAME }}
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }}
run: |
# Missing secrets must fail loudly: an anonymous `docker push`
# would answer 401 with a message most people misread as a
# missing repository.
[ -n "$REGISTRY_USERNAME" ] || { echo "::error::secret REGISTRY_USERNAME is empty"; exit 1; }
[ -n "$REGISTRY_PASSWORD" ] || { echo "::error::secret REGISTRY_PASSWORD is empty"; exit 1; }
printf '%s' "$REGISTRY_PASSWORD" \
| docker login registry.noelsabia.dev -u "$REGISTRY_USERNAME" --password-stdin
- name: Refuse to overwrite a published version
env:
REGISTRY_USERNAME: ${{ secrets.REGISTRY_USERNAME }}
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }}
VERSION: ${{ steps.version.outputs.version }}
run: |
# A version tag is immutable once published. Gitea can replay any
# run, and `docker push` would silently replace :vX.Y.Z with a
# rebuilt image, so the release would change content after the
# fact. A missing tag (first attempt, or a run that failed before
# pushing) is fine - nothing was published.
#
# Registry v2 API instead of `docker manifest inspect`: the
# Debian 20.10 client fails that command on OCI indexes with
# "unsupported manifest media type and no default available",
# for existing and missing tags alike.
url="https://registry.noelsabia.dev/v2/cv_website/nuxt/manifests/${VERSION}"
code=$(curl -s -o /dev/null -w '%{http_code}' \
-u "${REGISTRY_USERNAME}:${REGISTRY_PASSWORD}" \
-H 'Accept: application/vnd.docker.distribution.manifest.v2+json' \
-H 'Accept: application/vnd.oci.image.index.v1+json' \
-H 'Accept: application/vnd.oci.image.manifest.v1+json' \
"$url")
echo "GET /v2/cv_website/nuxt/manifests/${VERSION}: HTTP ${code}"
case "$code" in
200) echo "::error::${VERSION} is already published and version tags are immutable - delete that tag in the registry deliberately to replace it"; exit 1 ;;
404) echo "${VERSION} is not published yet - continuing" ;;
401|403) echo "::error::registry rejected the credentials (HTTP ${code}) - check REGISTRY_USERNAME/REGISTRY_PASSWORD"; exit 1 ;;
*) echo "::error::unexpected answer from the registry (HTTP ${code})"; exit 1 ;;
esac
- name: Build image
env:
VERSION: ${{ steps.version.outputs.version }}
@@ -128,41 +179,33 @@ jobs:
VERSION: ${{ steps.version.outputs.version }}
run: |
# Start the real server inside the built image and require a 2xx
# from /. Cheaper than publishing a broken release, and the only
# step that proves the Nitro output actually runs. Done inside the
# container because the app's port is not reachable from this job
# container - the daemon publishes on the host, not here.
# with a real SSR body, not an empty shell. Cheaper than
# publishing a broken release, and the only step that proves the
# Nitro output actually runs. Done inside the container because
# the app's port is not reachable from this job container - the
# daemon publishes on the host, not here.
#
# The size floor is deliberately loose: the rendered homepage was
# 97 kB on 2026-10-24, so 10 kB catches "200 but nothing rendered"
# without failing on a future copy change.
docker run --rm "${IMAGE}:${VERSION}" sh -c '
node .output/server/index.mjs &
server=$!
i=0
while [ "$i" -lt 30 ]; do
if node -e "fetch(\"http://127.0.0.1:3000/\").then(r => process.exit(r.ok ? 0 : 1)).catch(() => process.exit(1))"; then
echo "server answered 2xx after ${i}s"
if out=$(node -e "fetch(\"http://127.0.0.1:3000/\").then(async r => { const body = await r.text(); if (!r.ok || body.length < 10000) process.exit(1); console.log(r.status + \" \" + body.length + \" bytes\") }).catch(() => process.exit(1))" 2>/dev/null); then
echo "homepage rendered after ${i}s: HTTP ${out}"
kill "$server"
exit 0
fi
i=$((i + 1))
sleep 1
done
echo "server did not answer 2xx within 30s"
echo "no 2xx homepage with a rendered body within 30s"
kill "$server"
exit 1
'
- name: Log in to the registry
env:
REGISTRY_USERNAME: ${{ secrets.REGISTRY_USERNAME }}
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }}
run: |
# Missing secrets must fail loudly: an anonymous `docker push`
# would answer 401 with a message most people misread as a
# missing repository.
[ -n "$REGISTRY_USERNAME" ] || { echo "::error::secret REGISTRY_USERNAME is empty"; exit 1; }
[ -n "$REGISTRY_PASSWORD" ] || { echo "::error::secret REGISTRY_PASSWORD is empty"; exit 1; }
printf '%s' "$REGISTRY_PASSWORD" \
| docker login registry.noelsabia.dev -u "$REGISTRY_USERNAME" --password-stdin
- name: Tag latest
if: steps.version.outputs.latest == 'true'
env:
+9
View File
@@ -0,0 +1,9 @@
# actionlint configuration.
#
# `publish` is the label of the second act_runner registration (the one with
# the host Docker socket). actionlint only knows GitHub's hosted labels, so
# custom self-hosted labels have to be declared here or every workflow using
# them fails the lint hook.
self-hosted-runner:
labels:
- publish
+6
View File
@@ -55,6 +55,12 @@ repos:
hooks:
- id: actionlint
files: \.gitea/workflows/.*\.ya?ml$
# actionlint only auto-loads its config when the file is
# .github/actionlint.yaml *and* it recognises a project root by
# .github/workflows, which this repo does not have (it uses
# .gitea/workflows). Pass the config explicitly, or the custom
# `publish` runner label is reported as unknown.
args: [-config-file, .github/actionlint.yaml]
# --------------------------------------------------------- ESLint (Nuxt) -
# Local hook: needs `pnpm install` to have run in this repo. --fix makes
+29 -10
View File
@@ -73,17 +73,24 @@ this machine means Gitea's data, k3s, and the CV itself. `docker_host` is a
that runner, including PR jobs whose code comes from a branch.
Therefore the rule: **the socket only ever goes on a runner label that
PR-triggered jobs do not use.** Two acceptable layouts:
PR-triggered jobs do not use.**
* a second act_runner registration with `docker_host: automount` and a
distinct label (e.g. `publish`), used only by this workflow; or
* one runner, but this workflow moved to the dedicated label and
`pr-checker.yml` left on a label without the socket.
The workflow follows it by scheduling onto a dedicated label:
Not acceptable: adding `automount` to the single shared runner while
`pr-checker.yml` runs on it. If that is the only option, treat it as an
accepted risk and record it here - the trade is "PR code can become root on
the host" against "images can be built at all".
```yaml
runs-on: publish # NOT ubuntu-latest, which pr-checker.yml uses
```
That label only exists once a second act_runner registration is configured
with `container.docker_host: automount` and, for example,
`--labels publish:docker://node:22-bookworm`. Until then the job queues
without starting, and `timeout-minutes` does not apply to a job that never
starts - register the runner before the first release tag.
Not acceptable: adding `automount` to the shared `ubuntu-latest` runner.
That hands PR code a root-equivalent socket on the host that also runs
Gitea and k3s. If it is ever the only option, treat it as an accepted risk
and record it here rather than letting the workflow imply it.
# Why `--platform linux/arm64`
@@ -102,14 +109,26 @@ run.
Both are repo secrets. The login uses `--password-stdin` so the value never
appears in the process list or the log.
# Before the first release tag
1. Repo secrets `REGISTRY_USERNAME` and `REGISTRY_PASSWORD` exist.
2. A runner with the `publish` label exists and its config has
`container.docker_host: automount`. Without the label the job queues
forever; without the socket it fails at `Check daemon access`.
3. `pr-checker.yml` still runs on a label whose runner has no socket.
4. Namespace `cv` has an `imagePullSecret` for the same credentials.
5. `registry.noelsabia.dev` answers over HTTPS.
# Guards in the workflow
| Guard | Why |
|---|---|
| Strict `^vMAJOR.MINOR.PATCH$` check | A `v[0-9]*` prefix match accepts `v1`, `v1-anything` and `v1.2.3-rc1`, all of which are valid registry tags and would publish silently |
| `latest` only when the built tag is the highest `vX.Y.Z` in the repository | Gitea can replay any run. Without the comparison, re-running `v1.2.0` after `v1.3.0` drags `latest` backwards. A re-run of the newest tag still moves it - that release had not produced an image yet. If the tag list cannot be fetched, `latest` is skipped with a warning rather than guessed |
| Smoke test before any push | The image is started and must answer 2xx on `/` from inside the container. Build success only proves the bundle compiled. Port publishing is not usable here: the daemon binds on the host, not in the job container |
| A published version tag is never overwritten | Before building, the registry's v2 API is asked for `manifests/<version>`: `200` aborts the run, `404` continues. A replay of a released tag fails instead of silently replacing published content. `docker manifest inspect` is deliberately *not* used - the Debian 20.10 client fails it on OCI indexes with `unsupported manifest media type and no default available`, for existing and missing tags alike |
| Smoke test before any push | The image is started and must answer 2xx with at least 10 kB of rendered HTML on `/` (the real page is ~97 kB), so "200 but nothing rendered" fails too. Port publishing is not usable here: the daemon binds on the host, not in the job container |
| `timeout-minutes: 20` | A hung build would hold the capacity-1 runner and every job queued behind it |
| `.github/actionlint.yaml` | Declares the custom `publish` label, otherwise the lint hook reports it as unknown. The hook passes the file with `-config-file`, because actionlint only auto-loads it when it recognises a `.github/workflows` project |
# Costs and consequences
+1
View File
@@ -4,6 +4,7 @@
* **Creation**: [publish-image.yml](../.gitea/workflows/publish-image.yml) - a `vX.Y.Z` tag push builds `Dockerfile` on the arm64 runner and pushes `registry.noelsabia.dev/cv_website/nuxt` as the version and `sha-<12>`, plus `latest` when that tag is the newest release. Main pushes deliberately do not build: the registry holds releases, not commits. The job uses the runner's own Docker daemon (client only in the `node:22-bookworm` container), so the act_runner needs `container.docker_host: automount`; the first step fails with that cause if it is missing. That socket is root-equivalent, so it may only sit on a label PR jobs cannot use - recorded under the trust boundary in [Publishing images to the own registry](decisions/registry-publishing.md). Repo secrets: `REGISTRY_USERNAME`, `REGISTRY_PASSWORD`.
* **Review follow-up** (PR #19, 1 BLOCKER + 4 WARN + 2 NIT): applied the strict `vMAJOR.MINOR.PATCH` check, the newest-release rule for `latest`, a smoke test that starts the image and requires 2xx on `/`, and `timeout-minutes: 20`. Rejected with evidence: the `.dockerignore` leak (`.env*`, `*.pem`, `.git` are excluded) and the Docker-client/daemon API mismatch (20.10.24 client against a 29.9.2 daemon built successfully over the socket).
* **Second review follow-up** (PR #19): the job now runs on the dedicated `publish` label instead of `ubuntu-latest`, so the host socket it needs can never be shared with PR jobs - the earlier version required a runner configuration its own trust boundary forbade. Added the immutability guard (registry v2 `manifests/<version>`: `200` aborts, `404` continues) after measuring that `docker manifest inspect` fails on OCI indexes with the pinned 20.10 client; the smoke test now also requires a rendered body (10 kB floor) rather than a bare 2xx. `.github/actionlint.yaml` declares the custom label, and the hook passes it with `-config-file` because this repo keeps workflows in `.gitea/`.
## 2026-10-23 (PR #16 review checked, three findings applied)
* **Update**: [pr-checker.yml](../.gitea/workflows/pr-checker.yml) - the review prompt now states that `pnpm-lock.yaml` is excluded from `.pr-review-diff.patch` on purpose. The reviewer read the exclusion as an uncommitted lockfile and raised a false BLOCKER; the lockfile is in the PR and matches `package.json`.