Create release drafts from "Release ..." commits.

This commit adds a second job to the build workflow that runs after the
build job has completed. The job creates a GitHub Releases _draft_ that
needs to be manually published in order to be publicly available.

The job runs if, and only if, the following conditions are met:

- The build job has completed _successfully_, i.e. if the Gradle `build`
  task fails, the draft job doesn't run.

- The `push` event that triggered the workflow happened on the `master`
  branch, i.e. releases will never be created from temporary branches.

- The commit message of the most recent commit in the `push` event that
  triggered the workflow starts with `"Release "`, i.e. there must be a
  commit that explicitly tries to "release" a build.

- The version string does _not_ end with `-SNAPSHOT`, i.e. development
  builds will not be released.

All of these conditions act as safeguards, so we don't end up releasing
something we don't want to release, but they also prevent bloating the
Releases page with a bunch of useless drafts.

The job uses the `version` output variable from the build job that was
introduced in a recent commit to extract release notes using the script
that was also introduced recently, as well as for the name of the _tag_
to create when the release is published.

Note that the `GITHUB_TOKEN` environment variable is required to be set
when we want to use the GitHub CLI in a workflow [1]. The job also has
an explicit `contents: write` permission, which is required for creating
releases from GitHub Actions.
This commit is contained in:
Andreas Troelsen 2023-12-31 04:24:02 +01:00
parent 798ae0f578
commit e5ffe169a1

View File

@ -43,3 +43,37 @@ jobs:
| tr -d "'" \
)
echo "version=${version}" >> "${GITHUB_OUTPUT}"
draft:
needs: build
if: |
needs.build.result == 'success' &&
github.ref_name == 'master' &&
startsWith(github.event.head_commit.message, 'Release ') &&
!endsWith(needs.build.outputs.version, '-SNAPSHOT')
runs-on: ubuntu-latest
permissions:
contents: write
env:
VERSION: ${{ needs.build.outputs.version }}
steps:
- name: 'Checkout'
uses: actions/checkout@v4
- name: 'Download artifact'
uses: actions/download-artifact@v4
with:
name: MobArena.jar
- name: 'Extract release notes'
run: scripts/extract-release-notes -f github "${VERSION}" > release-notes.md
- name: 'Create release draft'
run: gh release create "${VERSION}" --draft --notes-file release-notes.md MobArena-*.jar
env:
GITHUB_TOKEN: ${{ github.token }}