Files
coder/scripts/releaser/release.go
T
Kyle CarberryandClaude Opus 5.5 12766eca93 ci: create release tags and branches with the release App after approval (#32212)
## 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>
2026-11-15 19:54:29 -05:00

818 lines
27 KiB
Go

package main
import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"time"
"golang.org/x/xerrors"
"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/pretty"
"github.com/coder/serpent"
)
//nolint:revive // Long function is fine for a sequential release flow.
func runRelease(ctx context.Context, inv *serpent.Invocation, executor ReleaseExecutor, ghAvailable, dryRun bool) error {
w := inv.Stderr
// --- Release landscape ---
infof(w, "Checking current releases...")
allTags, err := allSemverTags()
if err != nil {
return xerrors.Errorf("listing tags: %w", err)
}
var latestMainline *version
for _, t := range allTags {
if t.Pre == "" {
latestMainline = &t
break
}
}
stableMinor := -1
latestStableStr := "(unknown)"
if latestMainline != nil {
stableMinor = latestMainline.Minor - 1
// Find highest tag in the stable minor series.
for _, t := range allTags {
if t.Major == latestMainline.Major && t.Minor == stableMinor && t.Pre == "" {
latestStableStr = t.String()
break
}
}
if latestStableStr == "(unknown)" {
latestStableStr = fmt.Sprintf("(none found for v%d.%d.x)", latestMainline.Major, stableMinor)
}
}
fmt.Fprintln(w)
mainlineStr := "(none)"
if latestMainline != nil {
mainlineStr = latestMainline.String()
}
fmt.Fprintf(w, " Latest mainline release: %s\n", pretty.Sprint(cliui.BoldFmt(), mainlineStr))
fmt.Fprintf(w, " Latest stable release: %s\n", pretty.Sprint(cliui.BoldFmt(), latestStableStr))
fmt.Fprintln(w)
// --- Branch detection ---
currentBranch, err := gitOutput("branch", "--show-current")
if err != nil {
return xerrors.Errorf("detecting branch: %w", err)
}
// Two modes:
// 1. On "main" — for tagging release candidates (RCs).
// 2. On "release/X.Y" — for releases and patches.
// RCs are tagged directly on main to avoid the toil of
// cherry-picking hundreds of commits onto a release branch.
// The release/X.Y branch is only cut when the release is
// ready.
//
// Detached HEAD is common: the release manager checks out a
// specific commit on main before running the tool. We detect
// this by checking whether HEAD is an ancestor of origin/main.
branchRe := regexp.MustCompile(`^release/(\d+)\.(\d+)$`)
onMain := currentBranch == "main"
var branchMajor, branchMinor int
// Detached HEAD: currentBranch is empty. Check if HEAD is
// reachable from origin/main.
if currentBranch == "" {
if err := gitRun("merge-base", "--is-ancestor", "HEAD", "origin/main"); err == nil {
onMain = true
currentBranch = "main"
successf(w, "Detached HEAD is an ancestor of main — RC tagging mode.")
}
}
switch {
case onMain:
successf(w, "On main branch — RC tagging mode.")
case branchRe.MatchString(currentBranch):
m := branchRe.FindStringSubmatch(currentBranch)
branchMajor, _ = strconv.Atoi(m[1])
branchMinor, _ = strconv.Atoi(m[2])
successf(w, "Using release branch: %s", currentBranch)
default:
if currentBranch == "" {
warnf(w, "Detached HEAD is not reachable from origin/main.")
} else {
warnf(w, "Current branch %q is not 'main' or a release branch (release/X.Y).", currentBranch)
}
branchInput, err := cliui.Prompt(inv, cliui.PromptOptions{
Text: "Enter the branch to use (e.g. main, release/2.21)",
Validate: func(s string) error {
if s == "main" || branchRe.MatchString(s) {
return nil
}
return xerrors.New("must be 'main' or release/X.Y (e.g. release/2.21)")
},
})
if err != nil {
return err
}
currentBranch = branchInput
if currentBranch == "main" {
onMain = true
successf(w, "On main branch — RC tagging mode.")
} else {
m := branchRe.FindStringSubmatch(currentBranch)
branchMajor, _ = strconv.Atoi(m[1])
branchMinor, _ = strconv.Atoi(m[2])
successf(w, "Using release branch: %s", currentBranch)
}
}
// --- Commit selection (RC mode) ---
// RCs are always tagged at a specific commit. Show the current
// HEAD and let the user confirm or provide a different SHA.
// We always checkout the commit so the rest of the flow
// operates in detached HEAD at the exact commit being tagged.
if onMain {
headSHA, err := gitOutput("rev-parse", "HEAD")
if err != nil {
return xerrors.Errorf("resolving HEAD: %w", err)
}
headShort := headSHA[:12]
headTitle, _ := gitOutput("log", "-1", "--format=%s", "HEAD")
fmt.Fprintf(w, " Current commit: %s %s\n", headShort, headTitle)
fmt.Fprintln(w)
commitInput, err := cliui.Prompt(inv, cliui.PromptOptions{
Text: "Commit SHA to tag (press Enter to use current)",
Default: headShort,
})
if err != nil {
return err
}
commitInput = strings.TrimSpace(commitInput)
// Resolve the input to a full SHA.
targetSHA, err := gitOutput("rev-parse", commitInput)
if err != nil {
return xerrors.Errorf("resolving %q: %w", commitInput, err)
}
// Always checkout so we're in detached HEAD at the
// target commit for the rest of the flow.
if err := gitRun("checkout", "--quiet", targetSHA); err != nil {
return xerrors.Errorf("checking out %s: %w", commitInput, err)
}
if targetSHA != headSHA {
newTitle, _ := gitOutput("log", "-1", "--format=%s", "HEAD")
successf(w, "Checked out %s %s", targetSHA[:12], newTitle)
}
fmt.Fprintln(w)
}
// --- Fetch & sync check ---
infof(w, "Fetching latest from origin...")
if err := gitRun("fetch", "--quiet", "--tags", "origin", currentBranch); err != nil {
return xerrors.Errorf("fetching: %w", err)
}
// Skip the local-vs-remote sync check in RC mode because
// we always checkout a specific commit (detached HEAD).
if !onMain {
localHead, err := gitOutput("rev-parse", "HEAD")
if err != nil {
return xerrors.Errorf("resolving HEAD: %w", err)
}
remoteHead, _ := gitOutput("rev-parse", "origin/"+currentBranch)
if remoteHead != "" && localHead != remoteHead {
warnf(w, "Your local branch is not up to date with origin/%s.", currentBranch)
fmt.Fprintf(w, " Local: %s\n", localHead[:12])
fmt.Fprintf(w, " Remote: %s\n", remoteHead[:12])
if err := confirmWithDefault(inv, "Continue anyway?", cliui.ConfirmNo); err != nil {
return err
}
fmt.Fprintln(w)
}
}
// --- Find previous version & suggest next ---
mergedTags, err := mergedSemverTags()
if err != nil {
return xerrors.Errorf("listing merged tags: %w", err)
}
var prevVersion *version
var suggested version
var changelogBaseRef string
if onMain { //nolint:nestif // Sequential release flow with two distinct modes is inherently nested.
// On main, suggest the next RC. Find the latest RC tag
// across all tags, then suggest the next one. If no RC
// tags exist, suggest rc.0 for the next minor after the
// latest mainline release.
var latestRC *version
for _, t := range allTags {
if t.IsRC() {
v := t
latestRC = &v
break
}
}
switch {
case latestRC != nil:
prevVersion = latestRC
infof(w, "Latest RC tag: %s", latestRC.String())
// Check if a final release already exists for this
// RC's minor series. If so, the series is complete
// and we should start the next minor's RC cycle.
seriesComplete := false
for _, t := range allTags {
if t.Major == latestRC.Major && t.Minor == latestRC.Minor && t.Pre == "" {
infof(w, "Final release %s already exists for this series, moving to next minor.", t.String())
seriesComplete = true
break
}
}
if seriesComplete {
suggested = version{
Major: latestRC.Major,
Minor: latestRC.Minor + 1,
Patch: 0,
Pre: "rc.0",
}
} else {
suggested = version{
Major: latestRC.Major,
Minor: latestRC.Minor,
Patch: latestRC.Patch,
Pre: fmt.Sprintf("rc.%d", latestRC.rcNumber()+1),
}
}
case latestMainline != nil:
infof(w, "No RC tags found. Latest mainline: %s", latestMainline.String())
suggested = version{
Major: latestMainline.Major,
Minor: latestMainline.Minor + 1,
Patch: 0,
Pre: "rc.0",
}
default:
infof(w, "No previous tags found.")
suggested = version{Major: 2, Minor: 0, Patch: 0, Pre: "rc.0"}
}
} else {
// On a release branch, find the latest tag matching this
// branch's major.minor. Without this filter, tags from
// newer branches reachable via merge history would be
// picked up incorrectly.
for _, t := range mergedTags {
if t.Major == branchMajor && t.Minor == branchMinor {
v := t
prevVersion = &v
break
}
}
// changelogBaseRef is the git ref used as the starting
// point for release notes. When a tag exists in this
// minor series we use it directly. For the first release
// on a new minor no matching tag exists, so we compute
// the merge-base with the previous minor's release branch
// instead. This works even when that branch has no tags
// yet. As a last resort we fall back to the latest
// reachable tag from a previous minor.
if prevVersion == nil {
prevReleaseBranch := fmt.Sprintf("release/%d.%d", branchMajor, branchMinor-1)
if err := gitRun("fetch", "--quiet", "origin", prevReleaseBranch); err != nil {
warnf(w, "Could not fetch %s: %v", prevReleaseBranch, err)
}
if mb, mbErr := gitOutput("merge-base", "HEAD", "origin/"+prevReleaseBranch); mbErr == nil && mb != "" {
changelogBaseRef = mb
infof(w, "Using merge-base with %s as changelog base: %s", prevReleaseBranch, mb[:12])
} else {
// No previous release branch; fall back to the
// latest reachable tag from a previous minor.
for _, t := range mergedTags {
if t.Major == branchMajor && t.Minor < branchMinor {
changelogBaseRef = t.String()
break
}
}
}
}
if prevVersion == nil {
infof(w, "No previous release tag found on this branch.")
suggested = version{Major: branchMajor, Minor: branchMinor, Patch: 0}
} else {
infof(w, "Previous release tag: %s", prevVersion.String())
if prevVersion.IsRC() {
// Branch has only RC tags; suggest the
// release (same base, no pre-release suffix).
suggested = version{
Major: prevVersion.Major,
Minor: prevVersion.Minor,
Patch: prevVersion.Patch,
}
} else {
suggested = version{
Major: prevVersion.Major,
Minor: prevVersion.Minor,
Patch: prevVersion.Patch + 1,
}
}
}
}
fmt.Fprintln(w)
// --- Version prompt ---
versionInput, err := cliui.Prompt(inv, cliui.PromptOptions{
Text: "Version to release",
Default: suggested.String(),
Validate: func(s string) error {
if _, ok := parseVersion(s); !ok {
return xerrors.New("must be in format vMAJOR.MINOR.PATCH or vMAJOR.MINOR.PATCH-rc.N (e.g. v2.31.1 or v2.31.0-rc.0)")
}
return nil
},
})
if err != nil {
return err
}
newVersion, _ := parseVersion(versionInput)
// Validate version against branch context.
switch {
case onMain && !newVersion.IsRC():
return xerrors.Errorf("cannot tag a non-RC version (%s) on main; switch to a release/X.Y branch", newVersion)
case !onMain && newVersion.IsRC():
return xerrors.Errorf("cannot tag an RC (%s) on a release branch; switch to main", newVersion)
case !onMain && (newVersion.Major != branchMajor || newVersion.Minor != branchMinor):
warnf(w, "Version %s does not match branch %s (expected v%d.%d.X).",
newVersion, currentBranch, branchMajor, branchMinor)
if err := confirmWithDefault(inv, "Continue anyway?", cliui.ConfirmNo); err != nil {
return err
}
fmt.Fprintln(w)
}
fmt.Fprintln(w)
infof(w, "=== Coder Release: %s ===", newVersion)
fmt.Fprintln(w)
// --- Check if tag already exists ---
tagExists := false
existingTag, _ := gitOutput("tag", "-l", newVersion.String())
if existingTag != "" {
tagExists = true
warnf(w, "Tag '%s' already exists!", newVersion)
if err := confirmWithDefault(inv, "The release workflow will reuse it if it is at this commit. Continue?", cliui.ConfirmNo); err != nil {
return err
}
fmt.Fprintln(w)
}
// --- Check open PRs ---
// This runs before breaking changes so any last-minute merges
// are caught by the subsequent checks. Skipped on main since
// there are always open PRs targeting main.
if !onMain {
infof(w, "Checking for open PRs against %s...", currentBranch)
var openPRs []ghPR
if ghAvailable {
openPRs, err = ghListOpenPRs(currentBranch)
if err != nil {
warnf(w, "Failed to check open PRs: %v", err)
}
} else {
infof(w, "Skipping (no gh CLI).")
}
if len(openPRs) > 0 {
fmt.Fprintln(w)
warnf(w, "There are open PRs targeting %s that may need merging first:", currentBranch)
fmt.Fprintln(w)
for _, pr := range openPRs {
fmt.Fprintf(w, " #%d %s (@%s)\n", pr.Number, pr.Title, pr.Author)
}
fmt.Fprintln(w)
if err := confirmWithDefault(inv, "Continue without merging these?", cliui.ConfirmNo); err != nil {
return err
}
fmt.Fprintln(w)
} else {
successf(w, "No open PRs against %s.", currentBranch)
}
fmt.Fprintln(w)
}
// --- Semver sanity checks ---
if prevVersion != nil { //nolint:nestif // Sequential release checks are inherently nested.
// Downgrade check.
if prevVersion.GreaterThan(newVersion) {
warnf(w, "Version DOWNGRADE detected: %s → %s.", prevVersion, newVersion)
if err := confirmWithDefault(inv, "Continue?", cliui.ConfirmNo); err != nil {
return err
}
fmt.Fprintln(w)
}
// Duplicate check.
if prevVersion.Equal(newVersion) {
warnf(w, "Version %s is the SAME as the previous tag %s.", newVersion, prevVersion)
if err := confirmWithDefault(inv, "Continue?", cliui.ConfirmNo); err != nil {
return err
}
fmt.Fprintln(w)
}
// Skipped patch check.
if newVersion.Major == prevVersion.Major && newVersion.Minor == prevVersion.Minor {
expectedPatch := prevVersion.Patch + 1
if newVersion.Patch > expectedPatch {
warnf(w, "Skipping patch version(s): expected v%d.%d.%d, got %s.",
newVersion.Major, newVersion.Minor, expectedPatch, newVersion)
if err := confirmWithDefault(inv, "Continue?", cliui.ConfirmNo); err != nil {
return err
}
fmt.Fprintln(w)
}
}
// Breaking changes in patch release check.
if newVersion.Major == prevVersion.Major && newVersion.Minor == prevVersion.Minor && newVersion.Patch > prevVersion.Patch {
infof(w, "Checking for breaking changes in patch release...")
// Both the "!" title marker and the release/breaking PR
// label are evaluated over prevVersion..HEAD only, so
// breaking changes shipped in earlier releases on this
// branch are not reported again.
commitRange := prevVersion.String() + "..HEAD"
commits, err := commitLog(commitRange)
if err != nil {
return xerrors.Errorf("reading commit log: %w", err)
}
prMeta := emptyPRMetadata()
if ghAvailable {
prMeta = loadPRMetadata(w, commits)
}
breakingCommits := findBreakingCommits(commits, prMeta)
if len(breakingCommits) > 0 {
fmt.Fprintln(w)
warnf(w, "BREAKING CHANGES detected in a PATCH release — this violates semver!")
fmt.Fprintln(w)
fmt.Fprintf(w, " Breaking changes since %s:\n", prevVersion)
for _, c := range breakingCommits {
fmt.Fprintf(w, " - %s %s\n", c.SHA, c.Title)
}
fmt.Fprintln(w)
if err := confirmWithDefault(inv, "Continue with patch release despite breaking changes?", cliui.ConfirmNo); err != nil {
return err
}
fmt.Fprintln(w)
} else {
successf(w, "No breaking changes detected.")
}
}
}
// --- Channel selection ---
// This is done before release notes generation because the
// notes format differs between mainline and stable channels.
// RC releases are always on the "rc" channel and skip the
// stable/mainline prompt.
channel := "mainline"
if newVersion.IsRC() {
channel = "rc"
infof(w, "Channel: rc (release candidate, will be marked as prerelease on GitHub).")
} else {
channelDefault := cliui.ConfirmNo
channelHint := ""
if newVersion.Minor == stableMinor {
channelDefault = cliui.ConfirmYes
channelHint = " (this looks like a stable release)"
}
_, err = cliui.Prompt(inv, cliui.PromptOptions{
Text: fmt.Sprintf("Mark this as the latest stable release on GitHub?%s", channelHint),
Default: channelDefault,
IsConfirm: true,
})
if err == nil {
channel = "stable"
} else if !errors.Is(err, cliui.ErrCanceled) {
return err
}
if channel == "stable" {
infof(w, "Channel: stable (will be marked as GitHub Latest).")
} else {
infof(w, "Channel: mainline (will be marked as prerelease).")
}
}
fmt.Fprintln(w)
// --- Adjust changelog base for initial releases ---
// When the new version is a .0 release (e.g. v2.33.0) and
// prevVersion is an RC (e.g. v2.33.0-rc.3), the release
// notes should show all changes since the last stable
// release in the previous minor series (e.g. v2.32.X),
// not just the delta from the last RC.
if !onMain && newVersion.Patch == 0 && !newVersion.IsRC() && prevVersion != nil && prevVersion.IsRC() {
var lastStable *version
for _, t := range allTags {
if t.Pre == "" && t.Major == newVersion.Major && t.Minor < newVersion.Minor {
lastStable = &t
break
}
}
if lastStable != nil {
infof(w, "Changelog base: %s (last stable release before %s series).", lastStable, newVersion)
prevVersion = lastStable
} else {
warnf(w, "No previous stable release found; changelog will diff from RC %s.", prevVersion)
}
}
// --- Generate release notes ---
infof(w, "Generating release notes...")
var commitRange string
switch {
case prevVersion != nil:
commitRange = prevVersion.String() + "..HEAD"
case changelogBaseRef != "":
commitRange = changelogBaseRef + "..HEAD"
default:
commitRange = "HEAD"
}
commits, err := commitLog(commitRange)
if err != nil {
return xerrors.Errorf("reading commit log: %w", err)
}
prMeta := emptyPRMetadata()
if ghAvailable {
prMeta = loadPRMetadata(w, commits)
}
type section struct {
Key string
Title string
}
sections := []section{
{"breaking", "BREAKING CHANGES"},
{"security", "SECURITY"},
{"feat", "Features"},
{"fix", "Bug fixes"},
{"docs", "Documentation"},
{"refactor", "Code refactoring"},
{"perf", "Performance improvements"},
{"test", "Tests"},
{"build", "Builds"},
{"ci", "Continuous integration"},
{"chore", "Chores"},
{"revert", "Reverts"},
{"other", "Other changes"},
{"experimental", "Experimental changes"},
}
sectionCommits := make(map[string][]string)
for _, c := range commits {
meta := prMeta.lookupCommit(c.FullSHA, c.PRCount)
// Skip dependabot commits.
if meta.Author == "dependabot" || meta.Author == "app/dependabot" {
continue
}
cat := categorizeCommit(c.Title, meta.Labels)
humanTitle := humanizeTitle(c.Title)
// Strip trailing PR ref from humanized title if present,
// so we can rebuild it with the SHA appended.
humanTitle = prNumRe.ReplaceAllString(humanTitle, "")
humanTitle = strings.TrimSpace(humanTitle)
// Build entry: - Title (#PR, SHA) (@author)
var entry string
if c.PRCount > 0 {
entry = fmt.Sprintf("- %s (#%d, %s)", humanTitle, c.PRCount, c.SHA)
} else {
entry = fmt.Sprintf("- %s (%s)", humanTitle, c.SHA)
}
if meta.Author != "" {
entry += fmt.Sprintf(" (@%s)", meta.Author)
}
sectionCommits[cat] = append(sectionCommits[cat], entry)
}
// Build release notes markdown matching the format from
// scripts/release/generate_release_notes.sh.
var notes strings.Builder
// Stable since header, mainline blurb, or RC advisory.
if channel == "stable" {
fmt.Fprintf(&notes, "> ## Stable (since %s)\n\n", time.Now().Format("January 02, 2006"))
}
fmt.Fprintln(&notes, "## Changelog")
switch channel {
case "rc":
fmt.Fprintln(&notes)
fmt.Fprintln(&notes, "> [!NOTE]")
fmt.Fprintln(&notes, "> This is a **release candidate** (RC) for testing purposes. It is not recommended for production use. Please report any issues you encounter. Learn more about our [Release Schedule](https://coder.com/docs/reference/releases).")
case "mainline":
// Only show the mainline blurb when the version is
// actually the current mainline series. Patches on
// older branches (e.g. ESR) are neither mainline nor
// stable, so we omit the note entirely.
if latestMainline != nil && newVersion.Minor == latestMainline.Minor {
fmt.Fprintln(&notes)
fmt.Fprintln(&notes, "> [!NOTE]")
fmt.Fprintln(&notes, "> This is a mainline Coder release. We advise enterprise customers without a staging environment to install our [latest stable release](https://github.com/coder/coder/releases/latest) while we refine this version. Learn more about our [Release Schedule](https://coder.com/docs/reference/releases).")
}
}
hasContent := false
for _, s := range sections {
if entries, ok := sectionCommits[s.Key]; ok && len(entries) > 0 {
fmt.Fprintf(&notes, "\n### %s\n\n", s.Title)
if s.Key == "experimental" {
fmt.Fprintln(&notes, "These changes are feature-flagged and can be enabled with the `--experiments` server flag. They may change or be removed in future releases.")
fmt.Fprintln(&notes)
}
for _, e := range entries {
fmt.Fprintln(&notes, e)
}
hasContent = true
}
}
if !hasContent {
prevStr := "the beginning of time"
if prevVersion != nil {
prevStr = prevVersion.String()
}
fmt.Fprintf(&notes, "\n_No changes since %s._\n", prevStr)
}
// Compare link.
compareBase := changelogBaseRef
if prevVersion != nil {
compareBase = prevVersion.String()
}
if compareBase != "" {
fmt.Fprintf(&notes, "\nCompare: [`%s...%s`](https://github.com/%s/%s/compare/%s...%s)\n",
compareBase, newVersion, owner, repo, compareBase, newVersion)
}
// Container image.
// The release workflow tags the image via scripts/image_tag.sh, which
// always prefixes the version with "v" (ghcr.io/coder/coder:v2.37.5).
imageTag := fmt.Sprintf("ghcr.io/coder/coder:%s", newVersion.String())
fmt.Fprintf(&notes, "\n## Container image\n\n- `docker pull %s`\n", imageTag)
// Install/upgrade links.
fmt.Fprintln(&notes, "\n## Install/upgrade")
fmt.Fprintln(&notes, "\nRefer to our docs to [install](https://coder.com/docs/install) or [upgrade](https://coder.com/docs/install/operate/upgrade) Coder, or use a release asset below.")
releaseNotes := notes.String()
// Write to file.
releaseNotesFile := fmt.Sprintf("build/RELEASE-%s.md", newVersion)
if err := os.MkdirAll("build", 0o755); err != nil {
return xerrors.Errorf("creating build directory: %w", err)
}
if err := os.WriteFile(releaseNotesFile, []byte(releaseNotes), 0o600); err != nil {
return xerrors.Errorf("writing release notes: %w", err)
}
// --- Preview ---
fmt.Fprintln(w)
fmt.Fprintln(w, pretty.Sprint(cliui.BoldFmt(), "--- Release Notes Preview ---"))
fmt.Fprintln(w)
fmt.Fprint(w, releaseNotes)
fmt.Fprintln(w, pretty.Sprint(cliui.BoldFmt(), "--- End Preview ---"))
fmt.Fprintln(w)
infof(w, "Release notes written to %s", releaseNotesFile)
fmt.Fprintln(w)
// --- Offer to edit ---
editor := os.Getenv("EDITOR")
if editor == "" {
editor = os.Getenv("GIT_EDITOR")
}
if editor != "" {
if err := confirmWithDefault(inv, fmt.Sprintf("Edit release notes in %s?", editor), cliui.ConfirmNo); err == nil {
cmd := exec.Command(editor, releaseNotesFile)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return xerrors.Errorf("editor: %w", err)
}
updated, err := os.ReadFile(releaseNotesFile)
if err != nil {
return xerrors.Errorf("reading edited release notes: %w", err)
}
// The file will be re-read from disk before the
// workflow trigger step.
_ = string(updated)
infof(w, "Release notes updated.")
}
fmt.Fprintln(w)
}
// --- Commit to release ---
// Only the release GitHub App may create tags. The release
// workflow creates the tag at this commit after its release
// environment is approved by a second release manager, and it
// refuses a commit that isn't already on the branch it runs on.
ref, err := gitOutput("rev-parse", "HEAD")
if err != nil {
return xerrors.Errorf("resolving HEAD: %w", err)
}
if err := gitRun("merge-base", "--is-ancestor", ref, "origin/"+currentBranch); err != nil {
return xerrors.Errorf("commit %s is not on origin/%s; push it (through a PR) before releasing", ref[:12], currentBranch)
}
if tagExists {
tagged, err := gitOutput("rev-list", "-n", "1", newVersion.String())
if err != nil {
return xerrors.Errorf("resolving tag %s: %w", newVersion, err)
}
if tagged != ref {
return xerrors.Errorf("tag %s already exists at %s, not at HEAD (%s)", newVersion, tagged[:12], ref[:12])
}
infof(w, "Tag %s already exists at this commit; the workflow will reuse it.", newVersion)
fmt.Fprintln(w)
}
// --- Trigger release workflow ---
// Re-read release notes from disk in case the user edited the
// file externally between the editor step and now.
freshNotes, err := os.ReadFile(releaseNotesFile)
if err != nil {
return xerrors.Errorf("re-reading release notes: %w", err)
}
releaseNotes = string(freshNotes)
fmt.Fprintln(w, pretty.Sprint(cliui.BoldFmt(), "Next step: trigger the 'release.yaml' GitHub Actions workflow."))
fmt.Fprintf(w, " Workflow: release.yaml\n")
fmt.Fprintf(w, " Repo: %s/%s\n", owner, repo)
fmt.Fprintf(w, " Ref: %s\n", currentBranch)
fmt.Fprintln(w)
fmt.Fprintln(w, pretty.Sprint(cliui.BoldFmt(), " Payload fields:"))
fmt.Fprintf(w, " version: %s\n", newVersion)
fmt.Fprintf(w, " commit: %s\n", ref)
fmt.Fprintf(w, " release_channel: %s\n", channel)
fmt.Fprintf(w, " dry_run: false\n")
fmt.Fprintln(w)
fmt.Fprintln(w, pretty.Sprint(cliui.BoldFmt(), " release_notes:"))
for _, line := range strings.Split(releaseNotes, "\n") {
fmt.Fprintf(w, " %s\n", line)
}
fmt.Fprintln(w)
if err := confirm(inv, "Trigger release workflow?"); err != nil {
infof(w, "Skipped workflow trigger. You can trigger it manually from GitHub Actions.")
fmt.Fprintln(w)
successf(w, "Done! 🎉")
return nil
}
dispatchedAt := time.Now()
if err := executor.TriggerWorkflow(ctx, currentBranch, newVersion.String(), ref, channel, releaseNotes); err != nil {
return xerrors.Errorf("triggering workflow: %w", err)
}
successf(w, "Release workflow triggered!")
fmt.Fprintln(w)
fmt.Fprintln(w, pretty.Sprint(cliui.BoldFmt(), "Next step: another release manager approves the release."))
fmt.Fprintf(w, " The run waits for its %q environment to be approved by a release manager\n", "release")
fmt.Fprintln(w, " other than you (GitHub notifies them). After approval it creates tag "+newVersion.String()+",")
fmt.Fprintln(w, " then builds and publishes; release-publish needs one more approval later.")
if !dryRun {
if url := ghFindDispatchedRun(currentBranch, dispatchedAt); url != "" {
fmt.Fprintf(w, " Run: %s\n", url)
} else {
fmt.Fprintf(w, " Run: https://github.com/%s/%s/actions/workflows/release.yaml?query=branch%%3A%s\n", owner, repo, currentBranch)
}
}
// --- Update release docs ---
// RC releases skip docs updates (calendar, helm versions, etc.)
// since they are not production releases.
if newVersion.IsRC() {
infof(w, "Skipping docs update for release candidate.")
} else {
promptAndUpdateDocs(inv, newVersion, channel, dryRun)
}
fmt.Fprintln(w)
successf(w, "Done! 🎉")
return nil
}