diff --git a/.forgejo/workflows/release.yml b/.forgejo/workflows/release.yml new file mode 100644 index 0000000..54ccf33 --- /dev/null +++ b/.forgejo/workflows/release.yml @@ -0,0 +1,88 @@ +# 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/commit-analyzer@13.0.1 + -p @semantic-release/release-notes-generator@14.1.1 + -p @semantic-release/changelog@7.0.0 + -p @semantic-release/git@11.0.1 + -p conventional-changelog-conventionalcommits@8.0.0 + -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 diff --git a/.releaserc.json b/.releaserc.json new file mode 100644 index 0000000..a042e35 --- /dev/null +++ b/.releaserc.json @@ -0,0 +1,35 @@ +{ + "branches": ["main"], + "tagFormat": "v${version}", + "plugins": [ + ["@semantic-release/commit-analyzer", { "preset": "conventionalcommits" }], + [ + "@semantic-release/release-notes-generator", + { + "preset": "conventionalcommits", + "presetConfig": { + "types": [ + { "type": "feat", "section": "Features" }, + { "type": "fix", "section": "Bug fixes" }, + { "type": "perf", "section": "Performance" }, + { "type": "refactor", "section": "Refactoring" }, + { "type": "docs", "section": "Documentation" }, + { "type": "style", "hidden": true }, + { "type": "test", "hidden": true }, + { "type": "chore", "hidden": true }, + { "type": "ci", "hidden": true } + ] + } + } + ], + ["@semantic-release/changelog", { "changelogFile": "CHANGELOG.md", "changelogTitle": "# Changelog" }], + [ + "@semantic-release/git", + { + "assets": ["CHANGELOG.md"], + "message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}" + } + ], + ["@saithodev/semantic-release-gitea", { "giteaUrl": "https://git.kmalloc.org" }] + ] +} diff --git a/Dockerfile b/Dockerfile index c952f15..6afce61 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,7 +20,9 @@ COPY cmd ./cmd COPY internal ./internal COPY migrations ./migrations COPY --from=web /src/internal/webui/dist ./internal/webui/dist -RUN CGO_ENABLED=0 go build -tags webui -trimpath -ldflags="-s -w" -o /out/sondad ./cmd/sondad +# The version of the release (see .releaserc.json), as GET /version and the User-Agent of the providers say it. +ARG VERSION=dev +RUN CGO_ENABLED=0 go build -tags webui -trimpath -ldflags="-s -w -X main.version=${VERSION}" -o /out/sondad ./cmd/sondad FROM alpine:3.22 LABEL org.opencontainers.image.title="Sonda" \ diff --git a/Makefile b/Makefile index c2f008f..75ee9da 100644 --- a/Makefile +++ b/Makefile @@ -8,8 +8,11 @@ .PHONY: build web test +# The version is the last release tag, and how far this checkout is from it (v1.2.3-4-gabcdef), or "dev" outside git. +VERSION ?= $(shell git describe --tags --always 2>/dev/null | sed 's/^v//' || echo dev) + build: web - CGO_ENABLED=0 go build -tags webui -trimpath -ldflags="-s -w" -o bin/sondad ./cmd/sondad + CGO_ENABLED=0 go build -tags webui -trimpath -ldflags="-s -w -X main.version=$(VERSION)" -o bin/sondad ./cmd/sondad web: cd web && npm ci && npm run build diff --git a/README.md b/README.md index 8cc969a..c41f98a 100644 --- a/README.md +++ b/README.md @@ -403,6 +403,19 @@ What it does today: sign in (and create the first account of a new server), brow More commands, all run in `web/`: `npm test`, `npm run build`, and `npm run gen:api`, which rebuilds `src/api/schema.d.ts` from the OpenAPI spec (it fetches the generator with `npx`, so it needs the network once). `npm run build` writes to `internal/webui/dist` (git-ignored), not to `web/dist`. +## Releases + +Every merge into `main` can make a release, by itself (`.forgejo/workflows/release.yml`, [semantic-release](https://semantic-release.gitbook.io)). The next version comes from the messages of the commits since the last one, written as [Conventional Commits](https://www.conventionalcommits.org): + +| Commit | Release | +|---|---| +| `fix: ...`, `perf: ...` | patch (0.2.0 to 0.2.1) | +| `feat: ...` | minor (0.2.0 to 0.3.0) | +| `feat!: ...`, or `BREAKING CHANGE:` in the body | major (0.2.0 to 1.0.0) | +| `docs:`, `test:`, `chore:`, `ci:`, `style:` | none by themselves | + +A release adds its entry to [CHANGELOG.md](CHANGELOG.md), tags the commit (`v0.3.0`), makes the release on the forge, and the image is built from the tag and pushed to `git.kmalloc.org/squid/sonda` as `0.3.0`, `0.3` and `latest`. The version is inside the binary: `GET /version` and the User-Agent of the providers say it (a build that no release made says `dev`). The commit that writes `CHANGELOG.md` says `[skip ci]`. The runner has no Docker, so the image is built with [kaniko](https://github.com/GoogleContainerTools/kaniko). The workflow needs the secret `FORGEJO_TOKEN`: a token of the owner with the scopes `write:repository` and `write:package`. + ## Development ```bash diff --git a/cmd/sondad/main.go b/cmd/sondad/main.go index b10faba..88712fc 100644 --- a/cmd/sondad/main.go +++ b/cmd/sondad/main.go @@ -60,8 +60,10 @@ import ( const defaultConfigFile = "sonda.toml" -// version goes into the User-Agent sent to providers. -const version = "0.1.0" +// version goes into the User-Agent sent to providers, and is what GET /version says. The release sets it +// (-ldflags "-X main.version=1.2.3": see the Dockerfile and the Makefile); a build that was not made by a release says +// "dev". +var version = "dev" // lastfmMinInterval spaces the requests to Last.fm well below its limit. const lastfmMinInterval = 250 * time.Millisecond