|
| 1 | +name: Handle release process |
| 2 | + |
| 3 | +# This workflow automates the release process. |
| 4 | +# Only safe to use if the following conditions are met: |
| 5 | +# 1. `main` is protected and requires a PR to merge. |
| 6 | +# 2. Only squash-merging is allowed. |
| 7 | +# 3. PRs are required to be up-to-date with `main` before merging. |
| 8 | +# 4. Everyone is forbidden from creating releases manually. |
| 9 | + |
| 10 | +on: |
| 11 | + # Case 1: manual trigger -> initiate release process |
| 12 | + workflow_dispatch: |
| 13 | + |
| 14 | + # Case 2: release PR is being updated -> update changelog |
| 15 | + push: |
| 16 | + branches: |
| 17 | + - 'bot/release/*' |
| 18 | + |
| 19 | + # Case 3: release PR is merged -> create a new release |
| 20 | + pull_request: |
| 21 | + types: |
| 22 | + - closed |
| 23 | + branches: |
| 24 | + - main |
| 25 | + |
| 26 | +env: |
| 27 | + GH_TOKEN: ${{ secrets.PL_PASTEURBOT_PAT_PUBLIC }} |
| 28 | + |
| 29 | +jobs: |
| 30 | + trigger-pr: |
| 31 | + runs-on: ubuntu-latest |
| 32 | + if: github.event_name == 'workflow_dispatch' |
| 33 | + |
| 34 | + steps: |
| 35 | + - name: Checkout code |
| 36 | + uses: actions/checkout@v4 |
| 37 | + with: |
| 38 | + token: ${{ secrets.PL_PASTEURBOT_PAT_PUBLIC }} |
| 39 | + fetch-depth: 0 |
| 40 | + fetch-tags: true |
| 41 | + |
| 42 | + - name: Set up Python |
| 43 | + uses: actions/setup-python@v5 |
| 44 | + with: |
| 45 | + python-version-file: "pyproject.toml" |
| 46 | + |
| 47 | + - name: Install git-cliff |
| 48 | + run: | |
| 49 | + pip install git-cliff |
| 50 | +
|
| 51 | + - name: Generate changelog |
| 52 | + id: generate_changelog |
| 53 | + run: | |
| 54 | + git-cliff --output CHANGELOG.md --bump |
| 55 | + new_version=$(git-cliff --bumped-version) |
| 56 | + echo "new_version=$new_version" >> $GITHUB_OUTPUT |
| 57 | +
|
| 58 | + - name: Create Pull Request |
| 59 | + uses: peter-evans/create-pull-request@v7 |
| 60 | + with: |
| 61 | + token: ${{ secrets.PL_PASTEURBOT_PAT_PUBLIC }} |
| 62 | + committer: PasteurBot <${{ vars.PL_PASTEURBOT_EMAIL }}> |
| 63 | + author: PasteurBot <${{ vars.PL_PASTEURBOT_EMAIL }}> |
| 64 | + commit-message: "chore: update changelog" |
| 65 | + title: "chore: 🚢 release ${{ steps.generate_changelog.outputs.new_version }}" |
| 66 | + branch: bot/release/${{ steps.generate_changelog.outputs.new_version }} |
| 67 | + draft: false |
| 68 | + base: main |
| 69 | + add-paths: CHANGELOG.md |
| 70 | + body: | |
| 71 | + This PR contains the generated changelog for the release ${{ steps.generate_changelog.outputs.new_version }}. |
| 72 | +
|
| 73 | + ⚠️ **Merging this PR will immediately trigger a new release**. ⚠️ |
| 74 | +
|
| 75 | + To specify additional release notes, please edit this comment after the following line. |
| 76 | +
|
| 77 | + --- |
| 78 | +
|
| 79 | + update-pr: |
| 80 | + runs-on: ubuntu-latest |
| 81 | + if: github.event_name == 'push' && startsWith(github.ref, 'refs/heads/bot/release/') |
| 82 | + |
| 83 | + steps: |
| 84 | + - name: Checkout code |
| 85 | + uses: actions/checkout@v4 |
| 86 | + with: |
| 87 | + token: ${{ secrets.PL_PASTEURBOT_PAT_PUBLIC }} |
| 88 | + fetch-depth: 0 |
| 89 | + fetch-tags: true |
| 90 | + |
| 91 | + - name: Set up Python |
| 92 | + uses: actions/setup-python@v5 |
| 93 | + with: |
| 94 | + python-version-file: "pyproject.toml" |
| 95 | + |
| 96 | + - name: Install git-cliff |
| 97 | + run: | |
| 98 | + pip install git-cliff |
| 99 | +
|
| 100 | + - name: Generate changelog |
| 101 | + id: generate_changelog |
| 102 | + run: | |
| 103 | + git-cliff --output CHANGELOG.md --bump |
| 104 | + new_version=$(git-cliff --bumped-version) |
| 105 | + echo "new_version=$new_version" >> $GITHUB_OUTPUT |
| 106 | +
|
| 107 | + - name: Update PR with changelog |
| 108 | + run: | |
| 109 | + git config --global user.name "PasteurBot" |
| 110 | + git config --global user.email "${{ vars.PL_PASTEURBOT_EMAIL }}" |
| 111 | +
|
| 112 | + git add CHANGELOG.md |
| 113 | +
|
| 114 | + if git diff --cached --quiet; then |
| 115 | + echo "No changes to commit" |
| 116 | + else |
| 117 | + git commit -m "chore: update changelog before release" |
| 118 | + git push origin HEAD:${{ github.ref }} |
| 119 | + fi |
| 120 | +
|
| 121 | + release: |
| 122 | + runs-on: ubuntu-latest |
| 123 | + if: github.event_name == 'pull_request' && github.event.pull_request.merged == true && startsWith(github.event.pull_request.head.ref, 'bot/release/') |
| 124 | + |
| 125 | + steps: |
| 126 | + - name: Checkout code |
| 127 | + uses: actions/checkout@v4 |
| 128 | + with: |
| 129 | + token: ${{ secrets.PL_PASTEURBOT_PAT_PUBLIC }} |
| 130 | + ref: ${{ github.event.pull_request.merge_commit_sha }} |
| 131 | + fetch-depth: 0 |
| 132 | + fetch-tags: true |
| 133 | + |
| 134 | + - name: Set up Python |
| 135 | + uses: actions/setup-python@v5 |
| 136 | + with: |
| 137 | + python-version-file: "pyproject.toml" |
| 138 | + |
| 139 | + - name: Install git-cliff |
| 140 | + run: | |
| 141 | + pip install git-cliff |
| 142 | +
|
| 143 | + - name: Get version numbers |
| 144 | + id: get_version |
| 145 | + run: | |
| 146 | + current_version=$(gh release view --json tagName --jq '.tagName' || echo "v0.0.0") |
| 147 | + echo "current_version=$current_version" >> $GITHUB_OUTPUT |
| 148 | +
|
| 149 | + new_version=$(git-cliff --bumped-version) |
| 150 | + echo "new_version=$new_version" >> $GITHUB_OUTPUT |
| 151 | +
|
| 152 | + - name: Assemble release notes |
| 153 | + id: release_notes |
| 154 | + env: |
| 155 | + PR_BODY: ${{ github.event.pull_request.body }} |
| 156 | + run: | |
| 157 | + changelog=$(git-cliff --unreleased --bump --latest --strip all) |
| 158 | + # Strip off first line (which is the version number) |
| 159 | + changelog=$(printf "%s" "$changelog" | sed '1d') |
| 160 | +
|
| 161 | + # Get custom notes from the PR body |
| 162 | + custom_notes="$PR_BODY" |
| 163 | + # Fix line endings |
| 164 | + custom_notes=$(echo "$custom_notes" | tr -d '\r') |
| 165 | + # Keep only the part after the first '---' line |
| 166 | + custom_notes=$(echo "$custom_notes" | sed -n '/^---$/,$p' | sed '1d') |
| 167 | + # Strip leading / trailing whitespace |
| 168 | + custom_notes=$(echo "$custom_notes" | sed 's/^[ \t]*//;s/[ \t]*$//') |
| 169 | +
|
| 170 | + # Create the release notes file |
| 171 | + touch /tmp/notes.md |
| 172 | + echo "# Release ${{ steps.get_version.outputs.new_version }}" > /tmp/notes.md |
| 173 | + if [[ -n "$custom_notes" ]]; then |
| 174 | + printf "%s\n\n" "$custom_notes" >> /tmp/notes.md |
| 175 | + fi |
| 176 | + printf "## What's Changed\n%s\n" "$changelog" >> /tmp/notes.md |
| 177 | +
|
| 178 | + # Append link to full diff |
| 179 | + echo -e "\n**Full diff**: https://github.com/${{ github.repository }}/compare/${{ steps.get_version.outputs.current_version }}...${{ steps.get_version.outputs.new_version }}" >> /tmp/notes.md |
| 180 | +
|
| 181 | + echo "release_note_file=/tmp/notes.md" >> $GITHUB_OUTPUT |
| 182 | +
|
| 183 | + - name: Create new release |
| 184 | + run: | |
| 185 | + gh release create "${{ steps.get_version.outputs.new_version }}" \ |
| 186 | + --title "${{ steps.get_version.outputs.new_version }}" \ |
| 187 | + --notes-file "${{ steps.release_notes.outputs.release_note_file }}" \ |
| 188 | + --target ${{ github.event.pull_request.merge_commit_sha }} \ |
| 189 | + --latest |
0 commit comments