Will & Skill Developers

Will & Skill Developers

Thoughts, snippets and ideas from the team at Will & Skill AB, Stockholm.

Erik Svedin


W&S devops part 2.5 - Testing a django project using drone.io

Erik SvedinErik Svedin

In the previous post we installed and integrated an instance of drone.io with our github account. Now its time to actually test a real project and send notifications about the build status to a Slack channel.

The project I will be using for this tutorial can be found here. It is a super simple project which has a single view and both a unit test as well as a functional test to go with it.

Grab the code and try it out locally:

$ python manage.py test

Instructing drone how to build, test and notify slack about your project

First of you should have the the code under your github account. The easiest way to get started is to fork the above repo at the 1.1 tag. Next thing is to log in to your drone instance which you created and activate this repo. You should now be at the dashboard view looking something like this:

Repo dashboard

What we need to do now is adding a .drone.yml to instruct drone how to actually build and test our project. This file should contain info of what docker image to use when the specific project is built, it can also contain a wide array of other options such as: notification channels (emailing, slack, hipchat), automatic deployment steps etc. Go check out the getting started overview.

For now however we will focus on the following:
1. Actually testing our code
2. Sending notifications to Slack

Preparing slack for incoming webhooks

If your team is using another channel of communication you can skip this part

So go ahead and create a new channel. At the top there should be a link saying "+ Add an app or custom integration". If you're not taken there automatically be sure to click "Configure" in the top right corner and go in to the "Custom integrations" tab. Select "Incoming webhooks" and click "add configuration". Here let's select our newly created channel, I called it "drone-channel".

slack integration step

When you click on the Add incomming WebHooks button you will get a WebHook URL. This is what we want in our .drone.yml file to be able to communicate with slack.

Creating the .drone.yml file

In the root of our project create the .drone.yml and fill it with the following:

  image: bradrydzewski/python:2.7
    - sudo pip install -r requirements.txt
    - xvfb-run python manage.py test
    webhook_url: 'YOUR_WEBHOOK_URL'
    username: 'drone'
    channel: '#drone-channel'
    on_started: false
    on_success: true
    on_failure: true

This uses one of the official Drone Docker build images: bradrydzewski/python:2.7 to build and test our code. Directly from the documentation:

Drone also provides official build images. These images are configured specifically for CI and have many common software packages pre-installed (git, xvfb, firefox, libsqlite, etc).

This is great since we got functional testing using Selenium, which needs to be run with a virtual display (your server doesn't have a monitor). Hence the xvfb-run before the python commmand.

The notify section makes use of the Slack notify plugin which comes with drone. This section can be replaced (or extended) with other channels including emails etc. Check out the readme on drone notifications.

Starting our first build on drone

Now for the moment of truth! Commit .drone.yml and push it to your repo!

In your projects panel on your drone server you should now see your build starting:
Executing build

Go to the details page of the current build to see it in action:
Build process

This view is great since you can see all the steps in your build process, which makes it pretty easy to debug. After a while your build will either succeed or fail, and you should now receive your Slack notification as instructed:

Drone slack notification

That's it for today ladies and gentlemen! You can now go in to a pushing frenzy. Always confident that there is a drone hovering above you, to keep you and your code in check!

Erik Svedin

Erik Svedin