ci: release by itself at every merge, with semantic-release

At every merge into main a workflow reads the Conventional Commits since the last release,
works out the version, writes CHANGELOG.md, tags the commit and makes the release on the
forge; when there is a release it builds the image from the tag and pushes it to
git.kmalloc.org/squid/sonda as X.Y.Z, X.Y and latest. The runner has no Docker, so the
image is built with kaniko.

The version is no longer written in the code: the release sets it (-X main.version) in the
Dockerfile and the Makefile, and a build that no release made says dev.
This commit is contained in:
tgiachi 2026-11-16 13:07:17 +01:00
commit a478c95536

View file

@ -0,0 +1,86 @@
# What happens at every merge into main:
# 1. semantic-release reads the commits since the last release (Conventional Commits), works out the next version,
# writes CHANGELOG.md, makes the release on the forge and tags it. A merge with nothing to release (docs, tests, ci)
# makes none.
# 2. When there is a release, the image is built from the tag and pushed to git.kmalloc.org/squid/sonda as
# X.Y.Z, X.Y and latest.
# The runner has no Docker, so the image is built with kaniko, which needs no daemon.
# Secret needed: FORGEJO_TOKEN, a token of the owner with the scopes write:repository and write:package.
name: release
on:
push:
branches: [main]
# One release at a time: two merges close together must not make two versions at once.
concurrency:
group: release
cancel-in-progress: false
jobs:
release:
runs-on: docker
# The commit that the release makes (CHANGELOG.md) says [skip ci]; this is the belt for those braces.
if: ${{ !contains(github.event.head_commit.message, '[skip ci]') }}
outputs:
version: ${{ steps.version.outputs.version }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # the whole history: the next version depends on every commit since the last tag
token: ${{ secrets.FORGEJO_TOKEN }}
- uses: actions/setup-node@v4
with:
node-version: 24
- name: Release
env:
GITEA_TOKEN: ${{ secrets.FORGEJO_TOKEN }}
GITEA_URL: https://git.kmalloc.org
GIT_AUTHOR_NAME: sonda-release
GIT_AUTHOR_EMAIL: squid@kmalloc.org
GIT_COMMITTER_NAME: sonda-release
GIT_COMMITTER_EMAIL: squid@kmalloc.org
run: >
npx --yes
-p semantic-release@25
-p @semantic-release/changelog@7.0.0
-p @semantic-release/git@11.0.1
-p conventional-changelog-conventionalcommits@10.4.1
-p @saithodev/semantic-release-gitea@2.1.0
semantic-release
- name: Which version was made, if any
id: version
run: |
# The tag of a release points at the commit the release made, which is where the checkout now is.
tag="$(git tag --points-at HEAD | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | head -n 1 || true)"
echo "version=${tag#v}" >> "$GITHUB_OUTPUT"
echo "released: ${tag:-nothing}"
image:
needs: release
if: ${{ needs.release.outputs.version != '' }}
runs-on: docker
container:
image: gcr.io/kaniko-project/executor:v1.23.2-debug
steps:
- name: Build the image from the tag and push it
env:
VERSION: ${{ needs.release.outputs.version }}
REGISTRY_USER: squid
REGISTRY_TOKEN: ${{ secrets.FORGEJO_TOKEN }}
run: |
set -eu
mkdir -p /kaniko/.docker
auth="$(printf '%s:%s' "$REGISTRY_USER" "$REGISTRY_TOKEN" | base64 | tr -d '\n')"
printf '{"auths":{"git.kmalloc.org":{"auth":"%s"}}}' "$auth" > /kaniko/.docker/config.json
image=git.kmalloc.org/squid/sonda
minor="${VERSION%.*}"
/kaniko/executor \
--dockerfile Dockerfile \
--context "git://git.kmalloc.org/squid/sonda.git#refs/tags/v${VERSION}" \
--build-arg "VERSION=${VERSION}" \
--label "org.opencontainers.image.version=${VERSION}" \
--destination "${image}:${VERSION}" \
--destination "${image}:${minor}" \
--destination "${image}:latest" \
--verbosity info