Creating a Continuous Delivery Pipeline with Travis-CI, ContainerShip, and DockerHub
Docker is a popular containerization engine that has exploded in popularity over the last two years.
Docker makes it simple for dev teams to collaborate, and makes it significantly easier to test locally or in a staging environment, and remain confident that the same code will work in production.
A lot of our customers at ContainerShip talk about the struggles they are facing when trying to get a stable and functional Continuous Delivery pipeline running to test their code. They want to be able to test, build their app into a Docker container, and deploy it to a hosting environment; but the number of tools and systems they need to master to get things working is a little daunting.
At ContainerShip, we personally use Jenkins for testing and builds, but a lot of folks prefer to use other tools to get the job done. In this post we’re going to go over the necessary steps to integrate Travis-CI, a popular Continuous Integration & Continuous Delivery service, with ContainerShip Cloud.
By the end of the post, you should be able to merge a change on Github, have it trigger a build within Travis-CI, and finally have Travis-CI trigger a webhook to deploy your newly built Docker image to a ContainerShip cluster.
Prerequisites
For this tutorial, you will need:
Accounts on the following services:
A running ContainerShip cluster:
- Create a ContainerShip cluster on your favorite hosting provider, it takes only minutes using ContainerShip Cloud, and you can sign in with Github.
This demo will make use of the following Github and DockerHub repositories, feel free to fork them to follow along:
All set with account and cluster creation? Let’s keep it moving!
Travis-CI Setup
Configure Github repos
You’ll need to tell Travis-CI which git repos you want it to trigger builds for. We’re using a repo we created called containership/ci-demo.
Once you have linked your git repo to Travis-CI, it will show up in the left side navigation. We ran some previous builds of this repo, so your dashboard may look a little bit different.
The next step is important, you need to configure your general settings for the repo and set necessary environment variables that will be used to authenticate with DockerHub and ContainerShip Cloud.
Most of these are self explanatory, especially the ones prefixed with DOCKER_, these are your DockerHub username, password, and email address.
ORGANIZATION is your ContainerShip Cloud Organization that you either created manually, or was created for you automatically during signup. API_KEY is the access key associated with your ORGANIZATION.
To find your API_KEY, and ORGANIZATION visit this link: https://cloud.containership.io/#/settings/organizations
Click on the organization to get your API_KEY.
Travis-CI .travis.yml file
Your repo will need to contain a file called .travis.yml. The contents help Travis-CI figure out the language being used, and how to run your tests and install any dependencies. The most important part for this tutorial is the “after_success” section which instructs Travis-CI to build a docker image, push it to DockerHub, then make a request to ContainerShip Cloud to trigger the new deployment. You’ll notice the environment variables you configured previously being used in this file.
sudo: required language: node_js node_js:
— “0.10” services:
— docker before_install:
— npm install script:
— npm test after_success:
— docker login -e=”$DOCKER_EMAIL” -u=”$DOCKER_USERNAME” -p=”$DOCKER_PASSWORD”
— docker build -t “containership/continuous-integration-demo:$TRAVIS_COMMIT” .
— docker push containership/continuous-integration-demo:$TRAVIS_COMMIT
— curl -X POST “https://api.containership.io/v1/$ORGANIZATION/clusters/$CLUSTER_ID/proxy" -H “X-ContainerShip-Cloud-API-Key:$API_KEY” -H “X-ContainerShip-Cloud-Organization:$ORGANIZATION” -H “Content-Type:application/json” -d “{\”url\”:\”/applications/ci-demo\”,\”method\”:\”PUT\”,\”data\”:{\”image\”:\”containership/continuous-integration-demo:$TRAVIS_COMMIT\”}}”
ContainerShip Cloud Setup
Create the ci-demo application
You’ll need to create the application definition for your “ci-demo” app on your ContainerShip Cluster, using the existing tag you have for this image in DockerHub. If you aren’t using tags yet in DockerHub it’s ok. You will be once you finish this tutorial.
Configure the values you want your application to have for CPU, Memory, and Name, then click Create. If you’re following along using our image from DockerHub, you’ll want to specific 8080 for the Container Port value.
Now that the application definition has been created, you need to scale this app up to 1 container by visiting the Containers menu in the upper right navigation.
The container box is green in the UI which means that it has started and is healthy.
If you visit the DNS entry for our ci-demo application, you will see the tag you specified when starting it being outputted in our browser. Looks good!
Triggering Builds
At this point you should be able to make a commit to the master branch in your git repo and have Travis-CI run a build, push the resulting image to DockerHub, and then trigger a new deployment on ContainerShip.
I went ahead and added a random newline to the README.md in the ci-demo git repository, and can see a build has started in TravisCI.
The output will grow as dependencies are installed and the commands specified in .travis.yml are executed. At the end you should see your docker build, docker push, and the POST request to the ContainerShip Cloud API.
You should now have a new tag of this image in DockerHub.
And if you check our ContainerShip application, it should be up and running on the new revision.
The last thing you need to do is check the output of the app by visiting it in your browser to ensure it’s showing the right revision.
Success!
Want to learn more about containers, devops, and automation? Sign up for our newsletter for more similar posts.