Looking for a free and easy way to build your open source projects? Travis CI enables you to build, test and deploy your code in minutes! In this post we will setup a Travis CI build to get you going quickly.

For a complete overview of the functionality of Travis read the docs here.

We will start off with a project in Micronaut (read more about Micronaut here) where we saw how to deploy it to Heroku in this post.

Setup Travis

First of all go to travis-ci.org and sign up/in to travis with your github account. Note that you have to authorise Travis to do some stuff like doing commits after a build.

authorize screen for travis ci 747x1024 - TRAVIS CI

Now navigate to ‘Settings’ under your account icon in the top right. Here you can enable repositories for Travis to build. Enable your project!

opt in list of github repos 1024x610 - TRAVIS CI

Travis build

Travis looks for a file .travis.yaml in the root directory of your repo. Here we build with Gradle so following some basic tutorials (here for example) we see the following result of the .travis.yaml

sudo: required

jdk:
  - openjdk13

script:
  - ./gradlew build

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

before_cache:
  - rm -f  $HOME/.gradle/caches/modules-2/modules-2.lock
  - rm -fr $HOME/.gradle/caches/*/plugin-resolution/

Push this to github and travis will automatically build this branch. Go to the travis dashboard and see the progress.

travis ci builds overview 1024x498 - TRAVIS CI

The build is a success! It ran for 1 min 41 sec and for the logs you can scroll down. If we would now create a pull request in github Travis triggers a PR build i.e. first merging the source and upstream branches before building the result. In github the status is shown.

travis ci checks on github branch 1024x485 - TRAVIS CI

Travis push image

After a successful build we want to push the Docker image to a registry. We will use the Heroku container registry to push our Docker image. We need to login, install the plugin, build en push subsequently.

...

install:
  - wget -qO- https://toolbelt.heroku.com/install.sh | sh
  - docker login --username _ --password=$HEROKU_API_KEY registry.heroku.com
  - heroku plugins:install @heroku-cli/plugin-container-registry

script:
  - ./gradlew stage
  - heroku container:push web --app gardenplanner-api

Note that we didn’t define the environment variable HEROKU_API_KEY. We can store this securely in travis if we select our build project and go to ‘More options‘ > ‘Settings‘. Add variable with name HEROKU_API_KEY and your Heroku api key as the value.

environment variables screen 1024x274 - TRAVIS CI

Push the code and your image is build en added to the Heroku container registry!

Travis deploy

With a successful build on master we would like to deploy our app to Heroku. (Read more about how to deploy manually to Heroku in this post.) Add the deploy step to your travis file as follows.

deploy:
  provider: script
  script:
    heroku container:release web --app gardenplanner-api

With this step in place it is time to merge your pull request to master. This will trigger a build with the deploy step! We can check the deployment activity in Heroku.

heroku overview of running apps 1024x259 - TRAVIS CI

And of course check that your app is available.

call to app running on heroku 1024x477 - TRAVIS CI

Nice! Travis deployed our app automatically when merged to master.

Hopefully you are now able to build and deploy your own application with Travis to automate your pipeline. Happy Travising!