Skip to content
Merged
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
4 changes: 2 additions & 2 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ updates:
directory: "/"
schedule:
interval: "weekly"
day: "monday"
day: "saturday"
time: "06:00"
open-pull-requests-limit: 10
reviewers:
Expand All @@ -24,7 +24,7 @@ updates:
directory: "/"
schedule:
interval: "weekly"
day: "monday"
day: "saturday"
time: "06:00"
open-pull-requests-limit: 5
reviewers:
Expand Down
248 changes: 248 additions & 0 deletions .github/workflows/auto-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
name: Weekly Release

on:
schedule:
- cron: "0 0 * * 0" # Sun 00:00 UTC
workflow_dispatch:
inputs:
version:
description: "Version to release (e.g. 1.2.0). Leave empty for the automatic minor bump."
required: false
type: string

permissions:
contents: read

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

jobs:
release-branch:
runs-on: ubuntu-latest
outputs:
has_changes: ${{ steps.check.outputs.has_changes }}
tag: ${{ steps.bump.outputs.tag }}
steps:
- uses: actions/checkout@v7
with:
ref: main
fetch-depth: 0
token: ${{ secrets.AUTO_MAINTENANCE_TOKEN }}

- name: Setup git
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"

- name: Check for unreleased changes
id: check
run: |
if awk '
/^## \[Unreleased\]$/ { in_section=1; next }
/^## / && in_section { exit }
in_section && NF { found=1 }
END { exit !found }
' CHANGELOG.md; then
echo "has_changes=true" >> "$GITHUB_OUTPUT"
else
echo "has_changes=false" >> "$GITHUB_OUTPUT"
echo "Nothing under [Unreleased], skipping release."
fi

- name: Install Rust toolchain
if: steps.check.outputs.has_changes == 'true'
uses: dtolnay/rust-toolchain@stable

- name: Install cargo-edit and cargo-vet
if: steps.check.outputs.has_changes == 'true'
uses: taiki-e/install-action@v2
with:
tool: cargo-edit,cargo-vet

- name: Bump workspace version
if: steps.check.outputs.has_changes == 'true'
id: bump
env:
INPUT_VERSION: ${{ inputs.version }}
run: |
if [ -n "$INPUT_VERSION" ]; then
VERSION="${INPUT_VERSION#v}" # accept "v1.2.0" or "1.2.0"

if ! [[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?$ ]]; then
echo "::error::'$INPUT_VERSION' is not a valid semver version."
exit 1
fi

cargo set-version --workspace "$VERSION"
else
cargo set-version --workspace --bump minor
fi

NEW_VERSION="$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].version')"

# Fail early if the tag exists (covers manual and automatic versions)
if git ls-remote --exit-code --tags origin "refs/tags/v${NEW_VERSION}" >/dev/null 2>&1; then
echo "::error::Tag v${NEW_VERSION} already exists."
exit 1
fi

echo "version=${NEW_VERSION}" >> "$GITHUB_OUTPUT"
echo "tag=v${NEW_VERSION}" >> "$GITHUB_OUTPUT"

- name: Update cargo vet exemptions
if: steps.check.outputs.has_changes == 'true'
run: |
cargo update
cargo vet regenerate exemptions
cargo vet regenerate imports

- name: Stamp CHANGELOG
if: steps.check.outputs.has_changes == 'true'
env:
VERSION: ${{ steps.bump.outputs.version }}
TAG: ${{ steps.bump.outputs.tag }}
REPO: ${{ github.repository }}
run: |
HEADING="## [${VERSION}](https://github.com/${REPO}/releases/tag/${TAG})"
awk -v heading="$HEADING" '
/^## \[Unreleased\]$/ {
print
print ""
print heading
next
}
{ print }
' CHANGELOG.md > CHANGELOG.md.tmp
mv CHANGELOG.md.tmp CHANGELOG.md

- name: Push branch, open PR, enable auto-merge
if: steps.check.outputs.has_changes == 'true'
env:
TAG: ${{ steps.bump.outputs.tag }}
VERSION: ${{ steps.bump.outputs.version }}
GH_TOKEN: ${{ secrets.AUTO_MAINTENANCE_TOKEN }}
run: |
BRANCH="release/${TAG}"
BODY="${RUNNER_TEMP}/pr-body.md"

# This version's section from the stamped changelog
awk -v v="$VERSION" '
index($0, "## [" v "]") == 1 { found=1; next }
/^## / && found { exit }
found { print }
' CHANGELOG.md > "$BODY"

if [ ! -s "$BODY" ]; then
echo "Automated Release." > "$BODY"
fi

git switch -c "$BRANCH"
git add -A Cargo.toml Cargo.lock CHANGELOG.md
if [ -d supply-chain ]; then
git add -A supply-chain/
fi
git add -u -- '*Cargo.toml'
git commit -m "bump: to ${TAG}"
git push origin "$BRANCH"

gh pr create \
--base main \
--head "$BRANCH" \
--title "bump: to ${TAG}" \
--body-file "$BODY"

gh pr merge "$BRANCH" --auto --squash

tag:
needs: release-branch
if: needs.release-branch.outputs.has_changes == 'true'
runs-on: ubuntu-latest
timeout-minutes: 120
steps:
- name: Wait for PR to pass and auto-merge
id: wait
env:
BRANCH: release/${{ needs.release-branch.outputs.tag }}
GH_TOKEN: ${{ secrets.AUTO_MAINTENANCE_TOKEN }}
GH_REPO: ${{ github.repository }}
run: |
while true; do
STATE="$(gh pr view "$BRANCH" --json state --jq .state)"
case "$STATE" in
MERGED)
SHA="$(gh pr view "$BRANCH" --json mergeCommit --jq .mergeCommit.oid)"
echo "sha=${SHA}" >> "$GITHUB_OUTPUT"
exit 0 ;;
CLOSED)
echo "::error::Release PR was closed without merging."
exit 1 ;;
esac
sleep 30
done

- name: Switch to main
uses: actions/checkout@v7
with:
ref: main
fetch-depth: 0
token: ${{ secrets.AUTO_MAINTENANCE_TOKEN }}

- name: Create tag
env:
TAG: ${{ needs.release-branch.outputs.tag }}
SHA: ${{ steps.wait.outputs.sha }}
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git tag -a "$TAG" -m "$TAG" "$SHA"
git push origin "$TAG"

- name: Create GitHub release
env:
TAG: ${{ needs.release-branch.outputs.tag }}
SHA: ${{ steps.wait.outputs.sha }}
GH_TOKEN: ${{ secrets.AUTO_MAINTENANCE_TOKEN }}
GH_REPO: ${{ github.repository }}
run: |
VERSION="${TAG#v}"
NOTES="${RUNNER_TEMP}/notes.md"
GENERATED="${RUNNER_TEMP}/generated.md"

# Pre-release versions (e.g. 2.0.0-rc.1) contain a "-"
FLAGS=()
if [[ "$VERSION" == *-* ]]; then
FLAGS+=(--prerelease --latest=false)
fi

# 1. This version's section from the changelog at the merged commit
git show "${SHA}:CHANGELOG.md" | awk -v v="$VERSION" '
index($0, "## [" v "]") == 1 { found=1; next }
/^## / && found { exit }
found { print }
' > "$NOTES"

# 2. GitHub's generated notes (the tag exists now, so this works)
gh api "repos/${GH_REPO}/releases/generate-notes" \
-f tag_name="$TAG" --jq .body > "$GENERATED" || : > "$GENERATED"

# 3. Append "New Contributors" and "Full Changelog" from the generated notes
if [ -s "$NOTES" ]; then
if grep -q '^## New Contributors' "$GENERATED"; then
{
echo
awk '
/^## New Contributors/ { f=1 }
/^\*\*Full Changelog\*\*/ { f=0 }
f
' "$GENERATED"
} >> "$NOTES"
fi
grep '^\*\*Full Changelog\*\*' "$GENERATED" >> "$NOTES" || true

gh release create "$TAG" --verify-tag --title "$TAG" \
--notes-file "$NOTES" "${FLAGS[@]}"
else
gh release create "$TAG" --verify-tag --title "$TAG" \
--generate-notes "${FLAGS[@]}"
fi
Loading
Loading