## Summary **Unblocks releases after the `coder/coder` lockdown.** Only the `coder-release-publisher` App can now create tags and release branches (rulesets "Release tags (App only)" and "Release branch creation", managed in `coder/github-org-terraform`). Its key is only available in environments that need a **second release manager's approval** (`release-managers`, no self-review, no admin bypass). People can't push release tags anymore, so the tag is now created **inside the approved release run**. ### Releasing (`./scripts/release.sh` works as before, minus one step) 1. Run `./scripts/release.sh` on `release/X.Y` (or `main` for an RC), as today. It still picks the version, checks open PRs and breaking changes, and writes the release notes. 2. Instead of creating and pushing the tag, it checks the commit is already on `origin/<branch>` and dispatches `release.yaml` **on the branch** with `version`, `commit`, `release_channel` and `release_notes`. 3. The releaser prints the run's URL. GitHub also notifies the `release-managers` reviewers. **Another release manager approves** the run's `release` environment (Review deployments). 4. The approved job creates the annotated tag `version` at `commit` with the App, then builds, signs and publishes that commit as before. `release.yaml` can also be run by hand from the Actions UI with the same inputs. ### `release.yaml` - New `version` and `commit` inputs (required unless `dry_run`). - The `release` job checks out `commit`. Before anything else, it: - refuses a malformed version; - refuses a version that doesn't match the branch (`release/X.Y`, or `main`/`release/X.Y` for `-rc.N`); - refuses a commit that isn't already on the dispatched branch; - refuses an existing tag that points elsewhere; - otherwise creates the annotated tag with the App. A re-run reuses a tag already at that commit. `version.sh` and the rest of the job are unchanged. - **Homebrew and the `coder/packages` dispatch use the new `coder-package-publisher` App** (`release-publish` environment) instead of `CDRCI_GITHUB_TOKEN`. The dispatch moves to its own `publish-packages` job in `release-publish`, where that key lives; it's approved together with Homebrew and winget. Winget still uses the `cdrci` machine user, since an App can't open PRs on `microsoft/winget-pkgs`. - Every App token is scoped to one repository and the permissions it needs (`zizmor`'s `github-app` audit: 5 high findings to 0, one of which predates this PR). ### `create-release-branch.yaml` (new) Dispatched on `main` with `version` (e.g. `2.40`) and `commit`. After approval in the `release-refs` environment, it creates `release/X.Y` at that commit, refusing one that isn't on `main` or a branch that already exists. ### `scripts/releaser` - `ReleaseExecutor` drops `CreateTag`/`PushTag`. - `TriggerWorkflow` dispatches on the branch with `version` and `commit`. - An existing tag must already point at HEAD. - After dispatching, it looks up the run (`gh run list`, polling briefly) and prints its URL and the approval steps. ## Before merging - [ ] `RELEASE_APP_ID` and `RELEASE_APP_PRIVATE_KEY` set in the **`release-refs`** environment (currently empty), and the rotated release App key in `release`. - [x] `CODER_PACKAGE_PUBLISHER_APP_ID` / `CODER_PACKAGE_PUBLISHER_APP_PRIVATE_KEY` in `release-publish`; App installed on `homebrew-coder` and `packages`. ## After merging: backport to every branch you release from `release.yaml` runs from the dispatched branch's own copy, and `./scripts/release.sh` runs the branch's releaser. So **each `release/X.Y` needs this change before it can release**. Active lines by recent tags: `release/2.39`, `2.38`, `2.37`, `2.36`, `2.35`, `2.34`, `2.29`. ## Relation to #31321 #31321 (`Tag release`) runs the releaser non-interactively in CI. This PR is the smaller change that unblocks releases now. #31321 can build on it: its tagging job would need `environment: release-refs` (where the App key lives) plus the extra App permissions it asks for, which are a manifest change in `coder/github-org-terraform` `apps/`. ## Testing - `go build`, `go vet`, `go test ./scripts/releaser` pass. - `actionlint` and `zizmor` are clean on both workflows. - Not exercised end to end. Suggest the first use be an RC (`v2.40.0-rc.N` from `main`), watching the tag step. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
218 lines
5.6 KiB
Go
218 lines
5.6 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
"os/exec"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// ghOutput runs a gh CLI command and returns trimmed stdout.
|
|
func ghOutput(args ...string) (string, error) {
|
|
cmd := exec.Command("gh", args...)
|
|
out, err := cmd.Output()
|
|
if err != nil {
|
|
var exitErr *exec.ExitError
|
|
if errors.As(err, &exitErr) {
|
|
return "", exitErr
|
|
}
|
|
return "", err
|
|
}
|
|
return strings.TrimSpace(string(out)), nil
|
|
}
|
|
|
|
// checkGHAuth verifies that the gh CLI is installed and
|
|
// authenticated. Returns true if gh is available.
|
|
func checkGHAuth() bool {
|
|
cmd := exec.Command("gh", "auth", "status")
|
|
cmd.Stdout = nil
|
|
cmd.Stderr = nil
|
|
return cmd.Run() == nil
|
|
}
|
|
|
|
// ghPR is a minimal pull request representation parsed from gh CLI
|
|
// JSON output.
|
|
type ghPR struct {
|
|
Number int `json:"number"`
|
|
Title string `json:"title"`
|
|
Author string `json:"author"`
|
|
Labels []string
|
|
}
|
|
|
|
// ghListOpenPRs returns open PRs targeting the given branch via
|
|
// the gh CLI.
|
|
func ghListOpenPRs(branch string) ([]ghPR, error) {
|
|
out, err := ghOutput("pr", "list",
|
|
"--repo", owner+"/"+repo,
|
|
"--base", branch,
|
|
"--state", "open",
|
|
"--json", "number,title,author",
|
|
"--jq", `.[] | "\(.number)\t\(.title)\t\(.author.login)"`,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if out == "" {
|
|
return nil, nil
|
|
}
|
|
var prs []ghPR
|
|
for _, line := range strings.Split(out, "\n") {
|
|
parts := strings.SplitN(line, "\t", 3)
|
|
if len(parts) < 3 {
|
|
continue
|
|
}
|
|
num, _ := strconv.Atoi(parts[0])
|
|
prs = append(prs, ghPR{
|
|
Number: num,
|
|
Title: parts[1],
|
|
Author: parts[2],
|
|
})
|
|
}
|
|
return prs, nil
|
|
}
|
|
|
|
// prMetadata holds labels and author for a merged PR.
|
|
type prMetadata struct {
|
|
Labels []string
|
|
Author string
|
|
}
|
|
|
|
// prMetadataMaps holds PR metadata indexed by both merge-commit SHA
|
|
// and PR number. On release branches, commits are cherry-picked so
|
|
// their SHA differs from the original merge commit on main. The PR
|
|
// number (preserved in the commit title) provides a fallback lookup.
|
|
type prMetadataMaps struct {
|
|
bySHA map[string]prMetadata
|
|
byNumber map[int]prMetadata
|
|
}
|
|
|
|
// lookupCommit returns PR metadata for a commit, trying the full SHA
|
|
// first and falling back to PR number for cherry-picked commits.
|
|
func (m *prMetadataMaps) lookupCommit(fullSHA string, prNumber int) prMetadata {
|
|
if meta, ok := m.bySHA[fullSHA]; ok {
|
|
return meta
|
|
}
|
|
if prNumber > 0 {
|
|
return m.byNumber[prNumber]
|
|
}
|
|
return prMetadata{}
|
|
}
|
|
|
|
// loadPRMetadata fetches PR metadata for the given commits via the gh
|
|
// CLI. It never fails: when the query errors, a warning is written and
|
|
// empty maps are returned so callers degrade to title-only
|
|
// categorization.
|
|
func loadPRMetadata(w io.Writer, commits []commitEntry) *prMetadataMaps {
|
|
prMeta, err := ghBuildPRMetadataMap(commits)
|
|
if err != nil {
|
|
warnf(w, "Failed to fetch PR metadata: %v", err)
|
|
} else if prMeta != nil {
|
|
return prMeta
|
|
}
|
|
return emptyPRMetadata()
|
|
}
|
|
|
|
func emptyPRMetadata() *prMetadataMaps {
|
|
return &prMetadataMaps{
|
|
bySHA: make(map[string]prMetadata),
|
|
byNumber: make(map[int]prMetadata),
|
|
}
|
|
}
|
|
|
|
// ghBuildPRMetadataMap returns PR metadata indexed by both
|
|
// merge-commit SHA and PR number for merged PRs targeting main.
|
|
// This matches the bash script's approach of querying --base main
|
|
// with a date filter based on the oldest commit in the range.
|
|
func ghBuildPRMetadataMap(commits []commitEntry) (*prMetadataMaps, error) {
|
|
empty := &prMetadataMaps{
|
|
bySHA: make(map[string]prMetadata),
|
|
byNumber: make(map[int]prMetadata),
|
|
}
|
|
if len(commits) == 0 {
|
|
return empty, nil
|
|
}
|
|
// Find the earliest commit timestamp to scope the PR query.
|
|
earliest := commits[0].Timestamp
|
|
for _, c := range commits[1:] {
|
|
if c.Timestamp < earliest {
|
|
earliest = c.Timestamp
|
|
}
|
|
}
|
|
lookbackDate := time.Unix(earliest, 0).Format("2006-01-02")
|
|
|
|
out, err := ghOutput("pr", "list",
|
|
"--repo", owner+"/"+repo,
|
|
"--base", "main",
|
|
"--state", "merged",
|
|
"--limit", "10000",
|
|
"--search", "merged:>="+lookbackDate,
|
|
"--json", "number,mergeCommit,labels,author",
|
|
"--jq", `.[] | "\(.number)\t\(.mergeCommit.oid)\t\(.author.login)\t\([.labels[].name] | join(","))"`,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if out == "" {
|
|
return empty, nil
|
|
}
|
|
result := &prMetadataMaps{
|
|
bySHA: make(map[string]prMetadata),
|
|
byNumber: make(map[int]prMetadata),
|
|
}
|
|
for _, line := range strings.Split(out, "\n") {
|
|
parts := strings.SplitN(line, "\t", 4)
|
|
if len(parts) < 4 {
|
|
continue
|
|
}
|
|
num, _ := strconv.Atoi(parts[0])
|
|
sha := parts[1]
|
|
author := parts[2]
|
|
var labels []string
|
|
if parts[3] != "" {
|
|
labels = strings.Split(parts[3], ",")
|
|
slices.Sort(labels)
|
|
}
|
|
meta := prMetadata{
|
|
Labels: labels,
|
|
Author: author,
|
|
}
|
|
result.bySHA[sha] = meta
|
|
if num > 0 {
|
|
result.byNumber[num] = meta
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// ghFindDispatchedRun returns the URL of the release.yaml run that
|
|
// the current gh user dispatched on branch at or after since. A
|
|
// workflow dispatch doesn't return the run it creates, so this polls
|
|
// for a short while; it returns "" if the run doesn't show up.
|
|
func ghFindDispatchedRun(branch string, since time.Time) string {
|
|
user, err := ghOutput("api", "user", "--jq", ".login")
|
|
if err != nil || user == "" {
|
|
return ""
|
|
}
|
|
query := `[.[] | select(.createdAt >= "` + since.UTC().Add(-time.Minute).Format(time.RFC3339) + `")] | first | .url // ""`
|
|
for range 10 {
|
|
url, err := ghOutput("run", "list",
|
|
"--repo", owner+"/"+repo,
|
|
"--workflow", "release.yaml",
|
|
"--branch", branch,
|
|
"--event", "workflow_dispatch",
|
|
"--user", user,
|
|
"--limit", "5",
|
|
"--json", "url,createdAt",
|
|
"--jq", query,
|
|
)
|
|
if err == nil && url != "" {
|
|
return url
|
|
}
|
|
time.Sleep(3 * time.Second)
|
|
}
|
|
return ""
|
|
}
|