6. Blast the results¶
This chapter will walk you through creating custom Slack messages depending on the outcome of your action.
6.1. What kind of alerts do you need?¶
Now that your scraper has been automated, let’s talk about alerts. You may want to receive alerts for the following scenarios
Your action succeeded, but nothing new was committed
Your action succeeded, and there’s new data
Your action failed 😔
6.2. Send a message with Slack’s Incoming Webhooks¶
Join #nicar23
at nicar-2023-sandbox.slack.com. You should have received an email invite to Slack, along with a Google Doc containing a webhook.
Slack’s incoming webhooks allow you to send messages from your apps. Visit Slack for instructions on how to create one for your channel.
Let’s send a simple message using the channel’s webhook.
curl -d '{"text":"Hello world. I am Iris :wave:"}' WEBHOOK
6.3. Save the Webook as a GitHub Secret¶
If your repository is public, you probably want to hide your webhook from others. You can do this by using GitHub Secrets.
To add the webhook to your GitHub Secrets, go to your repo and click on Settings
. In the left corner you will see the Secrets
dropdown menu. Select Actions
. Then select the New repository secrets
button in the top right corner.
Copy the webhook and add as a secret. Name it SLACK_WEBHOOK
6.3.1. Customizing your Slack messages¶
We will be using the Slack Incoming Webhook action from the GitHub Actions Marketplace.
Copy the code below. This step will send a message once your action is completed
- name: Slack Notification on SUCCESS
if: success()
uses: tokorom/action-slack-incoming-webhook@main
env:
INCOMING_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}
with:
text: A successful scrape!
Below that add another step for a message to be sent when your scrape fails - this time let’s add some useful links and colors.
- name: Slack Notification on FAILURE
if: failure()
uses: tokorom/action-slack-incoming-webhook@main
env:
INCOMING_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}
with:
text: Something went wrong.
attachments: |
[
{
"color": "bad",
"author_name": "${{ github.actor }}",
"author_icon": "${{ github.event.sender.avatar_url }}",
"fields": [
{
"title": "GitHub Actions URL",
"value": "https://github.com/${{github.repository}}/actions/runs/${{github.run_id}}"
}
]
}
]
Let’s test it out! Edit your action file to trigger a fail. For example, a simple change in jupyter execute scrape.ipynb
to jupyter execute s.ipynb
is going to trigger a fail. Let’s see what happens to your Slack message.
6.4. Use outputs to distinguish “Success”¶
The “Success” message will be sent whether or not a new file was committed. To differentiate between the two outcomes, we will use the output from the Add and commit
step to create two different slack messages.
In actions, some steps can create an output that can be referenced in another step.
Outputs are formatted like so: steps.<action id>.outputs.<output name>
. We will be using the committed
output, which is listed in the action’s documentation.
Let’s go back to your first Slack message and specify the message for a successful scrape without changes. Change the condition from success()
to
if: (success() && steps.add_commit.outputs.committed=='false')
And change the message to
text: Nothing was committed.
Now let’s add one last Slack message for a successful run with a brand new file commit.
- name: Slack Notification on no new commits
if: (success() && steps.add_commit.outputs.committed=='true')
uses: tokorom/action-slack-incoming-webhook@main
env:
INCOMING_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}
with:
text: New file alert!
attachments: |
[
{
"color": "good",
"author_name": "${{ github.actor }}",
"author_icon": "${{ github.event.sender.avatar_url }}",
"fields": [
{
"title": "GitHub Actions URL",
"value": "https://github.com/${{github.repository}}/actions/runs/${{github.run_id}}"
},
{
"title": "Commit URL",
"value": "https://github.com/${{github.repository}}/commits"
}
]
}
]
Delete the warn-data.csv file and run the action one more time. Because there has been a new file committed, your message will say “New file alert!”
Congratulations! You’ve completed the class!