GitHub Actions is a powerful tool for automating workflows directly within your GitHub repositories. One common use case is to send notifications to a Slack channel whenever specific events occur in your repository, such as successful builds, failed tests, or completed deployments. This guide will show you how to set up detailed Slack notifications using GitHub Actions, complete with examples to help you get started.
1. Why Send Slack Notifications from GitHub Actions?Integrating Slack notifications with GitHub Actions is beneficial for several reasons:
\
\
Before we dive into the setup, ensure you have the following:
To send notifications to Slack, you need to create a webhook URL through the Slack API.
Go to the Slack API page. Click on “Create an App.”
Choose “From scratch” and give your app a name, then select the Slack workspace where you want to install the app
2. Create an Incoming Webhook:
Navigate to “Incoming Webhooks” under the “Features” section on the left sidebar.
Toggle the “Activate Incoming Webhooks” switch. Click on “Add New Webhook to Workspace.”
Choose the channel where you want the notifications to be sent, and click “Allow.”
Copy the Webhook URL provided. This URL will be used in your GitHub Actions workflow to send notifications.
Now that you have a Slack Webhook URL, you can create a GitHub Actions workflow to send notifications.
To keep your Slack Webhook URL secure, store it as a GitHub Secret.
The example above sends a very basic message. However, Slack messages can be much more detailed and formatted using Block Kit.
\ Example of a Detailed Slack Notification:
name: Send Detailed Slack Notification on: push: branches: - main jobs: notify: runs-on: ubuntu-latest steps: - name: Send Detailed Slack Notification env: SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} run: | curl -X POST -H 'Content-type: application/json' --data '{ "blocks": [ { "type": "section", "text": { "type": "mrkdwn", "text": "*New Push to Main Branch*" } }, { "type": "section", "fields": [ { "type": "mrkdwn", "text": "*Repository:*\n'${{ github.repository }}'" }, { "type": "mrkdwn", "text": "*Branch:*\n'${{ github.ref }}'" }, { "type": "mrkdwn", "text": "*Commit Message:*\n'${{ github.event.head_commit.message }}'" }, { "type": "mrkdwn", "text": "*Committer:*\n'${{ github.event.head_commit.committer.name }}'" } ] } ] }' $SLACK_WEBHOOK_URLExplanation:
\
\ This will send a detailed notification to your Slack channel every time there’s a push to the main branch, including information like the repository name, branch, commit message, and committer.
7. Step 5: Trigger Notifications on Specific EventsGitHub Actions can be triggered by various events beyond just a push to a branch. Some common triggers include:
\
\ Workflow Failures:
on: workflow_run: workflows: ["CI"] types: - completed\ Scheduled Events:
on: schedule: - cron: '0 9 * * 1' # Every Monday at 9 AM\ Example for Workflow Failure Notification: This example sends a notification only if a workflow fails.
name: Notify on Failure on: workflow_run: workflows: ["CI"] types: - completed jobs: notify_on_failure: if: ${{ failure() }} runs-on: ubuntu-latest steps: - name: Notify Slack on Failure env: SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} run: | curl -X POST -H 'Content-type: application/json' --data '{ "text": ":x: *CI Workflow Failed*", "blocks": [ { "type": "section", "text": { "type": "mrkdwn", "text": "*Workflow:* CI\n*Status:* Failed\n*Repository:* ${{ github.repository }}" } } ] }' $SLACK_WEBHOOK_URLThis sends a failure notification with details about the workflow and the repository, ensuring your team is immediately aware of any issues.
8. Handling Advanced Use CasesYou may want to send different notifications depending on the type of event or the result of the action. For instance, you might want to send one type of message for a successful build and another for a failure.
\ Example: Conditional Notifications:
name: Notify on Success or Failure on: push: branches: - main jobs: build: runs-on: ubuntu-latest steps: - name: Checkout Code uses: actions/checkout@v2 - name: Run Tests run: | echo "Running tests..." # Simulate a test failure exit 1 notify: needs: build runs-on: ubuntu-latest steps: - name: Notify Slack env: SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} run: | if [ "${{ needs.build.result }}" == "success" ]; then curl -X POST -H 'Content-type: application/json' --data '{"text":"Build Succeeded!"}' $SLACK_WEBHOOK_URL; else curl -X POST -H 'Content-type: application/json' --data '{"text":"Build Failed!"}' $SLACK_WEBHOOK_URL; fiIn this example, the notification is sent based on whether the build was successful or failed.
9. Best PracticesTo ensure that your Slack notifications from GitHub Actions are effective, follow these best practices:
\
\
\
\
\
All Rights Reserved. Copyright , Central Coast Communications, Inc.