|
| 1 | +name: Release PR |
| 2 | + |
| 3 | +# Keeps one release pull request, chore: release <version>, open from release/next while CHANGELOG.md has entries |
| 4 | +# under ## Unreleased. scripts/release_pr.py renames that section to the next +tibiash.N version and sets __version__ |
| 5 | +# to it. The tibia-sh App pushes the branch and opens the pull request, so the pull request's CI runs, which it would |
| 6 | +# not for one opened with GITHUB_TOKEN. Nothing merges it by itself: drptbl's merge is the release. The tag job of that |
| 7 | +# merge's run tags v<version> on the merge commit, and the tag starts release.yml. |
| 8 | +on: |
| 9 | + push: |
| 10 | + branches: |
| 11 | + - main |
| 12 | + workflow_dispatch: |
| 13 | + |
| 14 | +permissions: {} |
| 15 | + |
| 16 | +# Runs take turns, and a pending run is never replaced, so the run of every push to main, a release merge's included, |
| 17 | +# runs to the end. |
| 18 | +concurrency: |
| 19 | + group: release-pr |
| 20 | + cancel-in-progress: false |
| 21 | + queue: max |
| 22 | + |
| 23 | +defaults: |
| 24 | + run: |
| 25 | + shell: bash |
| 26 | + |
| 27 | +jobs: |
| 28 | + # Mints the App's token in the release-trigger environment, which deploys from main alone. The job installs |
| 29 | + # nothing, restores no cache and runs no dependency code: only git, gh and the standard-library |
| 30 | + # scripts/release_pr.py from its own checkout, under python3 -I. It commits only the two files the script wrote. |
| 31 | + propose: |
| 32 | + runs-on: ubuntu-latest |
| 33 | + timeout-minutes: 10 |
| 34 | + environment: release-trigger |
| 35 | + permissions: |
| 36 | + contents: read # the checkout and its tags |
| 37 | + pull-requests: read # the open release pull request |
| 38 | + steps: |
| 39 | + - name: Check out |
| 40 | + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 |
| 41 | + with: |
| 42 | + fetch-depth: 0 |
| 43 | + fetch-tags: true |
| 44 | + persist-credentials: false |
| 45 | + # Exit 3 means CHANGELOG.md has no unreleased entries and nothing was written. The script checks that before it |
| 46 | + # works out a version, so a release merge's run, whose version is not tagged yet, ends here green. |
| 47 | + - name: Write the release |
| 48 | + id: propose |
| 49 | + run: | |
| 50 | + tags="$RUNNER_TEMP/tags.txt" |
| 51 | + git show-ref --tags --dereference > "$tags" |
| 52 | + if version="$(python3 -I scripts/release_pr.py propose --tags "$tags" --body "$RUNNER_TEMP/body.md")"; then |
| 53 | + if [[ ! $version =~ ^[0-9]+\.[0-9]+\.[0-9]+\+tibiash\.[0-9]+$ ]]; then |
| 54 | + echo "::error::release_pr.py propose printed a version that is not x.y.z+tibiash.N." |
| 55 | + exit 1 |
| 56 | + fi |
| 57 | + echo "Proposing $version." |
| 58 | + echo "version=$version" >> "$GITHUB_OUTPUT" |
| 59 | + else |
| 60 | + status=$? |
| 61 | + if [ "$status" -ne 3 ]; then |
| 62 | + exit "$status" |
| 63 | + fi |
| 64 | + echo "CHANGELOG.md has no unreleased entries, so there is nothing to release." |
| 65 | + fi |
| 66 | + # With github.token. The App's login is its slug with [bot], as GitHub shows its pull requests. |
| 67 | + - name: Find the open release pull request |
| 68 | + id: pr |
| 69 | + env: |
| 70 | + GH_TOKEN: ${{ github.token }} |
| 71 | + GH_REPO: ${{ github.repository }} |
| 72 | + APP_LOGIN: ${{ vars.TIBIA_SH_APP_SLUG }}[bot] |
| 73 | + run: | |
| 74 | + if [[ ! $APP_LOGIN =~ ^[a-z0-9-]+\[bot\]$ ]]; then |
| 75 | + echo "::error::The App's login, from vars.TIBIA_SH_APP_SLUG, is not <slug>[bot]." |
| 76 | + exit 1 |
| 77 | + fi |
| 78 | + query="head=$GITHUB_REPOSITORY_OWNER:release/next&base=main&state=open" |
| 79 | + number="$(timeout --kill-after=10 120 gh api "repos/{owner}/{repo}/pulls?$query" \ |
| 80 | + --jq 'map(select(.user.login == env.APP_LOGIN)) | .[0].number // empty')" |
| 81 | + if [[ ! $number =~ ^[0-9]*$ ]]; then |
| 82 | + echo "::error::The open release pull request has no plain number." |
| 83 | + exit 1 |
| 84 | + fi |
| 85 | + echo "number=$number" >> "$GITHUB_OUTPUT" |
| 86 | + # A token of the App for this repository alone, which expires within the hour: pull requests, to close the one |
| 87 | + # that has nothing left to release. |
| 88 | + - id: close_token |
| 89 | + if: ${{ steps.propose.outputs.version == '' && steps.pr.outputs.number != '' }} |
| 90 | + uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0 |
| 91 | + with: |
| 92 | + client-id: ${{ vars.TIBIA_SH_APP_CLIENT_ID }} |
| 93 | + private-key: ${{ secrets.TIBIA_SH_APP_PRIVATE_KEY }} |
| 94 | + owner: tibia-sh |
| 95 | + repositories: tibiawiki-sql |
| 96 | + permission-pull-requests: write |
| 97 | + - name: Close the release pull request |
| 98 | + if: ${{ steps.propose.outputs.version == '' && steps.pr.outputs.number != '' }} |
| 99 | + env: |
| 100 | + GH_TOKEN: ${{ steps.close_token.outputs.token }} |
| 101 | + GH_REPO: ${{ github.repository }} |
| 102 | + PR: ${{ steps.pr.outputs.number }} |
| 103 | + run: | |
| 104 | + timeout --kill-after=10 120 gh pr close "$PR" \ |
| 105 | + --comment "CHANGELOG.md on main has no unreleased entries, so there is nothing to release." |
| 106 | + - name: Commit the release |
| 107 | + if: ${{ steps.propose.outputs.version != '' }} |
| 108 | + env: |
| 109 | + VERSION: ${{ steps.propose.outputs.version }} |
| 110 | + GIT_AUTHOR_NAME: github-actions[bot] |
| 111 | + GIT_AUTHOR_EMAIL: 41898282+github-actions[bot]@users.noreply.github.com |
| 112 | + GIT_COMMITTER_NAME: github-actions[bot] |
| 113 | + GIT_COMMITTER_EMAIL: 41898282+github-actions[bot]@users.noreply.github.com |
| 114 | + run: | |
| 115 | + if [[ "$(git status --porcelain)" != $' M CHANGELOG.md\n M tibiawikisql/__init__.py' ]]; then |
| 116 | + echo "::error::propose must change CHANGELOG.md and tibiawikisql/__init__.py, and nothing else." |
| 117 | + exit 1 |
| 118 | + fi |
| 119 | + git add CHANGELOG.md tibiawikisql/__init__.py |
| 120 | + git commit --quiet -m "chore: release $VERSION" |
| 121 | + # A token of the App for this repository alone, which expires within the hour: contents, to push release/next, |
| 122 | + # and pull requests, to open or update its pull request. Minted after the release is committed, so nothing |
| 123 | + # before this step runs with it. |
| 124 | + - id: push_token |
| 125 | + if: ${{ steps.propose.outputs.version != '' }} |
| 126 | + uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0 |
| 127 | + with: |
| 128 | + client-id: ${{ vars.TIBIA_SH_APP_CLIENT_ID }} |
| 129 | + private-key: ${{ secrets.TIBIA_SH_APP_PRIVATE_KEY }} |
| 130 | + owner: tibia-sh |
| 131 | + repositories: tibiawiki-sql |
| 132 | + permission-contents: write |
| 133 | + permission-pull-requests: write |
| 134 | + # Force-pushes release/next, which replaces the previous proposal, then opens the pull request or updates the open |
| 135 | + # one. The token reaches git through a credential helper that reads it from the environment, so it is never |
| 136 | + # written to .git/config and never appears on a command line. Auto-merge stays off. |
| 137 | + - name: Push release/next and open or update its pull request |
| 138 | + if: ${{ steps.propose.outputs.version != '' }} |
| 139 | + env: |
| 140 | + GH_TOKEN: ${{ steps.push_token.outputs.token }} |
| 141 | + GH_REPO: ${{ github.repository }} |
| 142 | + VERSION: ${{ steps.propose.outputs.version }} |
| 143 | + PR: ${{ steps.pr.outputs.number }} |
| 144 | + run: | |
| 145 | + title="chore: release $VERSION" |
| 146 | + # shellcheck disable=SC2016 # git's shell expands $GH_TOKEN when it runs the helper |
| 147 | + timeout --kill-after=10 120 git -c credential.helper= \ |
| 148 | + -c 'credential.helper=!f() { echo username=x-access-token; echo "password=$GH_TOKEN"; }; f' \ |
| 149 | + push --force origin HEAD:refs/heads/release/next |
| 150 | + if [[ -n $PR ]]; then |
| 151 | + timeout --kill-after=10 120 gh api --method PATCH "repos/{owner}/{repo}/pulls/$PR" --silent \ |
| 152 | + -f title="$title" -F body=@"$RUNNER_TEMP/body.md" |
| 153 | + else |
| 154 | + timeout --kill-after=10 120 gh api "repos/{owner}/{repo}/pulls" --silent \ |
| 155 | + -f head=release/next -f base=main -f title="$title" -F body=@"$RUNNER_TEMP/body.md" |
| 156 | + fi |
| 157 | +
|
| 158 | + # Tags the release that drptbl's merge of the release pull request made. The list of a commit's pull requests has no |
| 159 | + # merged_by, so the job reads, with github.token, the pull request whose merge commit is the pushed commit on its |
| 160 | + # own, and scripts/release_pr.py decides from it. Only the App's release/next pull request, merged by drptbl, tags |
| 161 | + # v<version> at the merge commit. The same isolation as the propose job, and the App's token, which the release tags |
| 162 | + # ruleset lets create v* tags, is minted only to create the tag. The tag's push starts release.yml. |
| 163 | + tag: |
| 164 | + runs-on: ubuntu-latest |
| 165 | + timeout-minutes: 10 |
| 166 | + environment: release-trigger |
| 167 | + permissions: |
| 168 | + contents: read # the checkout and its tags |
| 169 | + pull-requests: read # the pull request the pushed commit merged |
| 170 | + steps: |
| 171 | + - name: Check out |
| 172 | + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 |
| 173 | + with: |
| 174 | + fetch-depth: 0 |
| 175 | + fetch-tags: true |
| 176 | + persist-credentials: false |
| 177 | + # With no merged pull request, as for a direct push, the script is given null. It fails when the version is |
| 178 | + # untagged, and skips otherwise. |
| 179 | + - name: Read the merged pull request |
| 180 | + env: |
| 181 | + GH_TOKEN: ${{ github.token }} |
| 182 | + GH_REPO: ${{ github.repository }} |
| 183 | + run: | |
| 184 | + numbers="$(timeout --kill-after=10 120 gh api "repos/{owner}/{repo}/commits/$GITHUB_SHA/pulls" \ |
| 185 | + --jq '[.[] | select(.merge_commit_sha == env.GITHUB_SHA and .merged_at) | "\(.number)"] | join(" ")')" |
| 186 | + if [[ -z $numbers ]]; then |
| 187 | + echo null > "$RUNNER_TEMP/pr.json" |
| 188 | + elif [[ $numbers =~ ^[0-9]+$ ]]; then |
| 189 | + timeout --kill-after=10 120 gh api "repos/{owner}/{repo}/pulls/$numbers" > "$RUNNER_TEMP/pr.json" |
| 190 | + else |
| 191 | + echo "::error::More than one merged pull request has $GITHUB_SHA as its merge commit." |
| 192 | + exit 1 |
| 193 | + fi |
| 194 | + # fail turns the run red: the version is not x.y.z+tibiash.N, its tag points at another commit, or it has no |
| 195 | + # tag and no merged pull request was found, as for a release merge GitHub did not list yet. |
| 196 | + - name: Decide |
| 197 | + id: decide |
| 198 | + env: |
| 199 | + APP_LOGIN: ${{ vars.TIBIA_SH_APP_SLUG }}[bot] |
| 200 | + run: | |
| 201 | + tags="$RUNNER_TEMP/tags.txt" |
| 202 | + git show-ref --tags --dereference > "$tags" |
| 203 | + result="$(python3 -I scripts/release_pr.py tag-decision --tags "$tags" --pr "$RUNNER_TEMP/pr.json" \ |
| 204 | + --head "$GITHUB_SHA" --app-login "$APP_LOGIN")" |
| 205 | + decision="$(sed -n 's/^decision=//p' <<< "$result")" |
| 206 | + version="$(sed -n 's/^version=//p' <<< "$result")" |
| 207 | + case $decision in |
| 208 | + tag) echo "Tagging v$version at $GITHUB_SHA." ;; |
| 209 | + noop) echo "v$version already points at $GITHUB_SHA." ;; |
| 210 | + skip) echo "$GITHUB_SHA is not drptbl's merge of the App's release pull request, so nothing is tagged." ;; |
| 211 | + fail) |
| 212 | + echo "::error::v$version cannot be tagged at $GITHUB_SHA. The version is not x.y.z+tibiash.N," \ |
| 213 | + "its tag points at another commit, or it has no tag and no merged pull request was found." \ |
| 214 | + "For a release merge, re-run this run once the pull request shows as merged." |
| 215 | + exit 1 |
| 216 | + ;; |
| 217 | + *) |
| 218 | + echo "::error::release_pr.py tag-decision printed an unknown decision." |
| 219 | + exit 1 |
| 220 | + ;; |
| 221 | + esac |
| 222 | + echo "decision=$decision" >> "$GITHUB_OUTPUT" |
| 223 | + echo "version=$version" >> "$GITHUB_OUTPUT" |
| 224 | + # A token of the App for this repository alone, which expires within the hour: contents, to create the tag. |
| 225 | + - id: tag_token |
| 226 | + if: ${{ steps.decide.outputs.decision == 'tag' }} |
| 227 | + uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0 |
| 228 | + with: |
| 229 | + client-id: ${{ vars.TIBIA_SH_APP_CLIENT_ID }} |
| 230 | + private-key: ${{ secrets.TIBIA_SH_APP_PRIVATE_KEY }} |
| 231 | + owner: tibia-sh |
| 232 | + repositories: tibiawiki-sql |
| 233 | + permission-contents: write |
| 234 | + - name: Create the tag |
| 235 | + if: ${{ steps.decide.outputs.decision == 'tag' }} |
| 236 | + env: |
| 237 | + GH_TOKEN: ${{ steps.tag_token.outputs.token }} |
| 238 | + GH_REPO: ${{ github.repository }} |
| 239 | + VERSION: ${{ steps.decide.outputs.version }} |
| 240 | + run: | |
| 241 | + if [[ ! $VERSION =~ ^[0-9]+\.[0-9]+\.[0-9]+\+tibiash\.[0-9]+$ || ! $GITHUB_SHA =~ ^[0-9a-f]{40}$ ]]; then |
| 242 | + echo "::error::The version is not x.y.z+tibiash.N, or the pushed commit is not a full SHA." |
| 243 | + exit 1 |
| 244 | + fi |
| 245 | + timeout --kill-after=10 120 gh api "repos/{owner}/{repo}/git/refs" --silent \ |
| 246 | + -f ref="refs/tags/v$VERSION" -f sha="$GITHUB_SHA" |
| 247 | + echo "Tagged v$VERSION at $GITHUB_SHA." |
0 commit comments