Automating Slack Notifications with GitHub Actions

Sahil Khurana
August 11, 2023

In modern software development, continuous integration and continuous deployment (CI/CD) practices have become essential for delivering high-quality code faster. One crucial aspect of CI/CD is notifying team members about important events and updates in the development process. Slack, a popular team collaboration tool, serves as an excellent platform for real-time communication within development teams. In this blog, we will explore how to set up automated Slack notifications using GitHub Actions to keep your team informed and enhance collaboration.

Prerequisites

Before we begin, make sure you have the following in place:

  1. A GitHub repository where your project’s source code resides
  2. A Slack workspace where you want to receive the notifications.
  3. Appropriate permissions to create GitHub Actions workflows in your repository.
  4. Slack API credentials to send messages to your workspace.

Step 1: Set Up Slack Integration

To enable GitHub Actions to send messages to your Slack workspace, you need to create a Slack app and generate API credentials.

  1. Go to the Slack API website and log in with your Slack credentials.
  2.  Click on “Create an App” and provide a name and the Slack workspace where you want to use the app.
  3. In the app’s dashboard, navigate to “Incoming Webhooks” under “Features,” and toggle the switch to enable it.
  4. Scroll down and click on “Add New Webhook to Workspace.” Choose the channel where you want the notifications to appear, and Slack will generate an API URL for you. Save this URL for later use.

Step 2: Create GitHub Actions Workflow

Now, let’s create a GitHub Actions workflow that triggers Slack notifications when specific events occur in your repository.

  1.  In your GitHub repository, create a new directory named `.github/workflows` if it doesn’t exist.
  2. Inside the `.github/workflows` directory, create a new YAML file, e.g., `slack_notifications.yml`. This file will define the workflow.

In this YAML file, we define a workflow called “Slack Notifications” that triggers on pushes to the main branch and pull requests. The workflow consists of a single job, “notify_slack,” which runs on an Ubuntu environment.

Step 3: Create the Slack Notification Script

Next, we need a script that sends notifications to Slack. For simplicity, let’s use Node.js and the popular `axios` library to make HTTP requests to the Slack API.

  • In the root directory of your repository, create a new file named `sendSlackNotification.js`

This script reads the required environment variables set by the GitHub Actions workflow and sends a notification to the Slack channel based on whether it’s a commit push or a pull request.

Step 4: Set GitHub Secrets

To keep your Slack webhook URL secure, you should use GitHub Secrets to store sensitive information. Here’s how:

  1. In your GitHub repository, navigate to “Settings” > “Secrets” > “New Repository Secret.”
  2. Name the secret `SLACK_WEBHOOK_URL` and paste the Slack API URL you generated earlier.

Another way to trigger Slack Notifications using Github workflows action

Instead of making API call to trigger Slack notification, we can add the following script in our Github workflows file directly

There are multiple input parameters that can be added under `with` of the above script.

webhook-url (optional)
Only required if the SLACK_WEBHOOK_URL environment variable is not set.

status (required)
The status must be defined. It can either be the current job status using:

or a hardcoded custom status such as “starting” or “in progress”:
status: in progress

steps (optional)
The individual status of job steps can be included in the Slack message using:

channel (optional)
To override the channel or to send the Slack message to an individual use:

Step 5: Commit and Test

Commit the changes to your repository, and GitHub Actions will automatically pick up the new workflow. Whenever a push to the main branch or a pull request is created, the workflow will be triggered, and a Slack notification will be sent to the specified channel.

Conclusion

That’s it! Now you have a Slack trigger script integrated into your GitHub Actions workflow, enabling you to receive real-time notifications when specific events occur in your CI/CD pipeline. Feel free to customize the script and workflow to suit your specific use cases and team’s needs. Happy automating!

Subscribe for More

Tava is released in phases as we develop our full suite. Subscribe for release updates.

Get Ready to #DisruptEverything

Tava is free to get started. Build your custom application today.