Files
rss-to-opds/.gitea/workflows/build.yaml
Tyler Perkins 10c27e0810
Some checks failed
build / container (push) Failing after 5s
Request packages:write so the automatic token can push the image
The login step reached the registry but was refused:

  Error response from daemon: Get "https://git.clortox.com/v2/": unauthorized

The registry is enabled (an unauthenticated /v2/ returns a proper UNAUTHORIZED
registry error) and a personal access token authenticates against it, so the
automatic token is what lacks the scope.  Gitea's automatic token starts
read-only, so the workflow now asks for packages:write.  The REGISTRY_TOKEN
fallback stays, and is preferred when set.
2026-11-15 18:22:44 -05:00

195 lines
7.2 KiB
YAML

# 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
# Gitea's automatic token starts read-only, and pushing an image needs write
# access to packages. Asking for it here means no deployment has to manage a
# personal access token; an instance that does not grant package scope to the
# automatic token can set a REGISTRY_TOKEN secret instead, which the login step
# prefers whenever it is present.
permissions:
contents: read
packages: write
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'
# Secrets are not environment variables -- they only exist when named
# with an expression, which is what this block is for. The fallback
# lives in the shell rather than in a `||` expression so that it does
# not depend on the evaluator supporting either.
env:
TOKEN_FROM_SECRET: ${{ secrets.REGISTRY_TOKEN }}
TOKEN_FROM_GITEA: ${{ secrets.GITHUB_TOKEN }}
run: |
set -eu
token="${TOKEN_FROM_SECRET:-}"
if [ -z "$token" ]; then
token="${TOKEN_FROM_GITEA:-}"
fi
if [ -z "$token" ]; then
echo "::error::no registry token available. Gitea's automatic"
echo "token is normally enough; if a push is refused, create a"
echo "PAT with the write:package scope as REGISTRY_TOKEN."
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"