Developer Community

How to create CircleCI workflows for Slack and Airtable

AUGUST 19, 2022 • 7 MIN READ
Written by
Esther Agbaje

As teams continue to supercharge their CI/CD pipelines using CircleCI, they need robust workflows that help them work more efficiently. If you've ever struggled with how to trigger multiple workflows from CircleCI to other applications, you've come to the right place.

With Buildable, you can establish an event-based connection from CircleCI and then set up an action to Slack channels to instantly notify teams about their build status (or other details). In this guide, we'll also take it a step further by creating a workflow that extracts details of events from CircleCI into an Airtable base.

To follow along, you need the following:

  • A Buildable account - if you don’t have an account, sign up for free here
  • A CircleCI workflow
  • A Slack workspace
  • An Airtable account

Without further ado, let's get into it!

Getting set up in Buildable

This workflow is going to be triggered by an event in CircleCI. So, you need to connect CircleCI as an event source in Buildable.

Add CircleCI as a connection

Inside the Buildable IDE, click on the Connections icon from the top right and create a new connection.

Select CircleCI as a source from the options.

Be sure you selected CircleCI as a source and not action.

Fill in the necessary credentials

Give the connection a name and set the environment as "Development.”

Also to enter your CircleCI Personal API key and Project ID.

💡 Tip: To find your CircleCI credentials, check out our docs.

Test your connection and once successful, click Connect.

Subscribe to events

Next, you’ll need to select the events you want to subscribe to. As at the time writing this article,  you can subscribe to “job-completed” or “workflow-completed” events.

For this demo, choose the “Select all” option and click Subscribe.

Create a workflow

In Buildable, Workflows are the series of actions that create a reliable flow of logic. Navigate to the “My Workflows” and create a new workflow.

Give the workflow a suitable name, description and hit Create.

Then, at the top, attach the job-completed event we subscribed to earlier.

Now that we've successfully created a connection with CircleCI and added an event trigger, we'll be back to complete this workflow by adding an action.

But for now, let's head over to Slack.

Creating a Slack app

We need to create an app in Slack and generate the API token that would be used in Buildable.

Set up a new app

First, navigate to https://api.slack.com/apps and click the Create new app button.

Then, select the create an app from scratch option.

Give your app a name, select the Slack workspace where you want to use it, and click Create App.

Granting bot permissions

Next, we need to set the app permissions. Select Permissions from the list and scroll down to Scopes.

Under Bot Token Scopes, click on Add an OAuth Scope and select chat:write and chat:write.public permissions from the list.

Now, scroll back to the top of the page and click ”Install to Workspace”.

Finally, grant permission to the bot to access your workspace.

In the next screen, you'd see the Bot User OAuth Token. Copy this token and get ready to add it to Buildable.

We are done with Slack. Let's go add an action in Buildable!

Adding a Slack action in Buildable

Back in Buildable, click the Add New Action button and select Slack.

Scroll down and from the list of action templates, choose the Sends A Message To a Channel action.

You’ll be prompted to connect to Slack. Click the Connect now button.

Give your connection a name e.g Slack connection.

Remember the Slack Access token you copied earlier? Paste it in the required field and connect.

Add the payload to the body

To get the shape of the payload event, head over to Event Stream and click the Current flow event stream tab. Here you'll see all the events related to your workflow.

Click the job-completed event.

Under the payload tab, you'll see the structure of the data. Copy the data by hitting the copy icon 📋. Click the “Body” tab, and paste the copied data.

Update the action template

Open up the “Sends a Message to a Channel” action template you recently added.

You'll notice the template is pre-filled with some JavaScript code for the action. The nodeInput function defines the input that should be passed to the action before it's executed.

💡Tip: $body represents the entire payload coming from the event. This implies we can target whatever value we need using $body.

We’ll cleanup the code to include only the properties we need for this action.

From the payload, let's send the following details to Slack as the value in text:

  • Job name → $body.job.name
  • Status → $body.job.status
  • Date it happened → $body.happened_at
  • By → $body.pipeline.vcs.commit.author.name

Here's the snippet of the code:

text: `Job: ${$body.job.name} \\nStatus: ${$body.job.status}\\nHappened: ${$body.happened_at}\\nby: ${$body.pipeline.vcs.commit.author.name}`

Save this action and run the entire workflow.

Trigger a job in CircleCI

Now, let's trigger a job to our CircleCI workflow pipeline.

Back in CircleCI, go to your workflow and re-run.

Open your Slack workspace and go to the channel you specified. You should see the notification.

Great! We've just completed the first part of this workflow. We also need to send details from this event to Airtable. So, let's set that up in the following step.

Creating an Airtable base

In your Airtable account, create a new base and give it a name e.g CircleCI

Rename "Table 1,"  to "CircleCI demo” and add the following columns with the field type set to Single Line Text.

  • Repo
  • Job
  • Status
  • Engineer

Next, in the last column, add "Happened At" and set the field type to date.

Ensure you toggle on "Include time".

Finally, grab your Airtable Credential API key and Base ID. You’d be needing them in Buildable shortly.

💡 Tip: To find your Airtable credentials, check out our docs.

Completing the connection in Buildable

Back in Buildable, let's add another action.

From the list of apps in the Action Library, select Airtable and then the Create Records template.

If you’ve not connected Airtable to Buildable, you’d be prompted to add a connection.

Give your connection a name, paste in your Airtable credentials and click Connect.

Inside the action template, change "Table 1" to "CircleCI demo" (the name we gave our table in Airtable).

Next, map the values in our fields to the corresponding columns on Airtable.

From our payload, we can match the values as follows:

Repo → $body.project.name

Job → $body.job.name

Status → $body.job.status

Engineer → $body.pipeline.vcs.commit.author.name

Happened At → $body.happened_at

Below is the code snippet you can copy and paste into your editor

fields: { Repo: $body.project.name, Job: $body.job.name, Status: $body.job.status, Engineer: $body.pipeline.vcs.commit.author.name, "Happened At": $body.happened_at },

Hit the Save All Button and run your workflow.

You should receive the notification in your specified Slack channel, as well as new records populated to the Airtable base.

What's next?

In this tutorial, you created a workflow to send notifications from CircleCI events to your Slack channel and add details of this event as records to an Airtable base using Buildable.

If you wish to extend the logic of your workflows, you can add custom JavaScript functions from the action library.

Have questions? Join our Discord community, or reach out to us on Twitter.