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.
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.
Now navigate to ‘Settings’ under your account icon in the top right. Here you can enable repositories for Travis to build. Enable your project!
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.
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 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.
Push the code and your image is build en added to the Heroku container registry!
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.
And of course check that your app is available.
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!