Skip to content

Instantly share code, notes, and snippets.

@JoopAue
Last active March 6, 2023 14:01
Show Gist options
  • Save JoopAue/540fa036bea5e086fc5a0156fb235d7d to your computer and use it in GitHub Desktop.
Save JoopAue/540fa036bea5e086fc5a0156fb235d7d to your computer and use it in GitHub Desktop.
Slack Notifications for Github Actions jobs using 8398a7/action-slack@v3
name: Example Slack Notification
on: push
jobs:
example-slack:
runs-on: ubuntu-latest
permissions:
contents: read
actions: read
steps:
- name: Slack notification # Step should be at the end
if: always() # make sure it always executes, also on failures before this step
uses: 8398a7/action-slack@v3
with:
status: custom
mention: 'channel'
if_mention: 'failure'
fields: repo,message,commit,author,action,eventName,ref,workflow,job,took,pullRequest
custom_payload: |
{
attachments: [
{
color: '${{ job.status }}' === 'success' ? 'good' : 'danger', title: ('${{ job.status }}' === 'success' ? '🟢' : '🔴') +` ${process.env.AS_WORKFLOW}/${process.env.AS_JOB} ` + ('${{ job.status }}' === 'success' ? 'was successful!' : 'failed'),
fields: [{
title: 'Repository',
value: `${process.env.AS_REPO}`,
short: true
}, {
title: 'Author',
value: `${process.env.AS_AUTHOR}`.split('<')[0],
short: true
}, {
title: `${process.env.AS_REF}`.startsWith('refs/heads/') ? 'Branch' : `${process.env.AS_REF}`.startsWith('refs/tags/') ? 'Tag' : 'Git Reference',
value: `${process.env.AS_REF}`.startsWith('refs/heads/') ? `${process.env.AS_REF}`.split("refs/heads/").slice(1).join('') : `${process.env.AS_REF}`.startsWith('refs/tags/') ? `${process.env.AS_REF}`.split("refs/tags/").slice(1).join('') : `${process.env.AS_REF}`,
short: true
}, {
title: 'Commit',
value: `${process.env.AS_COMMIT}`,
short: true
}, {
title: 'Triggered By',
value: [{'origin': 'push', 'new': 'Push'}, {'origin': 'pull_request', 'new': 'Pull Request'}, {'origin': 'schedule', 'new': 'Schedule'}, {'origin': 'repository_dispatch', 'new': 'Deploy'}, {'origin': 'workflow_dispatch', 'new': 'GitHub Actions'}].find(item => item.origin === `${process.env.AS_EVENT_NAME}`)?.new || `${process.env.AS_EVENT_NAME}`,
short: true
}, {
title: 'Execution Time',
value: `${process.env.AS_TOOK}`,
short: true
}]
}]
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment