diff --git a/.github/scripts/changelog.mjs b/.github/scripts/changelog.mjs new file mode 100644 index 0000000..0306bca --- /dev/null +++ b/.github/scripts/changelog.mjs @@ -0,0 +1,33 @@ +// Print one version's section of CHANGELOG.md, for use as release notes. +// +// node changelog.mjs [CHANGELOG.md] +// +// The section starts at a "## v0.1.0" heading (the "v" is optional) and runs +// to the next "## " heading. Exits non-zero if it's missing or empty, so a +// release can't go out without notes. +import { readFileSync } from "node:fs"; + +function fail(msg) { + console.error(`::error::${msg}`); + process.exit(1); +} + +const [tag, path = "CHANGELOG.md"] = process.argv.slice(2); +if (!tag) fail("usage: changelog.mjs [CHANGELOG.md]"); + +const version = tag.replace(/^v/, "").replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); +const heading = new RegExp(`^##\\s+v?${version}(\\s|$)`); +const lines = readFileSync(path, "utf8").split(/\r?\n/); + +const start = lines.findIndex((l) => heading.test(l)); +if (start < 0) { + fail( + `${path} has no "## ${tag}" section. Add one, commit it, then move the tag to that commit and push it again.`, + ); +} +let end = lines.findIndex((l, i) => i > start && /^##\s/.test(l)); +if (end < 0) end = lines.length; + +const notes = lines.slice(start + 1, end).join("\n").trim(); +if (!notes) fail(`The "## ${tag}" section of ${path} is empty.`); +process.stdout.write(`${notes}\n`); diff --git a/.github/scripts/gitea-release.mjs b/.github/scripts/gitea-release.mjs index 3dded4a..8c22540 100644 --- a/.github/scripts/gitea-release.mjs +++ b/.github/scripts/gitea-release.mjs @@ -1,11 +1,12 @@ // Create (or reuse) the Gitea release for a tag and upload files to it. // -// node gitea-release.mjs ... +// node gitea-release.mjs ... // -// Reads GITHUB_SERVER_URL, GITHUB_REPOSITORY and GITHUB_SHA (set by Gitea -// Actions) and GITEA_TOKEN. Safe to re-run: an existing release is reused and -// assets with the same name are replaced. -import { openAsBlob } from "node:fs"; +// The release notes are the contents of . Reads GITHUB_SERVER_URL +// and GITHUB_REPOSITORY (set by Gitea Actions) and GITEA_TOKEN. Safe to +// re-run: an existing release is reused, its notes are updated, and assets +// with the same name are replaced. +import { openAsBlob, readFileSync } from "node:fs"; import { basename } from "node:path"; function fail(msg) { @@ -13,11 +14,14 @@ function fail(msg) { process.exit(1); } -const [tag, ...files] = process.argv.slice(2); -if (!tag || files.length === 0) fail("usage: gitea-release.mjs ..."); +const [tag, notesFile, ...files] = process.argv.slice(2); +if (!tag || !notesFile || files.length === 0) { + fail("usage: gitea-release.mjs ..."); +} +const notes = readFileSync(notesFile, "utf8").trim(); +if (!notes) fail(`${notesFile} is empty`); -const { GITHUB_SERVER_URL: server, GITHUB_REPOSITORY: repo, GITHUB_SHA: sha, GITEA_TOKEN: token } = - process.env; +const { GITHUB_SERVER_URL: server, GITHUB_REPOSITORY: repo, GITEA_TOKEN: token } = process.env; if (!server || !repo || !token) fail("GITHUB_SERVER_URL, GITHUB_REPOSITORY and GITEA_TOKEN must be set"); const api = `${server.replace(/\/+$/, "")}/api/v1/repos/${repo}`; @@ -37,16 +41,13 @@ async function call(method, path, { json, form, allow404 = false } = {}) { return res.status === 204 ? null : res.json(); } -const archive = files.map((f) => basename(f)).find((n) => n.endsWith(".tar.gz")); -const notes = [ - "Static Linux binary with the web UI built in.", - sha ? `\nBuilt from ${sha}.` : "", - archive ? `\nCheck the download with \`sha256sum -c ${archive}.sha256\`.` : "", -].join(""); - let release = await call("GET", `/releases/tags/${encodeURIComponent(tag)}`, { allow404: true }); if (release) { console.log(`Reusing release ${tag} (id ${release.id})`); + if ((release.body ?? "").trim() !== notes) { + release = await call("PATCH", `/releases/${release.id}`, { json: { body: notes } }); + console.log("Updated the release notes"); + } } else { release = await call("POST", "/releases", { json: { tag_name: tag, name: tag, body: notes, draft: false, prerelease: tag.includes("-") }, diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 3ab8532..2d258d3 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -2,7 +2,8 @@ name: Release # Push a tag such as v0.1.0 to build a static Linux binary (web UI included) # and publish it as a Gitea release. The tag must match the version in -# Cargo.toml's [workspace.package]. +# Cargo.toml's [workspace.package], and CHANGELOG.md must have a section for +# it: that section becomes the release notes. # # Written for Gitea Actions (act_runner). It lives in .github/workflows so the # CI workflow next to it keeps running too: Gitea ignores .github/workflows @@ -36,6 +37,14 @@ jobs: - uses: actions/setup-node@v4 with: { node-version: 22, cache: npm, cache-dependency-path: ui/package-lock.json } + + # Before the long build, so a missing entry fails in seconds. + - name: Read the release notes from CHANGELOG.md + run: | + mkdir -p dist + node .github/scripts/changelog.mjs "$GITHUB_REF_NAME" > dist/release-notes.md + cat dist/release-notes.md + - name: Build the UI run: | npm --prefix ui ci @@ -74,4 +83,6 @@ jobs: - name: Publish the release env: GITEA_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: node .github/scripts/gitea-release.mjs "$GITHUB_REF_NAME" "$ASSET" "$ASSET.sha256" + run: >- + node .github/scripts/gitea-release.mjs "$GITHUB_REF_NAME" dist/release-notes.md + "$ASSET" "$ASSET.sha256" diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..7b86286 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,7 @@ +# Changelog + +## v0.1.0 + +### Added + +- **Initial release.** diff --git a/README.md b/README.md index eb1f2e8..1aa33cd 100644 --- a/README.md +++ b/README.md @@ -91,16 +91,22 @@ Interrupted transfers resume after a restart. ### Making a release -`.github/workflows/release.yml` runs on Gitea Actions. To cut a release, bump -`version` in the root `Cargo.toml`, commit, then push a matching tag: +`.github/workflows/release.yml` runs on Gitea Actions. To cut a release: + +1. Bump `version` in the root `Cargo.toml`. +2. Add a `## v0.1.1` section to the top of `CHANGELOG.md`, using whichever of + `### Added`, `### Updated`, `### Fixed` and `### Removed` apply. +3. Commit, then push a matching tag: ```sh git tag v0.1.1 && git push origin v0.1.1 ``` The workflow runs the tests, builds the UI and binary, and attaches the -tarball and its `.sha256` to a Gitea release for that tag. Re-running it -replaces the files. Tags with a `-` (`v0.2.0-rc1`) are marked pre-release. +tarball and its `.sha256` to a Gitea release for that tag. The release notes +are that version's section of `CHANGELOG.md`; the workflow stops straight +away if there isn't one. Re-running it replaces the files and refreshes the +notes. Tags with a `-` (`v0.2.0-rc1`) are marked pre-release. ### Library mapping