ci: release automation from Conventional Commits
tools/release derives the next version from the commits since the last stable release and renders the changelog. `make release` tags it on main, CHANNEL=beta a numbered prerelease of the coming version; pushing the tag runs the release workflow, which publishes the release with its notes, Linux binaries and checksums. Images get channel tags: edge for main, beta for prereleases, latest, stable and X.Y for releases. Closes #33 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
1ad68e2e87
commit
68a7827475
8 files changed
+644
-5
No files matched your search
@@ -0,0 +1,156 @@
|
||||
// Command release derives versions and release notes from Conventional
|
||||
// Commits.
|
||||
//
|
||||
// release next [stable|beta] print the tag the next release gets
|
||||
// release notes [-json] <tag> print the release notes of a tag, with
|
||||
// -json as the body of a Gitea release
|
||||
// release check <tag> fail unless tag is a valid release tag
|
||||
//
|
||||
// Stable releases follow semantic versioning from the commits since the
|
||||
// last stable release: breaking changes move the major version (the minor
|
||||
// one before 1.0), features the minor and fixes the patch version. Betas
|
||||
// are numbered prereleases of the coming stable version.
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const usage = `usage: release next [stable|beta] | notes [-json] <tag> | check <tag>`
|
||||
|
||||
func main() {
|
||||
if err := run(os.Args[1:]); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func run(args []string) error {
|
||||
if len(args) == 0 {
|
||||
return usageError{}
|
||||
}
|
||||
switch args[0] {
|
||||
case "next":
|
||||
channel := "stable"
|
||||
if len(args) > 1 {
|
||||
channel = args[1]
|
||||
}
|
||||
tags, err := gitTags()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
from := ""
|
||||
if base := lastStable(tags); base != (version{}) {
|
||||
from = base.String()
|
||||
}
|
||||
commits, err := gitCommits(from, "HEAD")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
v, err := next(tags, commits, channel)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(v)
|
||||
case "notes":
|
||||
asJSON := len(args) == 3 && args[1] == "-json"
|
||||
if asJSON {
|
||||
args = args[1:]
|
||||
}
|
||||
if len(args) != 2 {
|
||||
return usageError{}
|
||||
}
|
||||
v, err := parseVersion(args[1])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tags, err := gitTags()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
prev, ok := previous(tags, v)
|
||||
from := ""
|
||||
var pp *version
|
||||
if ok {
|
||||
from, pp = prev.String(), &prev
|
||||
}
|
||||
commits, err := gitCommits(from, v.String())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
text := notes(v, pp, commits, repoURL())
|
||||
if !asJSON {
|
||||
fmt.Print(text)
|
||||
return nil
|
||||
}
|
||||
return json.NewEncoder(os.Stdout).Encode(map[string]any{
|
||||
"tag_name": v.String(), "name": v.String(), "body": text, "prerelease": v.Beta > 0,
|
||||
})
|
||||
case "check":
|
||||
if len(args) != 2 {
|
||||
return usageError{}
|
||||
}
|
||||
_, err := parseVersion(args[1])
|
||||
return err
|
||||
default:
|
||||
return usageError{}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type usageError struct{}
|
||||
|
||||
func (usageError) Error() string { return usage }
|
||||
|
||||
func git(args ...string) (string, error) {
|
||||
out, err := exec.Command("git", args...).Output()
|
||||
var exit *exec.ExitError
|
||||
if errors.As(err, &exit) {
|
||||
return "", fmt.Errorf("git %s: %s", strings.Join(args, " "), strings.TrimSpace(string(exit.Stderr)))
|
||||
}
|
||||
return string(out), err
|
||||
}
|
||||
|
||||
func gitTags() ([]version, error) {
|
||||
out, err := git("tag", "--list", "v*")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return parseTags(strings.Split(out, "\n")), nil
|
||||
}
|
||||
|
||||
// gitCommits lists the commits after from up to to, oldest first, without
|
||||
// merge commits. An empty from means the whole history.
|
||||
func gitCommits(from, to string) ([]commit, error) {
|
||||
rng := to
|
||||
if from != "" {
|
||||
rng = from + ".." + to
|
||||
}
|
||||
out, err := git("log", "--reverse", "--no-merges", "--format=%H%x1f%s%x1f%b%x1e", rng, "--")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var commits []commit
|
||||
for rec := range strings.SplitSeq(out, "\x1e") {
|
||||
f := strings.SplitN(strings.TrimLeft(rec, "\n"), "\x1f", 3)
|
||||
if len(f) != 3 {
|
||||
continue
|
||||
}
|
||||
commits = append(commits, commit{Hash: f[0], Subject: f[1], Body: f[2]})
|
||||
}
|
||||
return commits, nil
|
||||
}
|
||||
|
||||
// repoURL is the repository's web address in CI, for links in the notes.
|
||||
func repoURL() string {
|
||||
server, repo := os.Getenv("GITHUB_SERVER_URL"), os.Getenv("GITHUB_REPOSITORY")
|
||||
if server == "" || repo == "" {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSuffix(server, "/") + "/" + repo
|
||||
}
|
||||
Reference in new issue
Block a user