Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Running Daily Builds on CircleCI

DZone's Guide to

Running Daily Builds on CircleCI

How to run a build every day using CircleCI, then push it to GitHub and notify teammates on Slack.

· DevOps Zone
Free Resource

Download “The DevOps Journey - From Waterfall to Continuous Delivery” to learn learn about the importance of integrating automated testing into the DevOps workflow, brought to you in partnership with Sauce Labs.

This post details how to automate running a build daily on CircleCI.

A few weeks ago, I set up one such build to check that some Rails dependencies install properly from scratch. You might want to do something similar to check the status of a build over time. To match this example, you will need a separate unix or linux server running cron to kick off the builds each day.

How

Create a commit in your repository that you want to run regularly, then push it to Github. I made a branch in a simple Rails app called “unpinned_dependencies”. Its purpose is to check that the latest stable versions of the following gems will install correctly:

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug'
  gem 'rspec-rails'
  gem 'capybara-webkit'
  gem 'rspec_junit_formatter'
end

Generate a Token

Project Settings -> Permissions/API Permissions -> Create a token with a label
This will need the “All” permission to trigger builds:

image01.png

I recommend putting this token in the project UI, under environment variables. Your token will only be on CircleCI. Compare this to storing a token in a commit, which will be on your dev machines, on Github, CI and your production machines. For this example, it’s stored as CIRCLE_REPO_TOKEN.

image02.png

Figure out the right CircleCI API endpoint for your build, and put it in a script. 

# /bin/sh

curl -X DELETE \
      --header "Content-Type: application/json" \
      -d "{}" \
      “https://circleci.com/api/v1/project/<username>/<project>/build-cache?circle-token=$CIRCLE_REPO_TOKEN”


curl -X POST \
      --header "Content-Type: application/json" \
      -d "{}" \
      "https://circleci.com/api/v1/project/<username>/<project_name>/tree/<branch_name>?circle-token=$CIRCLE_REPO_TOKEN"

Note the first delete request. For the purposes of this project, I want to make sure all dependencies can install from scratch, so I’m first deleting the project cache. This is a crude approach, as the clear-cache API removes the cache for all branches. If this repo is shared with anyone else, this will affect their builds on other branches, slowing them down once a day.

A more fine-grained approach would be to use circle.yml delete cache folders if the branch matches a certain name. You could also make a separate repo just for running regular tests. I’ve done that with this example, to demonstrate the simplest possible use-case.

Automating the Build

I use cron on an Ubuntu box for this example. I simply copied run-daily-build.sh into /etc/cron.daily/. This doesn’t give me control over what time of day the script runs, which is okay for this build. If you want more granularity, you can create an entry in crontab to kick off run-daily-build.sh.

Slack Integration on CircleCI to Notify Results

Visit “Project Settings” -> “Notifications/Chat Notifications” -> “Slack Integration”

image03.png

Click through the “CircleCI Integration” link to login and choose a channel or person to message. I’ve used this step for both notifying a room and notifying myself, depending on what I’m exploring.

That’s pretty much it. You can test this whole flow by manually executing the script once. If you see a slack message like the following, then you’re good to go.

This was a relatively simple example, but after I’ve gone through this once, it’s given me ideas for other ways to extend this behavior. Perhaps in a future post I’ll write about building on top of these tools to find flakey errors. If you have any tips or tricks on how you maximize CircleCI for your team, let us know about it on Discuss.

Discover how to optimize your DevOps workflows with our cloud-based automated testing infrastructure, brought to you in partnership with Sauce Labs

Topics:
circleci ,continuous integration ,automation ,github ,slack ,rails

Published at DZone with permission of Eric Hu, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}