Since you need the notification content to be customizable, I would go with notification action + slack workflow (if your workspace has access to those).
Create a notification workflow
- Use the Workflow Builder to create a new workflow
- Choose
From a webhook
as the workflow trigger and define data variables
. These are the expected fields that your GitHub action will post to the webhook URL, e.g. title
, author
, pullRequestLink
etc. See Opened PR event payload for a list of fields available during this workflow event.
- Choose
Send a message
as the workflow action and define the message template. The interface lets you use the variables defined in the previous step as placeholders.
Set up a GitHub workflow to call the webhook
Use a GitHub action to post a structured payload to the workflow URL. The workflow can look something like this:
name: PR creation event notification
on:
pull_request:
- opened
- reopened
jobs:
notify:
name: PR notification
runs-on: [ubuntu-latest]
steps:
- name: Post message
uses: slackapi/[email protected]
env:
SLACK_WEBHOOK_URL: the webhook URL, preferably a GitHub secret
with:
payload: |
{
"title": ${{ toJson(github.event.pull_request.title) }},
"author": ${{ toJson(github.event.pull_request.user.login) }},
"link": ${{ toJson(github.event.pull_request.html_url) }}
}
Using toJson()
ensures that special characters are escaped properly (the payload
argument must be a valid JSON).
Reusing the workflow
This GHA feature wasn't available at the time the original question was asked.
If you expect to use the workflow in a large number of repositories, it's possible to make it reusable (to simplify maintenance). In that case, the generic workflow would have a workflow_call
trigger:
name: Reusable Slack notification workflow
on:
workflow_call:
inputs:
workflow-event:
required: true
type: string
secrets:
slackWebhookUrl:
required: true
jobs:
notify:
name: PR notification
runs-on: [ubuntu-latest]
steps:
- name: Post message
uses: slackapi/[email protected]
env:
SLACK_WEBHOOK_URL: the webhook URL, preferably a GitHub secret
with:
payload: |
{
"title": ${{ toJson(fromJson(inputs.workflow-event).title) }},
"author": ${{ toJson(fromJson(inputs.workflow-event).user.login) }},
"link": ${{ toJson(fromJson(inputs.workflow-event).html_url) }}
}
Let's assume this file is located at /.github/workflows/.generic-slack-notification.yml
in your-reusable-workflow-repo
repository within your-org
organization.
Notice that instead of referencing github.event.pull_request
(which is not available, since the reusable workflow is triggered by a different event), it now uses fromJson(inputs.workflow-event)
to parse the data from its input. The input is provided by the individual "caller workflows" from your repositories like this:
name: PR creation event notification
on:
pull_request:
- opened
- reopened
jobs:
notify:
name: PR notification
runs-on: [ubuntu-latest]
steps:
- name: Post message
uses: your-org/your-reusable-workflow-repo/.github/workflows/.generic-slack-notification.yml@main
with:
workflow-event: ${{ toJson(github.event.pull_request) }}
secrets:
slackWebhookUrl: ${{ secrets.SLACK_DEPLOYMENT_WEBHOOK_URL }}