Created
September 7, 2022 16:56
-
-
Save bkuhl/b443cbcc588d850252460038e406a8c0 to your computer and use it in GitHub Desktop.
Manual GitHub Action for creating a release and publishing release details to Slack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This github action is a Manual workflow that will create a release and generate release notes from | |
# merged pull requests. After creating the release, it will generate a Slack message from the release | |
# notes and publish a release notification to a given Slack channel. | |
# | |
# Relevant links: | |
# - https://docs.github.com/en/actions/managing-workflow-runs/manually-running-a-workflow | |
# - https://app.slack.com/block-kit-builder | |
# | |
# This file goes into your project's .github/workflows folder | |
name: Create release | |
# Can change this to "push" during testing | |
on: workflow_dispatch | |
jobs: | |
create-release: | |
runs-on: ubuntu-latest | |
steps: | |
# version number generation can be changed to whatever you like, uses the current time to ensure uniqueness during testing | |
- name: Assign version | |
run: | | |
version="$(date +"%Y.%m.%d.%s")" | |
echo "Using version $version" | |
echo "RELEASE_VERSION=$version" >> $GITHUB_ENV | |
- name: Release | |
id: release | |
uses: softprops/action-gh-release@v1 | |
with: | |
tag_name: ${{ env.RELEASE_VERSION }} | |
generate_release_notes: true | |
- uses: octokit/[email protected] | |
id: get_latest_release | |
with: | |
route: GET /repos/${{ github.repository }}/releases/tags/${{ env.RELEASE_VERSION }} | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
- uses: devops-actions/[email protected] | |
with: | |
json: "${{ fromJson(steps.get_latest_release.outputs.data).body }}" | |
filename: RELEASE_NOTES | |
- name: Release notes formatting - Strip html comment from the beginning of release notes | |
run: sed --in-place -e 's/<\!--[^>]*-->//g' RELEASE_NOTES && cat RELEASE_NOTES | |
- name: Release notes formatting - Strip "full changelog" link | |
run: sed --in-place -e 's/\*\*Full Changelog.*//g' RELEASE_NOTES && cat RELEASE_NOTES | |
- name: Release notes formatting - Strip new contributor section | |
run: | | |
sed --in-place -E -e 's/## New Contributors.*//' RELEASE_NOTES | |
sed --in-place -E -e 's/\* .+? made their first contribution .+?//' RELEASE_NOTES | |
cat RELEASE_NOTES | |
- name: Release notes formatting - Replace markdown bullets with a more slack-compatible format | |
run: sed --in-place -E -e 's/\* (.+?) (by .+?) in (http.+?)/- <\3|\1> \2/' RELEASE_NOTES && cat RELEASE_NOTES | |
- name: Release notes formatting - Replace headers with bold text | |
run: sed --in-place -E -e 's/## (.*)/*\1*/' RELEASE_NOTES && cat RELEASE_NOTES | |
- name: Ensure release body is JSON-friendly | |
run: | | |
jq --null-input '{"body": $releaseNotes}' --rawfile releaseNotes RELEASE_NOTES > RELEASE_NOTES_JSON && cat RELEASE_NOTES_JSON | |
- name: Assign release notes to step variable | |
id: release-notes-formatting | |
run: echo "::set-output name=RELEASE_NOTES::$(cat RELEASE_NOTES_JSON | jq '.body')" | |
- name: notify-slack | |
id: slack | |
uses: slackapi/[email protected] | |
with: | |
# Slack channel id, channel name, or user id to post message. | |
# See also: https://api.slack.com/methods/chat.postMessage#channels | |
channel-id: 'my-slack-channel' | |
payload: | | |
{ | |
"text": "", | |
"blocks": [ | |
{ | |
"type": "section", | |
"text": { | |
"type": "mrkdwn", | |
"text": "<${{ steps.release.outputs.url }}|v${{ env.RELEASE_VERSION }}> has been released." | |
} | |
}, | |
{ | |
"type": "section", | |
"text": { | |
"type": "mrkdwn", | |
"text": ${{ steps.release-notes-formatting.outputs.RELEASE_NOTES }} | |
} | |
} | |
] | |
} | |
env: | |
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }} | |
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's an example of what this looks like: