Skip to content

Instantly share code, notes, and snippets.

@bkuhl
Created September 7, 2022 16:56
Show Gist options
  • Save bkuhl/b443cbcc588d850252460038e406a8c0 to your computer and use it in GitHub Desktop.
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 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/request-action@v2.1.6
id: get_latest_release
with:
route: GET /repos/${{ github.repository }}/releases/tags/${{ env.RELEASE_VERSION }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- uses: devops-actions/json-to-file@v1.0.0
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/slack-github-action@v1.21.0
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
@bkuhl
Copy link
Author

bkuhl commented Sep 7, 2022

Here's an example of what this looks like:

Screen Shot 2022-09-07 at 1 15 24 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment