Skip to content
Closed
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
19 changes: 19 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,30 @@ jobs:
check-perms:
runs-on: ${{ github.repository_owner == 'coder' && 'depot-ubuntu-22.04-8' || 'ubuntu-latest' }}
steps:
# tag-release.yaml dispatches this workflow with the release App token,
# so the actor is the App's bot. Mint a read-only token to learn the
# App's slug and recognize it.
- name: Generate release App token
id: release_app_token
uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0
with:
app-id: ${{ secrets.RELEASE_APP_ID }}
private-key: ${{ secrets.RELEASE_APP_PRIVATE_KEY }}
permission-metadata: read

- name: Allow only maintainers/admins
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
RELEASE_APP_SLUG: ${{ steps.release_app_token.outputs.app-slug }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const releaseAppSlug = process.env.RELEASE_APP_SLUG;
if (releaseAppSlug && context.actor === `${releaseAppSlug}[bot]`) {
core.info(`Actor ${context.actor} is the release App; tag-release.yaml checked the human actor.`);
return;
}

const {data} = await github.rest.repos.getCollaboratorPermissionLevel({
owner: context.repo.owner,
repo: context.repo.repo,
Expand Down
160 changes: 160 additions & 0 deletions .github/workflows/tag-release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
# Runs the release wizard (scripts/releaser) unattended on the release/X.Y
# branch selected in "Use workflow from". It accepts the default answer to
# every prompt, tags the release, pushes the tag, triggers release.yaml, and
# opens the release docs PR.
#
# Release candidates are tagged on main, which this workflow does not run on.
# Use ./scripts/release.sh locally for those.
#
# Safety checks that default to "no" (open PRs, downgrades, breaking changes
# in a patch release, and so on) fail the run. To release anyway with open
# PRs against the release branch or breaking changes in a patch release,
# re-run with allow_open_prs or allow_breaking_changes enabled. For any other
# check, run ./scripts/release.sh locally.
name: Tag release

on:
workflow_dispatch:
inputs:
dry_run:
description: Print the tag, push, and workflow trigger instead of running them.
type: boolean
required: true
default: true
allow_open_prs:
description: Continue when open PRs target the release branch.
type: boolean
required: true
default: false
allow_breaking_changes:
description: Continue when a patch release contains breaking changes.
type: boolean
required: true
default: false

permissions: {}

concurrency:
group: ${{ github.workflow }}
cancel-in-progress: false

jobs:
# Only allow maintainers/admins to release.
check-perms:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- name: Allow only maintainers/admins
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const {data} = await github.rest.repos.getCollaboratorPermissionLevel({
owner: context.repo.owner,
repo: context.repo.repo,
username: context.actor
});
const role = data.role_name || data.user?.role_name || data.permission;
const perms = data.user?.permissions || {};
core.info(`Actor ${context.actor} permission=${data.permission}, role_name=${role}`);

const allowed =
role === 'admin' ||
role === 'maintain' ||
perms.admin === true ||
perms.maintain === true;

if (!allowed) core.setFailed('Denied: requires maintain or admin');

tag-release:
name: Tag and trigger release
needs: [check-perms]
runs-on: ubuntu-latest
# The release App token performs every write, so the job token stays
# read-only.
permissions:
contents: read
steps:
- name: Harden Runner
uses: step-security/harden-runner@dc2769abcc0fcfbadfc1df54081c1608579ef6b2 # v2.22.2
with:
egress-policy: audit

- name: Require a release branch
env:
REF: ${{ github.ref }}
run: |
set -euo pipefail
if [[ ! "$REF" =~ ^refs/heads/release/[0-9]+\.[0-9]+$ ]]; then
echo "::error::Select a release/X.Y branch in \"Use workflow from\", got: $REF"
exit 1
fi
Comment on lines +84 to +92

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@f0ssel what do you think about taking the release branch as a workflow_dispatch input instead, so the workflow always runs from main? That way, we don't have to keep all branches in sync (which has bitten us before), at least when it comes to the tooling responsible for the release process.
We could use a worktree to interact with the release branch, or we could simply tag origin/release/X.Y directly after a full fetch. Thoughts?


- name: Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
# The releaser needs every tag to pick the next version and the full
# history to generate release notes.
fetch-depth: 0
persist-credentials: false

- name: Set up mise tools
uses: ./.github/actions/setup-mise
with:
install-args: "go"

# The release App bypasses the tag create protection ruleset, which
# GITHUB_TOKEN cannot. It needs contents: write to push the tag and the
# docs branch, actions: write to dispatch release.yaml, and
# pull-requests: write to open the docs PR.
- name: Generate release App token
id: release_app_token
uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0
with:
app-id: ${{ secrets.RELEASE_APP_ID }}
private-key: ${{ secrets.RELEASE_APP_PRIVATE_KEY }}

- name: Run releaser
env:
GH_TOKEN: ${{ steps.release_app_token.outputs.token }}
APP_SLUG: ${{ steps.release_app_token.outputs.app-slug }}
DRY_RUN: ${{ inputs.dry_run }}
ALLOW_OPEN_PRS: ${{ inputs.allow_open_prs }}
ALLOW_BREAKING_CHANGES: ${{ inputs.allow_breaking_changes }}
# A bot pushes the tag, so record the human who started this run in
# the tag message.
REQUESTED_BY: ${{ github.actor }}
run: |
set -euo pipefail

git remote set-url origin "https://x-access-token:${GH_TOKEN}@github.com/${GITHUB_REPOSITORY}.git"
bot_login="${APP_SLUG}[bot]"
bot_id="$(gh api "/users/${bot_login}" --jq .id)"
git config user.name "$bot_login"
git config user.email "${bot_id}+${bot_login}@users.noreply.github.com"

args=(
--non-interactive
--requested-by "$REQUESTED_BY"
--requested-by-run-url "${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
)
if [[ "$DRY_RUN" == "true" ]]; then
args+=(--dry-run)
fi
if [[ "$ALLOW_OPEN_PRS" == "true" ]]; then
args+=(--allow-open-prs)
fi
if [[ "$ALLOW_BREAKING_CHANGES" == "true" ]]; then
args+=(--allow-breaking-changes)
fi
go run ./scripts/releaser "${args[@]}"
Comment thread
f0ssel marked this conversation as resolved.
Resolved

- name: Upload release notes
if: ${{ !cancelled() }}
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: release-notes
path: build/RELEASE-*.md
if-no-files-found: ignore
retention-days: 7
14 changes: 8 additions & 6 deletions scripts/releaser/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,27 +493,29 @@ func updateReleaseDocs(
}

// promptAndUpdateDocs asks the user if they want to create a
// docs update PR and does so if confirmed.
// docs update PR and does so if confirmed. It returns the error from
// creating the PR; declining the prompt is not an error.
func promptAndUpdateDocs(
inv *serpent.Invocation,
p *prompter,
newVer version,
channel string,
dryRun bool,
) {
) error {
w := inv.Stderr
_, _ = fmt.Fprintln(w)
_, _ = fmt.Fprintln(w, pretty.Sprint(cliui.BoldFmt(),
"Next step: create a PR updating release docs "+
"(calendar, helm versions, rancher)."))
_, _ = fmt.Fprintln(w)

if err := confirmWithDefault(inv, "Create docs update PR?", cliui.ConfirmYes); err != nil {
if err := p.confirmWithDefault("Create docs update PR?", cliui.ConfirmYes); err != nil {
infof(w, "Skipped docs update. You can update them manually.")
return
return nil
}

if err := updateReleaseDocs(inv, newVer, channel, dryRun); err != nil {
warnf(w, "Failed to create docs PR: %v", err)
warnf(w, "You'll need to update release docs manually.")
return xerrors.Errorf("creating docs PR: %w", err)
}
return nil
}
46 changes: 40 additions & 6 deletions scripts/releaser/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func main() {
var dryRun bool
var opts releaseOptions

cmd := &serpent.Command{
Use: "releaser",
Expand All @@ -24,25 +24,59 @@ func main() {
Name: "dry-run",
Flag: "dry-run",
Description: "Print mutating commands instead of executing them.",
Value: serpent.BoolOf(&dryRun),
Value: serpent.BoolOf(&opts.DryRun),
},
{
Name: "non-interactive",
Flag: "non-interactive",
Description: "Answer every prompt with its default, as if pressing Enter. " +
"Safety checks that default to no fail the release instead.",
Value: serpent.BoolOf(&opts.NonInteractive),
},
{
Name: "allow-open-prs",
Flag: "allow-open-prs",
Description: "Continue when open PRs target the release branch.",
Value: serpent.BoolOf(&opts.AllowOpenPRs),
},
{
Name: "allow-breaking-changes",
Flag: "allow-breaking-changes",
Description: "Continue when a patch release contains breaking changes.",
Value: serpent.BoolOf(&opts.AllowBreakingChanges),
},
{
Name: "requested-by",
Flag: "requested-by",
Description: "GitHub login of the user the release runs on behalf of. Recorded in the tag message.",
Value: serpent.StringOf(&opts.RequestedBy),
},
{
Name: "requested-by-run-url",
Flag: "requested-by-run-url",
Description: "URL of the workflow run that requested the release.",
Value: serpent.StringOf(&opts.RequestedByRunURL),
},
},
Handler: func(inv *serpent.Invocation) error {
return Run(inv, dryRun)
return Run(inv, opts)
},
}

err := cmd.Invoke().WithOS().Run()
if err != nil {
if errors.Is(err, cliui.ErrCanceled) {
os.Exit(1)
}
// Unwrap serpent's "running command ..." wrapper to keep output
// clean.
var runErr *serpent.RunCommandError
if errors.As(err, &runErr) {
err = runErr.Err
}
// An interactive cancel is the operator's own choice, so there
// is nothing to report. In non-interactive mode the error says
// which check stopped the release.
if errors.Is(err, cliui.ErrCanceled) && !opts.NonInteractive {
os.Exit(1)
}
pretty.Fprintf(os.Stderr, cliui.DefaultStyles.Error, "Error: %s\n", err)
os.Exit(1)
}
Expand Down
Loading
Loading