Slacking Test Notifications

Wercker is a Docker-Native CI/CD Automation platform for Kubernetes & Microservice Deployments

Zachary Flower
Zachary Flower
December 23, 2017

When it comes to flexibility and ease-of-use in a CI/CD platform, I'm a huge fan of Wercker Steps. For the uninitiated, Wercker Steps "are self-contained bash scripts or compiled binaries for accomplishing specific automation tasks, defined in the wercker.yml file of your application. Steps can be written manually or borrowed from the community via the Steps Registry."

The beauty of Wercker Steps is that anything you can do in a standard Linux environment can also be done in a Wercker Step. Steps can be dropped in at any point in the wercker.yml file, allowing you to quickly and efficiently add new features to your build process.

Let's say, for example, that you have a complicated test process that spans multiple languages, and you would like to send a completion report to your team's Slack channel after every step. While Wercker offers Slack notifications out of the box, adding a notification for each step in a build process isn't so simple. So, how should we go about accomplishing this?

Keep reading to find out how to solve a challenge like this using Wercker Steps.

 

Getting Started

First, let's take a look at a sample wercker.yml that has five separate steps.

 

 box: ubuntu
# Build definition
build:
# The steps that will be executed on build
steps:
- script:
name: step one
code: echo "Step One
- script:
name: step two
code: echo "Step Two"
- script:
name: step three
code: echo "Step Three"
- script:
name: step four
code: echo "Step Four"
- script:
name: step five
code: echo "Step Five"

 

To state the obvious, the steps above don't actually do anything, but for the sake of argument let's pretend that each one of them is doing something impressive and complicated. Before we add Slack notifications to our wercker.yml file, let's first take a look at how to send a basic Slack notification.

 

Slack Notifications

To borrow from Slack's API documentation, sending a notification to Slack is as simple as making a basic POST request using curl. With a registered webhook, the simplest request you can make to accomplish this contains only a message; all other values—name, avatar, etc.— fallback to defaults.

 

curl -X POST -H 'Content-type: application/json' --data '{"text":"This is a line of text.\nAnd this is another one."}' https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX 

 

To break it down, this command will send a POST request to a webhook URL—the https://hooks.slack.com part, with a basic two-line message. While the channel can be sent along within the JSON payload, it defaults to the one selected when creating the webhook.

With this information in mind, let's put it in the context of a Wercker Step. Since Wercker Steps can run raw BASH commands, the same command seen above can be put directly within a script step:

 

- install-packages:
packages: curl
- script:
name: send basic slack notification
code: |
curl -X POST -H 'Content-type: application/json' --data '{"text":"This is a line of text.\nAnd this is another one."}' https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX

 

It is important to note that, because there is a colon in our curl request, we must escape it by adding a pipe directly after code: directive. This tells Wercker to treat the following code as a multi-line literal block, which allows it to run cleanly without any issues.

Additionally, because our sample wercker.yml file is using a bare-minimum Ubuntu container, we also have to tell Wercker to install the curl command on it in order to successfully use it. While this can get ugly pretty quickly, this flexibility allows us to easily script pretty much anything within our Wercker build process without getting bogged down in syntax.

Another important thing to be aware of is that putting the raw Slack webhook URL directly within our wercker.yml file has high potential for abuse and insecurity. To combat this, Wercker has made it easy to define secure environment variables that can be accessed within the build process, but can only be seen or changed by users with the proper permissions.

 

Image 2017-05-09 at 6.27.08 pm.pngWercker Slack Webhook URL 

 

Putting It All Together

So, now that we know how to send custom Slack notifications, how do we work this into our build process to keep us in the loop every step of the way? Believe it or not, because of the flexibility of Wercker Steps, we can simply drop in the Slack notification script anywhere in our build steps process.

 

box: ubuntu
# Build definition
build:
# The steps that will be executed on build
steps:
- install-packages:
packages: curl
- script:
name: step one
code: echo "Step One"
- script:
name: step one complete
code: |
curl -X POST --data-urlencode 'payload={"text": "Build Step One Complete (20% done)"}' $SLACK_WEBHOOK_URL
- script:
name: step two
code: echo "Step Two"
- script:
name: step two complete
code: |
 curl -X POST --data-urlencode 'payload={"text": "Build Step Two Complete (40% done)"}' $SLACK_WEBHOOK_URL
- script:
name: step three
code: echo "Step Three"
- script:
name: step three complete
code: |
curl -X POST --data-urlencode 'payload={"text": "Build Step Three Complete (60% done)"}' $SLACK_WEBHOOK_URL
- script:
name: step four
code: echo "Step Four"
- script:
name: step four complete
code: |
curl -X POST --data-urlencode 'payload={"text": "Build Step Four Complete (80% done)"}' $SLACK_WEBHOOK_URL
- script:
name: step five
code: echo "Step Five"
- script:
name: step five complete
code: |
curl -X POST --data-urlencode 'payload={"text": "Build Step Five Complete (100% done)"}' $SLACK_WEBHOOK_URL 

 

As you can see above, all it takes to send Slack notifications for every step in our build process is to add the requisite steps in the right places—which, in this case, is after every official step. All that differs is the message we are sending to Slack.

 

Image 2017-05-15 at 4.46.37 pm.png

Custom Wercker Step Slack Notifications

 

Next Steps

While the above setup is a great example of what can be accomplished with Wercker Steps, you might be wondering how to make it cleaner or easier. While outside the scope of this document, Wercker has made it incredibly easy to create and share steps with other users within the Wercker ecosystem. No matter how you use or share them, though, Wercker Steps are an excellent way to take a good CI/CD workflow and turn it into a great one.

 

About the Author

Zachary Flower is a freelance web developer, writer, and polymath. He has an eye for simplicity and usability, and strives to build products with both the end user and business goals in mind. From building projects for the NSA to creating features for companies like Name.com and Buffer, Zach has always taken a strong stand against needlessly reinventing the wheel, often advocating for the use of well established third-party and open source services and solutions to improve the efficiency and reliability of a development project.

 

Like Wercker?

We’re hiring! Check out the careers page for open positions in Amsterdam, London and San Francisco.

As usual, if you want to stay in the loop follow us on twitter @wercker or hop on our public slack channel. If it’s your first time using Wercker, be sure to tweet out your #greenbuilds, and we’ll send you some swag!

 

Topics: Tutorials