|
| 1 | +name: Release |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + bump: |
| 7 | + description: "Version bump type" |
| 8 | + required: true |
| 9 | + type: choice |
| 10 | + options: |
| 11 | + - patch |
| 12 | + - minor |
| 13 | + - major |
| 14 | + |
| 15 | +env: |
| 16 | + CARGO_TERM_COLOR: always |
| 17 | + |
| 18 | +jobs: |
| 19 | + prepare-release: |
| 20 | + runs-on: ubuntu-latest |
| 21 | + permissions: |
| 22 | + contents: write |
| 23 | + pull-requests: write |
| 24 | + steps: |
| 25 | + - uses: actions/checkout@v4 |
| 26 | + with: |
| 27 | + fetch-depth: 0 |
| 28 | + |
| 29 | + - name: Calculate new version |
| 30 | + id: version |
| 31 | + run: | |
| 32 | + CURRENT=$(grep '^version' Cargo.toml | head -1 | sed 's/version = "\(.*\)"/\1/') |
| 33 | + IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT" |
| 34 | +
|
| 35 | + case "${{ inputs.bump }}" in |
| 36 | + major) MAJOR=$((MAJOR + 1)); MINOR=0; PATCH=0 ;; |
| 37 | + minor) MINOR=$((MINOR + 1)); PATCH=0 ;; |
| 38 | + patch) PATCH=$((PATCH + 1)) ;; |
| 39 | + esac |
| 40 | +
|
| 41 | + NEW="${MAJOR}.${MINOR}.${PATCH}" |
| 42 | + echo "current=$CURRENT" >> "$GITHUB_OUTPUT" |
| 43 | + echo "new=$NEW" >> "$GITHUB_OUTPUT" |
| 44 | + echo "Bumping $CURRENT -> $NEW" |
| 45 | +
|
| 46 | + - name: Update Cargo.toml version |
| 47 | + run: sed -i "s/^version = \"${{ steps.version.outputs.current }}\"/version = \"${{ steps.version.outputs.new }}\"/" Cargo.toml |
| 48 | + |
| 49 | + - name: Generate changelog entry |
| 50 | + env: |
| 51 | + GH_TOKEN: ${{ github.token }} |
| 52 | + run: | |
| 53 | + VERSION="${{ steps.version.outputs.new }}" |
| 54 | + DATE=$(date +%Y-%m-%d) |
| 55 | + LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "") |
| 56 | +
|
| 57 | + # Generate notes from GitHub (PR titles since last tag) |
| 58 | + if [ -n "$LAST_TAG" ]; then |
| 59 | + NOTES=$(gh api repos/${{ github.repository }}/releases/generate-notes \ |
| 60 | + -f tag_name="v${VERSION}" \ |
| 61 | + -f target_commitish=main \ |
| 62 | + -f previous_tag_name="$LAST_TAG" \ |
| 63 | + --jq '.body') |
| 64 | + else |
| 65 | + NOTES="- Initial release" |
| 66 | + fi |
| 67 | +
|
| 68 | + # Build the new changelog entry |
| 69 | + ENTRY="## [${VERSION}] - ${DATE} |
| 70 | +
|
| 71 | + ${NOTES}" |
| 72 | +
|
| 73 | + # Prepend to CHANGELOG.md after the header line |
| 74 | + awk -v entry="$ENTRY" ' |
| 75 | + /^# Changelog/ { print; getline; print; print ""; print entry; next } |
| 76 | + { print } |
| 77 | + ' CHANGELOG.md > CHANGELOG.tmp && mv CHANGELOG.tmp CHANGELOG.md |
| 78 | +
|
| 79 | + - name: Create release PR |
| 80 | + env: |
| 81 | + GH_TOKEN: ${{ github.token }} |
| 82 | + run: | |
| 83 | + BRANCH="release/v${{ steps.version.outputs.new }}" |
| 84 | + git checkout -b "$BRANCH" |
| 85 | + git add Cargo.toml CHANGELOG.md |
| 86 | + git config user.name "github-actions[bot]" |
| 87 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 88 | + git commit -m "release: v${{ steps.version.outputs.new }}" |
| 89 | + git push origin "$BRANCH" |
| 90 | + gh pr create \ |
| 91 | + --title "release: v${{ steps.version.outputs.new }}" \ |
| 92 | + --body "Bumps version from ${{ steps.version.outputs.current }} to ${{ steps.version.outputs.new }}." \ |
| 93 | + --label "release" |
0 commit comments