Travis CI is a continuous integration (CI) platform that runs your automated code tests and triggers specific actions based on the results. This article walks through how to use Nanobox and Travis CI to test your code and automate deployment.
Travis CI allows you to build some really flexible and powerful integration and deployment workflows. This article covers the basics of using Travis CI and Nanobox together:
- Configuring a codebase to use Travis CI with Nanobox
- Running tests with Nanobox
- Automating deployments with Nanobox and Travis CI
Nanobox is a "micro-platform" that builds and runs your application anywhere – your local machine, a CI server, or in production – using settings defined in your
Since this article is meant to focus on using Nanobox with Travis CI, I don't want to get into the nitty-gritty of configuring your app for Nanobox. If you're just getting started with Nanobox, the Getting Started Guides will walk you through it.
This walk-through uses my Sample Phoenix App, which includes all the configuration outlined in this article.
One of the reasons I chose Elixir/Phoenix is because of a handy testing utility built into Mix that makes writing and running tests really easy (a subject for another time ? ).
All I need to do to run the app's tests is:
I'll use this to test my code before deploying to my live app.
Create an App On Nanobox
In order to automate the deployment of an app with Nanobox, you must first create an app with Nanobox. The process of creating an app is documented in the Launch an App doc.
Set Up Travis CI
Travis CI integrates directly with Github and can run tests for any of your repos hosted there. When you setup your account on travis-ci.org (for open-source projects) or travis-ci.com (paid plans for private repos and additional features), you will grant Travis access to your Github repos.
Enable Travis CI on Your Repo
To have Travis CI run tests, you need to enable Travis CI on your repo. Go to your Travis CI profile and enable the repo you'd like to use with Travis.
Automate Nanobox Login with Environment Variables
When using Nanobox to deploy to a live app, you'll have to authenticate with your Nanobox account. This is typically done with an interactive prompt, but you can automate this process by adding
NANOBOX_PASSWORD environment variables to your project on Travis CI.
Go to the settings page for your project on Travis CI.
NANOBOX_PASSWORD environment variables. These should be the username and password of the Nanobox account you'll use to deploy to your live app.
Travis CI builds and runs your test environment using settings defined in your
.travis.yml. Go ahead and create a file named
.travis.yml in the root of your project.
Use the Nanobox CI Bootstrap Script
install node in the
.travis.yml allows you to run commands to prepare the environment for testing. Nanobox provides a CI bootstrap script that will download, install, and configure Nanobox on your CI server. Add the following to your
install: sudo bash -c "$(curl -fsSL https://s3.amazonaws.com/tools.nanobox.io/bootstrap/ci.sh)"
Set the Linux Distribution to "Trusty"
Ubuntu 14.04+ (Trusty Tahr) is required to install Docker, which Nanobox uses to provision containers for each of your app's components.
The Nanobox CI Bootstrap script requires
sudo privileges to install binaries on the system $PATH. Enabling
sudo permissions with Travis is simple.
Configure Your Tests
This is where the Travis CI really shines. The
script node of the
.travis.yml tells Travis what commands to execute in order to run your app's tests.
We want to both build our app and run our tests with Nanobox. This is done using the
nanobox run test-command. In the case of the Phoenix sample app, it will be
nanobox run mix test.
script: nanobox run mix test
The command to run your tests will be unique to your codebase, but the principle is the same.
nanobox run test-command.
Based on the success or failure of your tests, Travis CI will trigger certain actions. The
after_success node of the
.travis.yml allows you to define what actions are triggered when all tests pass.
To automatically deploy to your Nanobox app, you must first add the app as a remote, then deploy. Be sure to replace
app-name in the code below with the name of your app deployed with Nanobox.
after_success: - nanobox remote add app-name - nanobox deploy
The Final .travis.yml
With everything in place, the
.travis.yml should look something like this:
dist: trusty sudo: required install: sudo bash -c "$(curl -fsSL https://s3.amazonaws.com/tools.nanobox.io/bootstrap/ci.sh)" script: nanobox run mix test after_success: - nanobox remote add app-name - nanobox deploy
That's It! Push to Your Repo
Now when you push new commits to your Github repo, they will automatically trigger a build/test with Travis CI. You can view the output of the build process and tests in your Travis CI dashboard. When the tests pass, the updated code will be deployed to Nanobox.
Subscribe to Nanobox
Get the latest posts delivered right to your inbox