A Travisfile — or the .travis.yaml file — is the primary tool used to set up the Travis CI platform. In this post, we introduce the Travisfile and break down the different options the .travis.yml file provides at a high level.
What is Travis CI?
Travis CI lets us automate the process of code testing, integration, delivery, and deployment, allowing us to bring DevOps and Agile principles into our code practices from push to production deployment.
Fully-hosted and ready to use at only a few clicks of a button, Travis CI ties into your existing version control system and gets you ready to automate build testing and more with minimal up-front effort — gone are the days of having to supply your own testing infra!
Accelerate your career
What is a Travisfile?
Travis CI ties in with our existing version control so we can automatically test and deploy our code. But for Travis to work without our desired specifications, we need to supply it a single file: The
.travis.yml file — also known as the “Travisfile”.
The Travisfile contains all the instructions we need for managing the testing and deployment of our code.
The information we supply this file will serve one of two purposes:
- It can be a fact about the code — such as programming language, version, or needed services
- Or part of actual build phase — specific instructions for building, testing, and deploying the code
At the very minimum, we need to tell Travis what language we’re using. In fact, the following piece of code may well work as a successful Travisfile:
Once Travis knows which language to use, it can assume the build environment, language version, install script, build script, and additional jobs. These are the basic “facts” of our build.
|The build environment; this is the environment that the code is deployed on|
|The language the code is written in|
|Specify the language name as the key and the version is the value to supply a specific version|
|Enable a service on the build environment, such as Docker or MongoDB|
|Installation steps to prepare the build environment|
|Steps to build the application|
|Additional parameters for parallel builds and stages|
script are also phases, and phases are the steps Travis takes to set up, build, test, and potentially deploy the application.
There are twelve build phases, with
script being the only mandatory ones — although these are often set at the language-level and often do not need to be manually supplied.
That said, we can supply our own commands to every and each phase, if desired:
|Add apt sources to install packages|
|Install components needed to cache build, in caching is enabled|
|Commands run before install phase|
|Commands to run before application build|
|Commands to run before caching build|
|Commands to run when the build succeeds or fails|
|Commands to run prior to deploying code|
|Method of deploying the code|
|Commands to run after a successful deploy|
|Final commands to run prior to finishing the Travis run|
For example, a more complex Travisfile might look like:
env: focal language: node_js node_js: 'lts/*' services: rabbitmq before_install: ./init.sh install: ./install.sh after_success: ./package.sh jobs: include: - stage: docker after_success: docker push "$DOCKER_USERNAME"/app if: branch = docker deploy: provider: releases api_key: "$GITHUB_TOKEN" file: app.tar.gz skip_cleanup: true on: branch: main
This sets the build environment, language and version, needed service, as well as runs two custom scripts (once before the install and one replacing the default Node.js install command), before setting up a method of deployment to GitHub releases.
It also leverages some useful functions for building our file, such as defining the desired branch, and provides an alternative job if the branch being worked from is `docker`.
Travis CI is a convenient platform for easily testing, integrating, delivering, and deploying our code, and with a good knowledge of the needed
.travis.yml file, we’re well on our way to setting up Travis with any and all our projects as desired.
Learn more about Travis CI
Looking to learn more about Travis CI? Check out my new course Building a Continuous Integration Pipeline with Travis CI.
This course gets you started with Travis CI by breaking down the
.travis.yaml file. By breaking down this file, we’ll use its structure to learn the different build phases Travis CI takes when integrating our application. With knowledge of these phases, we can then optimize our Travisfile to perform a number of jobs, test against a number of parameters, work in both parallel and in stages, and deploy to as many endpoints as we need.
Keep in touch by following A Cloud Guru on Twitter and Facebook, subscribe to A Cloud Guru on YouTube for weekly updates, and join the conversation on Discord. You can also check out our current crop of free courses!