Add Gitea Actions pipeline that builds and publishes the image
Some checks failed
build / container (push) Failing after 14s

Runs on pushes to master, tag pushes and pull requests:

  * the Dockerfile's new `test` stage runs the whole suite, so a failing
    test stops the image from being built or pushed at all
  * the image builds with plain docker rather than the buildx and login
    actions, so a job never has to reach the internet to fetch third-party
    actions -- the most common way a self-hosted runner fails
  * it pushes to this instance's registry, deriving the host from
    github.server_url instead of hardcoding it

Tags published: `latest` and the branch or tag name (only master moves
`latest`), plus an immutable sha-<short> so an image can be traced back to
its commit.  Pull requests build but never push, so a fork cannot publish
into the registry.

Two things the gate caught once it ran inside a container:

  * the suite reads config.example.json from the repository root, a hidden
    dependency that would have failed the first pipeline run
  * `[ "$ref_name" = "$DEFAULT_BRANCH" ]` under `set -u` would have aborted
    every branch build if that env key were ever renamed; it now has an
    inline fallback

`make test-docker` runs the same gate locally, and README.md documents what
the runner needs: docker reachable from the job, and a token allowed to
write packages.

Also untrack .pi/, which holds pi session task output rather than project
files, and ignore it.
This commit is contained in:
2026-11-15 18:20:36 -05:00
parent b76aea2899
commit 577b29db39
7 changed files with 261 additions and 106 deletions

174
.gitea/workflows/build.yaml Normal file
View File

@@ -0,0 +1,174 @@
# Build the container image and publish it to this Gitea instance's
# registry.
#
# Gitea serves its container registry on the same host as the web UI, so
# the image lands at git.clortox.com/tyler/rss-to-opds. That host is
# derived from github.server_url rather than hardcoded, so this keeps
# working if the instance moves or the repository is forked. Set a
# repository variable named CONTAINER_REGISTRY to override it (for a
# separate registry host).
#
# Only actions/checkout is used. The build is plain docker rather than
# the buildx and login actions, so a job never has to reach the internet
# to fetch third-party actions -- which is the most common way a
# self-hosted runner fails.
#
# Two requirements on the runner, both documented in README.md:
#
# * Docker must be usable from the job. For a typical act_runner that
# means mounting /var/run/docker.sock into the job container. The
# guard step below fails with a clear message rather than a
# confusing one.
# * The token must be allowed to write packages. Gitea's automatic
# token usually is; if a push is refused for permissions, create a
# personal access token with the `write:package` scope and save it
# as the REGISTRY_TOKEN secret. This workflow prefers that secret
# whenever it is set.
name: build
on:
push:
branches:
- master
tags:
- "*"
pull_request:
branches:
- master
# Lets the image be rebuilt by hand from the Gitea UI.
workflow_dispatch:
env:
# The default branch: the only one that also moves `latest`.
DEFAULT_BRANCH: master
jobs:
container:
runs-on: ubuntu-latest
steps:
- name: Check out the repository
uses: actions/checkout@v4
# Fail early and legibly: without this, a runner missing Docker
# produces a bare "docker: not found" from whichever step runs.
- name: Check that docker is available
run: |
if ! command -v docker >/dev/null 2>&1; then
echo "::error::docker is not available in this job. On"
echo "act_runner, give the job the host socket, for example:"
echo "-v /var/run/docker.sock:/var/run/docker.sock"
exit 1
fi
docker version
# The gate. The Dockerfile's first stage runs the whole suite, so a
# failing test stops the image from being built or pushed at all.
# Nothing from that stage reaches the published image.
- name: Run the test suite as a build stage
run: docker build --target test .
# Exported through GITHUB_ENV rather than step outputs: fewer moving
# parts, and it works on any Gitea version.
- name: Work out the registry, image name and tags
run: |
set -eu
registry="${CONTAINER_REGISTRY:-}"
if [ -z "$registry" ]; then
# https://git.clortox.com/ -> git.clortox.com
registry="$(printf '%s' "${GITHUB_SERVER_URL:-}" |
sed -E 's#^[a-zA-Z]+://##; s#/+$##')"
fi
if [ -z "$registry" ]; then
echo "::error::cannot determine the registry host; set a"
echo "repository variable named CONTAINER_REGISTRY"
exit 1
fi
# Docker wants a lowercase repository path; usernames may not be.
path="$(printf '%s' "${GITHUB_REPOSITORY:-}" |
tr '[:upper:]' '[:lower:]')"
# Branch and tag names can hold characters a docker tag cannot
# ("feature/x", for instance), and a tag may not begin with
# "." or "-".
ref_name="${GITHUB_REF_NAME:-manual}"
safe_ref="$(printf '%s' "$ref_name" |
sed -E 's#[^A-Za-z0-9_.-]+#-#g; s#^[.-]+##')"
[ -n "$safe_ref" ] || safe_ref="manual"
# Always tag the immutable revision, so any image can be traced
# back to the commit it was built from.
sha="$(printf '%s' "${GITHUB_SHA:-0000000}" | cut -c1-7)"
tags="sha-$sha"
case "${GITHUB_REF:-}" in
refs/tags/*)
tags="$safe_ref latest $tags"
;;
*)
if [ "$ref_name" = "${DEFAULT_BRANCH:-master}" ]; then
tags="$safe_ref latest $tags"
else
tags="$safe_ref $tags"
fi
;;
esac
{
echo "REGISTRY=$registry"
echo "IMAGE=$registry/$path"
echo "TAGS=$tags"
echo "VERSION=$safe_ref"
} >> "$GITHUB_ENV"
if [ -n "${GITHUB_STEP_SUMMARY:-}" ]; then
printf 'image: %s\ntags: %s\n' "$registry/$path" "$tags" \
>> "$GITHUB_STEP_SUMMARY"
fi
echo "will build $registry/$path with tags: $tags"
# Nothing is pushed for a pull request: a fork should never be able
# to publish an image into this registry.
- name: Log in to the registry
if: github.event_name != 'pull_request'
run: |
set -eu
token="${REGISTRY_TOKEN:-${GITHUB_TOKEN:-}}"
if [ -z "$token" ]; then
echo "::error::no registry token available; set a"
echo "REGISTRY_TOKEN secret (scope: write:package)"
exit 1
fi
printf '%s' "$token" |
docker login "$REGISTRY" \
--username "${{ github.actor }}" --password-stdin
- name: Build without pushing (pull requests)
if: github.event_name == 'pull_request'
run: |
set -eu
number="${{ github.event.pull_request.number }}"
docker build --tag "$IMAGE:pr-$number" .
echo "built $IMAGE:pr-$number (not pushed)"
- name: Build and push
if: github.event_name != 'pull_request'
run: |
set -eu
# Every tag is applied in one build, so the layers build once.
# The unquoted expansion is deliberate: $TAGS is a list.
tag_args=""
for tag in $TAGS; do
tag_args="$tag_args --tag $IMAGE:$tag"
done
docker build $tag_args .
for tag in $TAGS; do
docker push "$IMAGE:$tag"
echo "pushed $IMAGE:$tag"
done
count="$(printf '%s' "$TAGS" | wc -w)"
echo "done: $count tag(s), latest is $VERSION"