When I first started working with GitHub about a month ago, there were already quite a few Slack notification actions available, but I found each to either be cumbersome to use, or lacking what seemed like simple features such as status based messages.
So I created adamkdean/simple-slack-notify to try and address those issues, and this post will show you how to use it.
The main features are:
As simple or complex as you need it to be
Status based messages meaning one step handles job successes, failures, and cancellations
JavaScript strings for embedding environment variables or custom logic into notification strings
Easy to add fields based on standard Slack JSON inputs
Example usage
The simplest use would consist of relying on the webhook's defaults and simply providing some text.
Overriding the channel is sometimes needed, such as to separate out builds, deployments, and alerts perhaps.
-name:Channel specific notificationuses:adamkdean/simple-slack-notify@masterwith:channel:'#alerts'text:'Somethingishappeningandsomeoneshouldprobablypanic'
The above works well, but what would really make someone panic is if we make the alert red, right?
You can use danger, warning, good, or a hex code such as #d90000.
The action also supports fields, but due to the limitations of GitHub actions only passing in inputs as strings, we can't use yaml arrays. So, this is how you'd specify a field:
-name:Specifying what to panic about notificationuses:adamkdean/simple-slack-notify@masterwith:channel:'#alerts'username:'PanicBot'text:'Somethingishappeningandsomeoneshouldprobablypanic'color:'danger'fields:|[{ "title": "Reason to panic", "value": "Deployed failed halfway through" }]
If there were multiple reasons to panic, you'd add more objects to the fields array:
-name:Specifying what to panic about notificationuses:adamkdean/simple-slack-notify@masterwith:channel:'#alerts'username:'PanicBot'text:'Somethingishappeningandsomeoneshouldprobablypanic'color:'danger'fields:|[{ "title": "Reason to panic", "value": "Deployed failed halfway through", "short": true },{ "title": "Timestamp", "value": "${Date.now()}", "short": true }]
Did you notice that some JavaScript snook in? Input strings are evaluated as a JavaScript strings, which means you can put environment variables into your messages, such as the GITHUB_WORKFLOW variable or GITHUB_RUN_NUMBER etc. The environment is stored within the env variable so to access environment variables in your strings, you simply use ${env.GITHUB_REPOSITORY} etc. Here's an example:
Now, each job has a status, which can be success, failed, or cancelled. Most other notification plugins use multiple blocks with if: success() and if: failed() etc but we don't need to do that. We can simply pass in the status and set status specific text. We use if: always() so that it runs regardless of whether the job is successful or not.
Please note that this project is no longer maintained, and has been archived.
Introduction
We've attempted to use a few of the Slack notification actions that are currently available, but they all seem to have limitations or be quite verbose, so we set out to create a simple yet effective action that just does what you need and nothing else. In the examples below, we'll show a few different variations of how the action could be used.
The main features are:
Status based messages meaning one step handles job successes, failures, and cancellations
JavaScript strings for embedding environment variables or custom logic into notification strings
Easy to add fields based on standard Slack JSON inputs
Be sure that you set the SLACK_WEBHOOK_URL environment variable, either in the job or in the step like this:
I just wanted to point out for anyone new to GitHub Actions that while above I'm referring to the master branch (i.e. adamkdean/simple-slack-notify@master), if you're using this in production, it's always wiser to use a version. As of writing, the latest version is 1.0.4 so we would use adamkdean/simple-slack-notify@1.0.4.