Skip to content
Open
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
125 changes: 125 additions & 0 deletions .github/workflows/release-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# Keeps one pull request, from release/plugins, that releases every plugin whose image changed since its last release
# (tools/release), and tags the versions it leaves on main once merged. Each run, for a push to main:
#
# 1. tags each version at the top of a plugin's CHANGELOG.md that has no tag yet, on the commit of the push: the merge
# of the release pull request. Each tag starts the release workflow, which publishes that plugin's image.
# 2. writes the next version of each plugin with a change since its last tag to its CHANGELOG.md, and force-pushes
# that, on the commit of the push, to release/plugins; then opens the pull request, or updates it. With nothing to
# release, it closes the pull request.
#
# It acts as the release App, whose key is in the environment `release`, which only main may use. A tag created or a
# branch pushed with GITHUB_TOKEN starts no workflow: the tags would publish nothing, and CI would not run on the pull
# request. The App may also create the tags plugins/*/v*, which the repository's tag ruleset forbids to others.
name: Release pull request

on:
push:
branches: [main]
workflow_dispatch:

permissions:
contents: read

concurrency:
group: release-pr
cancel-in-progress: false

env:
# Use exactly the Go version of .tool-versions; never download another toolchain.
GOTOOLCHAIN: local
GOWORK: "off"
BRANCH: release/plugins

defaults:
run:
# Named, so that a step runs with -o pipefail.
shell: bash

jobs:
release-pr:
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
environment: release
steps:
- name: A token of the release App
id: app
uses: actions/create-github-app-token@f418cff399f7aef4c2f0ad47e25ca7a6abbdd6fc # v3.4.0
with:
app-id: ${{ vars.RELEASE_APP_ID }}
private-key: ${{ secrets.RELEASE_APP_PRIVATE_KEY }}
permission-contents: write
permission-pull-requests: write
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
# The whole history with the tags: the last release of a plugin is its highest tag.
fetch-depth: 0
fetch-tags: true
persist-credentials: false
- uses: actions/setup-go@d8cbb8045d7f43706b9177cceb36c1f91a6644d5 # v7.1.0
with:
go-version-file: .tool-versions
cache-dependency-path: |
go.sum
tools/go.sum
plugins/*/go.sum
- name: Tag the versions of the changelogs
env:
GH_TOKEN: ${{ steps.app.outputs.token }}
REPOSITORY: ${{ github.repository }}
run: |
tags="$(go run -C tools ./cmd/release tags -root ..)"
for tag in $tags; do
gh api "repos/$REPOSITORY/git/refs" -f "ref=refs/tags/$tag" -f "sha=$GITHUB_SHA" > /dev/null
# And here, so that the next step counts it as released.
git tag "$tag" "$GITHUB_SHA"
echo "Tagged \`$tag\` on $GITHUB_SHA." >> "$GITHUB_STEP_SUMMARY"
done
- name: Write the next versions to the changelogs
id: prepare
env:
REPO_URL: ${{ github.server_url }}/${{ github.repository }}
run: go run -C tools ./cmd/release prepare -root .. -repo-url "$REPO_URL" -body "$RUNNER_TEMP/body.md" -github-output
- name: Open or update the pull request
if: steps.prepare.outputs.count != '0'
env:
GH_TOKEN: ${{ steps.app.outputs.token }}
REPOSITORY: ${{ github.repository }}
OWNER: ${{ github.repository_owner }}
SLUG: ${{ steps.app.outputs.app-slug }}
TITLE: ${{ steps.prepare.outputs.title }}
run: |
id="$(gh api "users/$SLUG%5Bbot%5D" --jq .id)"
git config user.name "${SLUG}[bot]"
git config user.email "${id}+${SLUG}[bot]@users.noreply.github.com"
git switch --quiet -c "$BRANCH"
git add plugins/*/CHANGELOG.md
git commit --quiet -m "$TITLE"

# The App's token for this push only; the checkout keeps no credentials.
auth="$(printf 'x-access-token:%s' "$GH_TOKEN" | base64 -w0)"
echo "::add-mask::$auth"
git -c "http.https://github.com/.extraheader=AUTHORIZATION: basic $auth" \
push --force --quiet origin "HEAD:refs/heads/$BRANCH"

number="$(gh api "repos/$REPOSITORY/pulls?head=$OWNER:$BRANCH&state=open" --jq '.[0].number // empty')"
if [ -z "$number" ]; then
url="$(gh api "repos/$REPOSITORY/pulls" -f title="$TITLE" -f head="$BRANCH" -f base=main \
-F body=@"$RUNNER_TEMP/body.md" --jq .html_url)"
else
url="$(gh api -X PATCH "repos/$REPOSITORY/pulls/$number" -f title="$TITLE" \
-F body=@"$RUNNER_TEMP/body.md" --jq .html_url)"
fi
echo "[$TITLE]($url)" >> "$GITHUB_STEP_SUMMARY"
- name: Close the pull request, with nothing to release
if: steps.prepare.outputs.count == '0'
env:
GH_TOKEN: ${{ steps.app.outputs.token }}
REPOSITORY: ${{ github.repository }}
OWNER: ${{ github.repository_owner }}
run: |
number="$(gh api "repos/$REPOSITORY/pulls?head=$OWNER:$BRANCH&state=open" --jq '.[0].number // empty')"
if [ -n "$number" ]; then
gh api -X PATCH "repos/$REPOSITORY/pulls/$number" -f state=closed > /dev/null
gh api -X DELETE "repos/$REPOSITORY/git/refs/heads/$BRANCH" > /dev/null || true
echo "Nothing to release: closed #$number." >> "$GITHUB_STEP_SUMMARY"
fi
34 changes: 23 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,33 @@ Each plugin is released on its own, as its container image, `ghcr.io/mycroblog/<
linux/arm64. Its `go.mod` replaces the root module with `../..`, so it builds from a checkout of this repository, not
with `go install …@version`. There are no GitHub releases: the images are the release.

To release a plugin, tag a commit of main with `plugins/<name>/v<version>` and push the tag:
Releases go through one pull request, from `release/plugins`, that the [release pull request
workflow](.github/workflows/release-pr.yml) keeps up to date with main. It lists each plugin whose image changed since
its last release, with its next version, and writes that version to the plugin's `CHANGELOG.md`, with the commits that
reach its image. Merging it tags each version on main, `plugins/<name>/v<version>`, as the release App.

- **What needs a release**: a change to the plugin's image. That is its own files, but its tests, its Markdown and
`plugin.yaml`; the code of the root module it builds; the modules of its build; `.dockerignore`; and `.tool-versions`.
A change to tests, to CI, or to `tools/` and `scripts/` releases nothing. A plugin never released is released at
`0.1.0`.
- **The next version**, from the [Conventional Commits](https://www.conventionalcommits.org/) that reach the image: a
breaking change (`feat!:`, or a `BREAKING CHANGE:` footer) moves the major version, a `feat` the minor version, and
anything else the patch version. Before 1.0.0, a breaking change moves the minor version, as a `feat` does. After a
pre-release, the next version is its release: a fix after `0.2.0-rc.1` is `0.2.0`.
- **Another version**, such as a pre-release or 1.0.0: edit the first heading of the plugin's `CHANGELOG.md` in the
pull request, such as `## 0.2.0-rc.1 (2026-11-27)`. To leave a plugin out, revert its `CHANGELOG.md` there. Each push
to main makes the pull request again, which drops such edits, so make them just before merging.

The last release of a plugin is its highest tag, and a version at the top of a `CHANGELOG.md` with no tag yet is tagged
on the next push to main, whoever wrote it there. To see what the pull request would hold:

```sh
git tag plugins/my-media-ffmpeg/v0.1.0 origin/main
git push origin plugins/my-media-ffmpeg/v0.1.0
GOWORK=off go run -C tools ./cmd/release plan -root ..
```

The tags `plugins/*/v*` are protected by a ruleset: only the release App, and administrators, may create them. An
administrator may still tag a commit of main by hand, as the workflow does.

The [release workflow](.github/workflows/release.yml) runs the plugin's integration tests on each architecture, pushes
the images, joins them into one index, and attests its provenance. It refuses a tag on a commit that is not on main, and
a version that is already published: a published version never changes.
Expand All @@ -137,14 +157,6 @@ a version that is already published: a published version never changes.
There is no `latest`: a plugin speaks the protocol of the version of mycroblog/my that `buf.gen.yaml` pins, so a
deployment names the version it runs. Each image carries that version in its label `dev.mycroblog.plugin.protocol`.

A plugin needs a new version when its build changed since its last release: its own files, the code of the root module
it imports, its Go version, or the packages of its image. affected tells which, though a change to the harness (CI,
`tools/`, `scripts/`) lists every plugin:

```sh
GOWORK=off go run -C tools ./cmd/affected -root .. -base plugins/my-media-ffmpeg/v0.1.0
```

Check an image before you run it:

```sh
Expand Down
131 changes: 79 additions & 52 deletions tools/affected/affected.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,10 @@ func Compute(ctx context.Context, root, base string) (Result, error) {
if err != nil {
return Result{}, err
}
mergeBase, err := git(ctx, root, "merge-base", base, "HEAD")
if err != nil {
return Result{}, err
}
mergeBase = strings.TrimSpace(mergeBase)
out, err := git(ctx, root, "diff", "--name-only", "--no-renames", mergeBase)
if err != nil {
return Result{}, err
}
untracked, err := git(ctx, root, "ls-files", "--others", "--exclude-standard")
mergeBase, changed, err := changedSince(ctx, root, base)
if err != nil {
return Result{}, err
}
changed := slices.Compact(slices.Sorted(slices.Values(append(lines(out), lines(untracked)...))))

plugins, err := pluginNames(root)
if err != nil {
Expand Down Expand Up @@ -130,6 +120,24 @@ func Compute(ctx context.Context, root, base string) (Result, error) {
return res, nil
}

// changedSince returns the merge base of base and HEAD, and the files changed since it, working tree included.
func changedSince(ctx context.Context, root, base string) (string, []string, error) {
mergeBase, err := git(ctx, root, "merge-base", base, "HEAD")
if err != nil {
return "", nil, err
}
mergeBase = strings.TrimSpace(mergeBase)
out, err := git(ctx, root, "diff", "--name-only", "--no-renames", mergeBase)
if err != nil {
return "", nil, err
}
untracked, err := git(ctx, root, "ls-files", "--others", "--exclude-standard")
if err != nil {
return "", nil, err
}
return mergeBase, slices.Compact(slices.Sorted(slices.Values(append(lines(out), lines(untracked)...)))), nil
}

func isHarness(f string) bool {
return slices.ContainsFunc(harness, func(h string) bool {
return f == h || strings.HasSuffix(h, "/") && strings.HasPrefix(f, h)
Expand Down Expand Up @@ -299,17 +307,42 @@ func (b build) modules(ctx context.Context, root string) (map[string]string, err
// affected returns the affected packages of the main module of the build, given the files changed since the checkout
// of the merge base at baseDir.
func (b build) affected(ctx context.Context, baseDir string, changed []string) ([]string, error) {
nodes, err := b.list(ctx)
nodes, code, err := b.code(ctx, baseDir, changed)
if err != nil {
return nil, err
}
mainPath := moduleOf(b.dir)
var out []string
for ip, n := range nodes {
// Only the packages of the build's own module: the root module's are tested in its own build, and a change of
// their code reaches the plugin's packages through their imports.
if n.module == nil || n.module.Path != mainPath {
continue
}
if code[ip] || changedFiles(b.root, n, changed, n.testFiles, false, true) ||
slices.ContainsFunc(n.testImports, func(imp string) bool { return code[imp] }) {
out = append(out, ip)
}
}
slices.Sort(out)
return out, nil
}

// code returns the packages of the build, and those whose code changed since the checkout of the merge base at
// baseDir: one of their code files changed, a package their code imports changed code, or the module they belong to
// changed version in this build.
func (b build) code(ctx context.Context, baseDir string, changed []string) (map[string]*node, map[string]bool, error) {
nodes, err := b.list(ctx)
if err != nil {
return nil, nil, err
}
headMods, err := b.modules(ctx, b.root)
if err != nil {
return nil, err
return nil, nil, err
}
baseMods, err := b.modules(ctx, baseDir)
if err != nil {
return nil, fmt.Errorf("at the merge base: %w", err)
return nil, nil, fmt.Errorf("at the merge base: %w", err)
}
// The modules of the build whose version changed, and the main modules whose directives changed.
moved := map[string]bool{}
Expand All @@ -319,33 +352,9 @@ func (b build) affected(ctx context.Context, baseDir string, changed []string) (
}
}

// changedFiles reports whether a file of files in the package's directory, or of its testdata/ with testdata,
// changed; or, with goFiles, a Go file of the directory that no longer exists.
changedFiles := func(n *node, files []string, goFiles, testdata bool) bool {
if n.dir == "" {
return false
}
rel, err := filepath.Rel(b.root, n.dir)
if err != nil || strings.HasPrefix(rel, "..") {
return false // outside the repository
}
rel = filepath.ToSlash(rel)
for _, f := range changed {
switch {
case path.Dir(f) == rel && slices.Contains(files, path.Base(f)):
return true
case goFiles && path.Dir(f) == rel && path.Ext(f) == ".go" && !strings.HasSuffix(f, "_test.go"):
return true // a deleted file of the package's code
case testdata && (strings.HasPrefix(f, rel+"/testdata/") || path.Dir(f) == rel && strings.HasSuffix(f, "_test.go")):
return true // a file of its testdata, or a deleted test file
}
}
return false
}

code := map[string]bool{}
for ip, n := range nodes {
if n.module != nil && moved[n.module.Path] || changedFiles(n, n.codeFiles, true, false) {
if n.module != nil && moved[n.module.Path] || changedFiles(b.root, n, changed, n.codeFiles, true, false) {
code[ip] = true
}
}
Expand All @@ -358,22 +367,40 @@ func (b build) affected(ctx context.Context, baseDir string, changed []string) (
}
}
}
return nodes, code, nil
}

mainPath := moduleOf(b.dir)
var out []string
for ip, n := range nodes {
// Only the packages of the build's own module: the root module's are tested in its own build, and a change of
// their code reaches the plugin's packages through their imports.
if n.module == nil || n.module.Path != mainPath {
continue
}
if code[ip] || changedFiles(n, n.testFiles, false, true) ||
slices.ContainsFunc(n.testImports, func(imp string) bool { return code[imp] }) {
out = append(out, ip)
// changedFiles reports whether a file of files in the directory of the package, in the repository at root, or of its
// testdata/ with testdata, is among changed; or, with goFiles, a Go file of the directory that no longer exists.
func changedFiles(root string, n *node, changed, files []string, goFiles, testdata bool) bool {
rel, ok := repoDir(root, n)
if !ok {
return false
}
for _, f := range changed {
switch {
case path.Dir(f) == rel && slices.Contains(files, path.Base(f)):
return true
case goFiles && path.Dir(f) == rel && path.Ext(f) == ".go" && !strings.HasSuffix(f, "_test.go"):
return true // a deleted file of the package's code
case testdata && (strings.HasPrefix(f, rel+"/testdata/") || path.Dir(f) == rel && strings.HasSuffix(f, "_test.go")):
return true // a file of its testdata, or a deleted test file
}
}
slices.Sort(out)
return out, nil
return false
}

// repoDir returns the directory of the package relative to the repository at root, with slashes, and whether it is in
// the repository at all.
func repoDir(root string, n *node) (string, bool) {
if n.dir == "" {
return "", false
}
rel, err := filepath.Rel(root, n.dir)
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
return "", false // outside the repository
}
return filepath.ToSlash(rel), true
}

func moduleOf(dir string) string {
Expand Down
Loading