I want to notify the results of my app’s tests of iOS on GitHub Actions to Slack!
I want to integrate GitHub Actions and Slack!
In I want to build iOS project on GitHub Actions! post, I pushed to GitHub for the time being, and now I can build and test it!
Then you want to be notified of the result. So, this time, I would like to be able to notify Slack of the execution result of Action.
The content of this article was investigated around January 2020.
If you think that there is already an Action that can notify Slack…
When I searched for “Slack” on GitHub’s Marketplace, I found a lot of actions that could be used to notify Slack.
I decided to use “Slack Notify” at the beginning. I thought it seems to end soon, and when I tried it, an error was displayed.
Looking at the error, it seems that this Action can only be run on a Linux OS (I don’t know why …). I also tried some of the other actions at the top of the search results, but got the same error. Since it is essential to specify MacOS as long as you build the iOS application, I decided to hit the URL of the webhook directly.
Run the webhook URL in curl to send a notification to Slack.
I referred to here to get the URL of the webhook. All you have to do now is execute an Action that uses it.
# Notify to slack when build and test succeeded
- name: Slack success notification
- run: |
curl -X POST --data-urlencode 'payload={
"text": ":tada::tada::tada:${{github.repository}}: Build and Test succeeded!:tada::tada::tada:"
}' ${{secrets.SLACK_WEBHOOK_URL}}
github.repository
is an environment variable that can get a character string in the format of “{user name} / {repository name}”. The URL of the Slack webhook I got earlier is stored in Secrets in the Github repository. By registering here, even if the Public repository is forked, its Secrets value will not be copied. If you know the Webhook URL, anyone can post it to the channel you set it up, so don’t hard-code it. The registered value can be obtained by secrets.{Registered key name}
. When I ran it and the build and tests were successful, Slack was properly notified as follows.
By the way, in case of an error, it is set as follows.
##### Run build action #####
# Notify to slack when build failed
- name: Slack build failure notification
- if: failure()
run: |
curl -X POST --data-urlencode 'payload={
"text": ":fire::fire::fire:${{github.repository}}: Build failed..:fire::fire::fire:"
}' ${{secrets.SLACK_WEBHOOK_URL}}
##### Run unit test action #####
# Notify to slack when test failed
- name: Slack test failure notification
if: failure()
run: |
curl -X POST --data-urlencode 'payload={
"text": ":fire::fire::fire:${{github.repository}}: Test failed..:fire::fire::fire:"
}' ${{secrets.SLACK_WEBHOOK_URL}}
By inserting if: failure ()
, it will be executed when the previous Action fails. (Reference) The results when the build fails and when the test fails are as follows.
Actually, if the build fails, I want to display only the message “Build failed ..”, but I don’t know how to do it at the moment … (If you know, please let me know!)
Repository used for testing
https://github.com/u5-03/Swift-github-actions If you use the Public repository, Github Actions can be used for free, so if you want to use it for verification, use the Public repository!
Reference materials