Skip to content
Open
128 changes: 128 additions & 0 deletions .github/workflows/cut-release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# Creates a release tag and publishes a GitHub release. Tag creation is restricted by a ruleset, so tags are
# only made here (as the bee-releaser app) after a second person approves the
# `release` environment. The commit is resolved before approval, so the
# reviewer approves an exact SHA (shown in the run summary).
# The published release then triggers publish_docker.yaml.
name: Cut release
run-name: Cut ${{ inputs.tag }} (by ${{ github.actor }})

on:
workflow_dispatch:
inputs:
tag:
description: "Tag to create, e.g. 1.1.12"
required: true
ref:
description: "Branch or full commit SHA to tag (must be on the default branch or a release/* branch)"
required: true
default: main

permissions: {}

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

env:
# Allowed tag format for this repo.
TAG_PATTERN: '^[0-9]+\.[0-9]+\.[0-9]+$'

jobs:
resolve:
runs-on: ubuntu-latest
permissions:
contents: read
outputs:
sha: ${{ steps.resolve.outputs.sha }}
steps:
- name: Validate tag and resolve commit
id: resolve
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
TAG: ${{ inputs.tag }}
REF: ${{ inputs.ref }}
with:
script: |
const { TAG, REF, TAG_PATTERN } = process.env
const { owner, repo } = context.repo
if (!new RegExp(TAG_PATTERN).test(TAG)) {
return core.setFailed(`tag must match ${TAG_PATTERN}, got '${TAG}'`)
}
try {
await github.rest.git.getRef({ owner, repo, ref: `tags/${TAG}` })
return core.setFailed(`tag ${TAG} already exists`)
} catch (e) {
if (e.status !== 404) throw e
}
const { data: c } = await github.rest.repos.getCommit({ owner, repo, ref: REF })
if (!/^[0-9a-f]{40}$/.test(c.sha ?? '')) {
return core.setFailed('ref did not resolve to a commit')
}
// getCommit also resolves commits that are on no branch, e.g. fork PR heads
// (refs/pull/*). Only tag commits on the default branch or a release/* branch.
const def = context.payload.repository.default_branch
const branches = (await github.paginate(github.rest.repos.listBranches, { owner, repo, per_page: 100 }))
.map(b => b.name)
.filter(n => n === def || n.startsWith('release/'))
.sort((a, b) => (a === def ? -1 : b === def ? 1 : a.localeCompare(b)))
let onBranch = ''
for (const b of branches) {
const { data: cmp } = await github.rest.repos.compareCommitsWithBasehead({ owner, repo, basehead: `${b}...${c.sha}`, per_page: 1 })
if (cmp.status === 'identical' || cmp.status === 'behind') { onBranch = b; break }
}
if (!onBranch) {
return core.setFailed(`${c.sha} is not on ${def} or a release/* branch, refusing to tag it`)
}
core.setOutput('sha', c.sha)
// Only the SHA, a link built from it and fixed text: nothing an outsider controls is rendered.
// <code> stops GitHub shortening the SHA into a 7-char commit reference.
const url = `https://github.com/${owner}/${repo}/commit/${c.sha}`
core.summary
.addHeading(`${TAG} will tag <code>${c.sha}</code>`)
.addRaw(`<p>${onBranch === def ? 'On the default branch.' : 'NOT on the default branch (on a release branch).'}</p>`, true)
.addLink(url, url)
await core.summary.write()

tag:
needs: resolve
runs-on: ubuntu-latest
environment: release
steps:
- name: Generate GitHub App token
id: app-token
uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0
with:
app-id: ${{ vars.RELEASE_APP_ID }}
private-key: ${{ secrets.RELEASE_APP_KEY }}
# Least privilege: only creates the tag and the release.
permission-contents: write

- name: Create tag and release
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
TAG: ${{ inputs.tag }}
SHA: ${{ needs.resolve.outputs.sha }}
with:
github-token: ${{ steps.app-token.outputs.token }}
script: |
const { TAG, SHA } = process.env
const { owner, repo } = context.repo
// Safe to re-run this job if createRelease failed after the tag was created:
// an existing tag is accepted only at the approved SHA, and the release is created only if missing.
try {
await github.rest.git.createRef({ owner, repo, ref: `refs/tags/${TAG}`, sha: SHA })
} catch (e) {
if (e.status !== 422) throw e
const { data: t } = await github.rest.git.getRef({ owner, repo, ref: `tags/${TAG}` })
if (t.object.sha !== SHA) {
return core.setFailed(`tag ${TAG} already exists at ${t.object.sha}, not at the approved ${SHA}`)
}
}
try {
await github.rest.repos.getReleaseByTag({ owner, repo, tag: TAG })
} catch (e) {
if (e.status !== 404) throw e
await github.rest.repos.createRelease({ owner, repo, tag_name: TAG, name: TAG, generate_release_notes: true })
}
core.summary.addHeading(`Tagged ${TAG} at ${SHA.slice(0, 12)}`)
await core.summary.write()
2 changes: 2 additions & 0 deletions .github/workflows/publish_docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ on:
jobs:
docker-release:
runs-on: ubuntu-latest
# Docker Hub credentials are scoped to this environment (default branch / release tags only).
environment: release-ci
steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down
Loading