DEV Community

Cover image for Create a SlackBot using GitHub Actions + Light AI
Light AI
Light AI

Posted on • Updated on

Create a SlackBot using GitHub Actions + Light AI

What is a Github Action?

The Official Github Definition of an Action is:

GitHub Actions is a continuous integration and continuous delivery (CI/CD) platform that allows you to automate your build, test, and deployment pipeline. You can create workflows that build and test every pull request to your repository, or deploy merged pull requests to production.

Creating a SlackBot for Github

We are going to create a SlackBot for Github, This SlackBot is going to send us a message in to our slack channel every time a pull request is made.

Understanding Our Workflow

Let's break down everything that is happening in the GitHub Actions workflow

Configuring our Github Action

Create a New Project or go to an Existing Project

Click Add File and then Click Create New File

Now let's add our Workflow, Copy and Paste the Following:

.github/workflows/slackbot.yml

Creating our Workflow

To generate this workflow I am going to use the Light AI Beta, to generate the GitHub Action, this will save me some time.

Light AI Beta Preview

I'll Select Github Actions in Light Docs and type Create a GitHub Actions that sends a slack notification on a pull request

Light Docs Output

We will copy and paste the output from the Light Docs™ into our workflow. To better understand what is happening I will take the time to breakdown the workflow

name: SlackBot for Pull Requests 

  push:
    branches:
      - main
  pull_request:
    branches: [ "main" ]
Enter fullscreen mode Exit fullscreen mode

Here we are giving a name for the Github Action and Specifying the Branch for our PR

Adding Jobs and Builds

A Workflow can be made up of many jobs-in this case we are only using a single job called Build.

jobs:
 build:
  runs-on:ubuntu-latest
Enter fullscreen mode Exit fullscreen mode

Steps and Checkout

Here we just add one step, which checkout's our code.

steps:
 - name: Check out Code 
   uses: actions/checkout@v2
Enter fullscreen mode Exit fullscreen mode

Adding your Slack API Key + Workflow

Now you will need a slack account for this last step to work. This part can be a little tricky.

 -name: Slack Notifcation 
    uses: rtCamp/action-slack-notify@master
    env:
      SLACK_WEBHOOK_URL:${{secrets.SLACK_WEBHOOK_URL}}
      MESSAGE: "A Pull Request was Made!!"
Enter fullscreen mode Exit fullscreen mode

Here is the complete Github Action so your .yaml is written correctly.

name: Slack Notification on Pull Request

# Controls when the workflow will run
on:
  # Triggers the workflow on pull request events but only for the "main" branch
  push:
    branches:
      - main
  pull_request:
    branches: [ "main" ]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
    # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
    - name: Check out code
      uses: actions/checkout@v2

    # Sends a Slack notification
    - name: Slack Notification
      uses: rtCamp/action-slack-notify@master
      env:
        SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK}}
        MESSAGE: "A new pull request was made!"

Enter fullscreen mode Exit fullscreen mode

Now that we have this Workflow, if we commit this we will end up with a failed build. We will fix this in the next section.

Create a Github Secret and Install WebHooks

Now we need to create a Github Secret so that we aren't exposing our Slack WebHook.

Let's go to our repo and click settings

You will need to click create a new repository secret

Name the Secret SLACK_WEBHOOK

Go to your slackurl/apps. In this instance it will be light.slack.com/apps

Slack App Directory

Now go down to the search bar and type Incoming WebHooks

Incoming WebHooks

Click Add to Slack and select your Workspace

Add to Slack

Click Add Incoming WebHook Integration You will be directed to a page where you will see your WebHook URL.Copy and paste that URL and go back to your Github Repo

The URL will be towards the bottom and look something like this

WebHook URL

Click Settings and then go down and click Secrets and then Actions

GitHub Actions

Click New Repository Secret and then add the WebHook URL you just copied

It should look like this

New Repo Secret

Now let's clone our repo and open it in visual studio code

First let's pull our latest changes git pull, Then let's create a new branch so we can test our pull request feature
git checkout -b slack-branch

Now let's use an empty commit to trigger the Github Action
git commit --allow-empty -m "dev: empty commit for testing"

We should see your Github Action running and Completed. Once complete go to your Slack and make sure you see the SlackBot notification on the channel you selected earlier in the setup process

Slack Notification

Now we need to update the Workflow because it's currently setup to only work on a push and a pull and we only want this to work on pull requests so we are notified when someone on our team has made a PR

This is the updated flow without the push trigger.

name: Slack Notification on Pull Request

# Controls when the workflow will run
on:
  # Triggers the workflow on pull request events but only for the "main" branch
  pull_request:
    branches: [ "main" ]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
    # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
    - name: Check out code
      uses: actions/checkout@v2

    # Sends a Slack notification
    - name: Slack Notification
      uses: rtCamp/action-slack-notify@master
      env:
        SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK}}
        MESSAGE: "A new pull request was made!"

Enter fullscreen mode Exit fullscreen mode

On your next GitHub commit you will see a Pull Request. Click create a pull request or new pull request. Click Commit Changes.

You Should now see a slack notification!
New Pull Request-Github

Congratulations you just created a SlackBot using GitHub Actions + Light AI

Credit:

Github Actions Output Generated by LightAI

Request Early Access

If you have any questions/thoughts for us, please reach out at hello@lightaibeta.com.

Top comments (0)