Skip to content
Draft
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
130 changes: 70 additions & 60 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,61 @@
name: Release

# Releases are cut from version tags pushed by mono's publish. A tag push runs
# this workflow from the tagged commit, so the dispatch job re-runs it on main:
# release artifacts must be signed as release.yaml@refs/heads/main. To build a
# tag by hand, run this workflow on main with that tag.
on:
schedule:
- cron: '0 0 * * 1' # every Monday at 00:00 UTC
push:
tags:
- 'v*'
workflow_dispatch:
inputs:
tag:
description: 'Existing vX.Y.Z tag on main that has no release yet'
required: true
type: string

permissions: {}

jobs:
dispatch:
name: Dispatch release on main
if: github.event_name == 'push'
runs-on: ubuntu-latest

permissions:
actions: write

steps:
- uses: step-security/harden-runner@aebdcf6156ab6691adf63218240e476564a1ed4c # v2.23.0
with:
egress-policy: block
allowed-endpoints: >
api.github.com:443

- name: Run the release workflow on main
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TAG: ${{ github.ref_name }}
run: gh workflow run release.yaml --repo "$GITHUB_REPOSITORY" --ref main -f tag="$TAG"

release:
name: Release
if: github.event_name == 'workflow_dispatch'
runs-on: ubuntu-latest

concurrency:
group: release-${{ inputs.tag }}
cancel-in-progress: false

# https://docs.github.com/en/actions/reference/authentication-in-a-workflow
permissions:
id-token: write
contents: write

env:
TAG: ${{ inputs.tag }}

steps:
- uses: step-security/harden-runner@aebdcf6156ab6691adf63218240e476564a1ed4c # v2.23.0
with:
Expand All @@ -40,72 +79,45 @@ jobs:
tuf-repo-cdn.sigstore.dev:443
uploads.github.com:443

- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Check the workflow runs on main
if: github.ref != 'refs/heads/main'
run: |
echo "Run this workflow on main: release artifacts must be signed as release.yaml@refs/heads/main."
exit 1

- name: Check the tag is a version
run: |
if [[ ! "$TAG" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "Tag $TAG is not a vX.Y.Z version."
exit 1
fi

- name: Check if any changes since last release
id: check
# Releases are immutable, so goreleaser must create the release itself.
# The list includes drafts, which goreleaser cannot see by tag.
- name: Check the tag has no release
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
git fetch --tags # no creds needed: repo is public; if this ever goes private, pass token explicitly here
TAG=$(git tag --points-at HEAD)
if [ -z "$TAG" ]; then
echo "No tag points at HEAD, checking if changes warrant a release."

# Get the last release tag
LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")

if [ -n "$LAST_TAG" ]; then
echo "Last release tag: $LAST_TAG"

# Get all changed files since last tag
CHANGED_FILES=$(git diff --name-only "$LAST_TAG"..HEAD)

# Only release if changes include .go files, go.mod, go.sum, .go-version, or LICENSE
RELEASE_WORTHY_CHANGES=$(echo "$CHANGED_FILES" | grep -E '(\.go$|^go\.mod$|^go\.sum$|^\.go-version$|^LICENSE$)' || true)

if [ -z "$RELEASE_WORTHY_CHANGES" ]; then
echo "No Go source files, go.mod, go.sum, .go-version, or LICENSE changed since last release. Skipping release."
echo "need_release=no" >> "$GITHUB_OUTPUT"
else
echo "Found release-worthy changes since last release:"
echo "$RELEASE_WORTHY_CHANGES"
echo "need_release=yes" >> "$GITHUB_OUTPUT"
fi
else
echo "No previous tags found. Creating first release."
echo "need_release=yes" >> "$GITHUB_OUTPUT"
fi
else
RELEASE=$(gh release view "$TAG" --json tagName --jq '.tagName' || echo "none")
if [ "$RELEASE" == "$TAG" ]; then
echo "A release exists for tag $TAG, which has the latest changes, so no need for a new tag or release."
echo "need_release=no" >> "$GITHUB_OUTPUT"
else
echo "Tag $TAG exists, but no release is associated. Need a new release."
echo "need_release=yes" >> "$GITHUB_OUTPUT"
echo "existing_tag=$TAG" >> "$GITHUB_OUTPUT"
fi
ids=$(gh api --paginate "repos/$GITHUB_REPOSITORY/releases?per_page=100" --jq '.[] | select(.tag_name == env.TAG) | .id')
if [ -n "$ids" ]; then
echo "A release for $TAG already exists. If it is a draft left by a failed run, delete it and run this workflow again."
exit 1
fi

- name: Bump version and push tag
id: create_tag
uses: mathieudutour/github-tag-action@af99e60ce8132224b8e6ebab5023449fe256ed46 # v7.0.0
if: steps.check.outputs.need_release == 'yes' && steps.check.outputs.existing_tag == ''
with:
github_token: ${{ secrets.GITHUB_TOKEN }}

- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
if: steps.check.outputs.need_release == 'yes'
with:
persist-credentials: false
ref: ${{ steps.check.outputs.existing_tag || steps.create_tag.outputs.new_tag }}
ref: refs/tags/${{ env.TAG }}
fetch-depth: 0

- name: Check the tag is on main
run: |
if ! git merge-base --is-ancestor HEAD origin/main; then
echo "Tag $TAG points at a commit that is not on main."
exit 1
fi

- uses: actions/setup-go@d8cbb8045d7f43706b9177cceb36c1f91a6644d5 # v7.1.0
if: steps.check.outputs.need_release == 'yes'
with:
go-version-file: './.go-version'
check-latest: true
Expand All @@ -114,20 +126,18 @@ jobs:

# Cosign is used by goreleaser to sign release artifacts.
- uses: sigstore/cosign-installer@756fe2d284c7292c4e9b62c8a28f1e5629c59532 # v4.1.3
if: steps.check.outputs.need_release == 'yes'
with:
# https://github.com/goreleaser/goreleaser/issues/6195
cosign-release: "v2.6.1"

- uses: goreleaser/goreleaser-action@a4f02ab5355a589de60d917a659ce617ed19b3de # v7.3.0
if: steps.check.outputs.need_release == 'yes'
with:
version: latest
install-only: true

- name: Release
if: steps.check.outputs.need_release == 'yes'
run: make release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TAG: ${{ steps.check.outputs.existing_tag || steps.create_tag.outputs.new_tag }}
GORELEASER_CURRENT_TAG: ${{ env.TAG }}
GIT_VERSION: ${{ env.TAG }}