Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 55 additions & 15 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,42 +1,82 @@
name: Release

# One-click release: pick a semver bump (patch, minor, or major) and this
# workflow bumps the shared workspace version in Cargo.toml/Cargo.lock, commits
# `chore(release): vX.Y.Z` to main, pushes a matching annotated tag, builds the
# `medulla` binary for each supported target from that tag, and publishes a
# GitHub Release with the packaged binaries and `latest.json` attached.
#
# Always releases from main, whatever ref it is dispatched on. The bump commit
# and tag go up in one atomic push, so a rejected push (main moved underneath
# the run) leaves neither behind. The release is created in this same workflow,
# so GITHUB_TOKEN pushes not triggering other workflows is fine.

on:
workflow_dispatch:
inputs:
release_type:
description: Semver increment to apply to the current version.
required: true
default: patch
type: choice
options: [patch, minor, major]

permissions:
contents: write

concurrency:
group: release-${{ github.ref }}
# One release at a time: two concurrent bumps would race for the same tag.
group: release
cancel-in-progress: false

jobs:
prepare:
name: Tag current main version
if: github.ref == 'refs/heads/main'
name: Bump version and tag (${{ inputs.release_type }})
runs-on: ubuntu-latest
outputs:
tag: ${{ steps.version.outputs.tag }}
version: ${{ steps.version.outputs.version }}
tag: ${{ steps.bump.outputs.tag }}
version: ${{ steps.bump.outputs.version }}
steps:
- uses: actions/checkout@v7
with:
ref: main
fetch-depth: 0

- name: Read version and create tag
id: version
- name: Configure git identity
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"

- name: Test the bump script
run: bash scripts/release/test-bump-version.sh

- name: Bump version
id: bump
run: bash scripts/release/bump-version.sh "${{ inputs.release_type }}"

- name: Ensure tag does not already exist
env:
TAG: ${{ steps.bump.outputs.tag }}
run: |
set -euo pipefail
version="$(sed -n 's/^version = "\(.*\)"$/\1/p' Cargo.toml | head -1)"
tag="v${version}"
if git ls-remote --exit-code --tags origin "refs/tags/${tag}" >/dev/null 2>&1; then
echo "Release tag ${tag} already exists" >&2
git fetch origin --tags --force
if git rev-parse -q --verify "refs/tags/${TAG}" >/dev/null ||
git ls-remote --exit-code --tags origin "refs/tags/${TAG}" >/dev/null; then
echo "Release tag ${TAG} already exists" >&2
exit 1
fi
git tag -a "${tag}" -m "${tag}"
git push origin "refs/tags/${tag}"
echo "version=${version}" >> "$GITHUB_OUTPUT"
echo "tag=${tag}" >> "$GITHUB_OUTPUT"

- name: Commit, tag, and push
env:
TAG: ${{ steps.bump.outputs.tag }}
run: |
set -euo pipefail
git add Cargo.toml Cargo.lock
git commit -m "chore(release): ${TAG}"
# Annotated tag + explicit ref push: --follow-tags skips lightweight
# tags, which would leave the build jobs nothing to check out.
git tag -a "${TAG}" -m "${TAG}"
git push --atomic origin HEAD:refs/heads/main "refs/tags/${TAG}"

build:
needs: prepare
Expand Down
22 changes: 15 additions & 7 deletions DEVELOPING.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,18 @@ for the full loop and the release process.

## Releasing

`Release` (`.github/workflows/release.yml`, dispatched manually) bumps the
version here, tags it here, builds the binary for every supported target, and
publishes the packaged artifacts — binaries, checksums, and the `latest.json`
update manifest `medulla update` reads — as a GitHub Release on the public
[tinyhumansai/medulla](https://github.com/tinyhumansai/medulla) repository. It
authenticates there with a GitHub App installation token scoped to that one
repository. Only build output crosses the boundary; no source is pushed there.
`Release` (`.github/workflows/release.yml`, dispatched manually on `main`)
takes a `release_type` of `patch`, `minor`, or `major`. It bumps the shared
`[workspace.package]` version and the workspace entries in `Cargo.lock` with
`scripts/release/bump-version.sh`, commits `chore(release): vX.Y.Z` to `main`,
pushes the matching annotated tag, builds the binary for every supported target
from that tag, and publishes the packaged artifacts — binaries, checksums, and
the `latest.json` update manifest `medulla update` reads — as a GitHub Release.

```sh
gh workflow run release.yml -R tinyhumansai/medulla -f release_type=minor
Comment thread
senamakel marked this conversation as resolved.
Resolved
```

`bash scripts/release/test-bump-version.sh` exercises the bump script offline;
`bash scripts/release/bump-version.sh minor` shows the bump on your own tree
(revert `Cargo.toml` and `Cargo.lock` afterwards).
97 changes: 97 additions & 0 deletions scripts/release/bump-version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#!/usr/bin/env bash
#
# bump-version.sh — apply a semver bump to the workspace version.
#
# Usage: scripts/release/bump-version.sh <patch|minor|major> [repo-root]
#
# The `[workspace.package]` version in the root Cargo.toml is the one version
# every member crate inherits. Bumping it also means rewriting the lockfile's
# entries for the workspace's own packages, or `cargo build --locked` in the
# release jobs rejects the tree. This script edits both files, verifies that
# they agree, and prints `version=` / `tag=` lines (also appended to
# $GITHUB_OUTPUT when that is set). It does not commit, tag, or push.

set -euo pipefail

bump="${1:-}"
root="${2:-.}"
cargo_toml="${root}/Cargo.toml"
cargo_lock="${root}/Cargo.lock"
# Workspace members whose lockfile entries carry the shared version.
packages=(medulla medulla-link medulla-tui)

case "$bump" in
patch | minor | major) ;;
*)
echo "usage: $0 <patch|minor|major> [repo-root]" >&2
exit 2
;;
esac

# The first `version = "…"` line is the [workspace.package] one; dependency
# versions are written inline (`{ version = … }`) and never start a line.
current="$(sed -n 's/^version = "\(.*\)"$/\1/p' "$cargo_toml" | head -1)"
if [[ ! "$current" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
echo "Cargo.toml workspace version '${current}' is not plain MAJOR.MINOR.PATCH" >&2
exit 1
fi
major="${BASH_REMATCH[1]}"
minor="${BASH_REMATCH[2]}"
patch="${BASH_REMATCH[3]}"

case "$bump" in
major) next="$((major + 1)).0.0" ;;
minor) next="${major}.$((minor + 1)).0" ;;
patch) next="${major}.${minor}.$((patch + 1))" ;;
esac

python3 - "$cargo_toml" "$cargo_lock" "$current" "$next" "${packages[@]}" <<'PY'
import sys

toml_path, lock_path, current, new, *packages = sys.argv[1:]

with open(toml_path, encoding="utf-8") as f:
toml = f.read()
old_line = f'\nversion = "{current}"\n'
if old_line not in toml:
raise SystemExit(f"{toml_path}: no workspace version line for {current}")
toml = toml.replace(old_line, f'\nversion = "{new}"\n', 1)

with open(lock_path, encoding="utf-8") as f:
lock = f.read()
for name in packages:
old = f'name = "{name}"\nversion = "{current}"\n'
if old not in lock:
raise SystemExit(f"{lock_path}: no {name} package at version {current}")
lock = lock.replace(old, f'name = "{name}"\nversion = "{new}"\n', 1)

with open(toml_path, "w", encoding="utf-8") as f:
f.write(toml)
with open(lock_path, "w", encoding="utf-8") as f:
f.write(lock)
PY

# Re-read both files so a partial edit cannot slip through as a release.
written="$(sed -n 's/^version = "\(.*\)"$/\1/p' "$cargo_toml" | head -1)"
if [[ "$written" != "$next" ]]; then
echo "Cargo.toml version is ${written} after bump, expected ${next}" >&2
exit 1
fi
for name in "${packages[@]}"; do
if ! grep -qzF "name = \"${name}\"
version = \"${next}\"" "$cargo_lock"; then
echo "Cargo.lock ${name} is not at ${next} after bump" >&2
exit 1
fi
done

echo "Bumped ${current} -> ${next} (${bump})" >&2
echo "version=${next}"
echo "tag=v${next}"
if [[ -n "${GITHUB_OUTPUT:-}" ]]; then
{
echo "previous=${current}"
echo "version=${next}"
echo "tag=v${next}"
} >> "$GITHUB_OUTPUT"
fi
83 changes: 83 additions & 0 deletions scripts/release/test-bump-version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/env bash
#
# test-bump-version.sh — exercise bump-version.sh against a scratch copy of a
# minimal manifest and lockfile. Run from the repository root; no cargo needed.

set -euo pipefail

script="$(cd "$(dirname "$0")" && pwd)/bump-version.sh"
work="$(mktemp -d)"
trap 'rm -rf "$work"' EXIT
failures=0

seed() {
cat > "$work/Cargo.toml" <<'EOF'
[workspace.package]
version = "0.14.1"

[workspace.dependencies]
serde = { version = "1" }
EOF
cat > "$work/Cargo.lock" <<'EOF'
[[package]]
name = "medulla"
version = "0.14.1"

[[package]]
name = "medulla-link"
version = "0.14.1"

[[package]]
name = "medulla-tui"
version = "0.14.1"

[[package]]
name = "other"
version = "0.14.1"
EOF
}

expect_bump() {
local kind="$1" want="$2" out
seed
out="$(bash "$script" "$kind" "$work" 2>/dev/null)"
if [[ "$out" != "version=${want}"$'\n'"tag=v${want}" ]]; then
echo "FAIL ${kind}: got '${out}'"
failures=$((failures + 1))
return
fi
if [[ "$(grep -c "version = \"${want}\"" "$work/Cargo.lock")" != 3 ]] ||
! grep -q '^name = "other"$' "$work/Cargo.lock" ||
[[ "$(grep -A1 '^name = "other"$' "$work/Cargo.lock" | tail -1)" != 'version = "0.14.1"' ]]; then
echo "FAIL ${kind}: lockfile not rewritten for exactly the workspace packages"
failures=$((failures + 1))
return
fi
echo "ok ${kind} -> ${want}"
}

expect_bump patch 0.14.2
expect_bump minor 0.15.0
expect_bump major 1.0.0

seed
if bash "$script" bogus "$work" >/dev/null 2>&1; then
echo "FAIL rejects unknown bump type"
failures=$((failures + 1))
else
echo "ok rejects unknown bump type"
fi

seed
sed -i 's/name = "medulla-tui"/name = "renamed"/' "$work/Cargo.lock"
if bash "$script" patch "$work" >/dev/null 2>&1; then
echo "FAIL rejects a lockfile missing a workspace package"
failures=$((failures + 1))
elif ! grep -q '^version = "0.14.1"$' "$work/Cargo.toml"; then
echo "FAIL left Cargo.toml edited after a lockfile failure"
failures=$((failures + 1))
else
echo "ok rejects a lockfile missing a workspace package"
fi

exit "$failures"
Loading