Skip to content

Continues Integration with Travis

Continuous Integration

Continuous Integration (CI) is a software development practice where developers integrate code into a shared repository frequently, ideally several times a day! Among the benefits of this practice is that you can detect errors quickly and resolve them more easily.

Build Pipeline

A build pipeline is a set of steps that move code from development to production.

In modern software development, the build pipeline is often automated. That means, for instance, when you commit and push your code into the repository, a cascade of actions will be triggered that include "compiling the code," "executing tests," etc., which eventually result in deploying (software) artifacts. Automated build pipelines are great for implementing continuous integration workflows for software.

In this reading, we'll look at building a simple build pipeline using Travis CI.

Travis CI

Travis CI is a cloud-based continuous integration tool that seamlessly integrates with any GitHub repository, supports every major programming language, and deploys to multiple different cloud platforms (including Heroku).

Travis CI has a free and a paid version. The free version, denoted by the .org domain name, offers full capabilities for any public GitHub repository. The paid version, which uses the .com domain name, is required for private GitHub repositories.

Creating a Build Pipeline with Travis CI

Travis CI works (by default) by monitoring a GitHub repository for new commits. When a new commit is made, it executes the steps of the build pipeline as defined in a configuration file. If any step fails, the pipeline terminates and it will notify us.

Let's create a build pipeline:

  • Create a public repository on GitHub.

  • Create a simple Gradle-Java project in IntelliJ.

  • Create a (local) Git repository to track the Gradle-Java project you've created in the previous step and add the public GitHub repository (which you've created in the first step) as the remote origin to your local Git repository.

  • Go to https://travis-ci.org/ and log into Travis CI with your GitHub account; authorize it to access your repositories.

  • By default, none of your repositories are considered "active." You need to enable the repository you've created earlier from the account settings page.

In your Account settings, you will see a list all of your public GitHub repositories, along with a toggle button. Clicking the toggle button will configure Travis CI to start monitoring that repository for new commits, using the default branch of master.

Note that each repository also has a Settings button. This is where you can configure e.g. which events trigger the pipeline (pushes, pull requests, and so on). For now, we will stick to the default settings.

  • In IntelliJ, create a new file at the root of your project directory and name it .travis.yml. You need to provide in this file the information required to configure a build pipeline. Without this file, Travis CI will not do anything for your repository!

  • You can configure a build pipeline using Travis CI with as little information as only specifying the programming language. So, add the following to .travis.yml:

1
2
language: java
jdk: openjdk8
  • When your project contains a build.gradle file in the repository root, Travis CI automatically builds your project with Gradle.

  • To optimize performance, we need to tell Travis CI explicitly that we want to store and use the Gradle cache and Wrapper for successive invocations of the build. So, add the following lines to .travis.yml:

1
2
3
4
5
6
7
8
before_cache:
  - rm -f  $HOME/.gradle/caches/modules-2/modules-2.lock
  - rm -fr $HOME/.gradle/caches/*/plugin-resolution/

cache:
  directories:
    - $HOME/.gradle/caches/
    - $HOME/.gradle/wrapper/

Make a simple "Hello world" Java application, commit and push the changes to the remote repository. Then, head over to your Travis CI dashboard; your app must have been built successfully.

From Travis CI to Heroku

Travis CI can automatically deploy your Heroku application after a successful build. To test this, please first update your Gradle-Java app to a simple Javalin app and deploy it to Heroku by following the instructions here.

Since we are not using Gradle-Heroku plugin to deploy the app, we must tell Heroku how to run our application after it was deployed by Travis CI. To this aim, you need to add a file named Procfile to the root of your application directory with the following content:

1
web: java -jar build/libs/javalinHerokuTravis-1.0-SNAPSHOT.jar

Note

The javalinHerokuTravis-1.0-SNAPSHOT.jar is the name of the sample which I've created but yours might be named differently.

Finally, add the following to travis.yml:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
deploy:
  provider: heroku
  api_key:
    secure: "YOUR ENCRYPTED API KEY"

deploy:
  provider: heroku
  api_key: "YOUR HEROKU API KEY"
  app: "YOUR HEROKU APP NAME"
  skip_cleanup: true

Heroku API Key and App name

You can login to Heroku and find your App name from dashboard. The API Key is available in your Account's settings page.

Danger

By placing the API Key inside the travis.yml and pushing it to a public GitHub repository, you will be exposing your key! This is a horrible practice!! You must instead use Travis and Heroku CLI to encrypt the key and add the secret encrypted key in there. Follow the instructions here.

Commit and push the changes. Travis CI must successfully build the app and deploy it to Heroku. To make sure it seamlessly works, try to change the message on the homepage from Hello Heroku! to something like Hello Heroku from Travis CI!; commit and push the changes, and sit back and observe Travis to build and deploy your app!

The sample code for this reading can be found here.