Anatomy of a Travis CI File
A Travisfile (or the .travis.yaml file) is the primary tool used to set up the Travis CI platform. Get a break down of the Travisfile and its options.
Jun 08, 2023 • 5 Minute Read
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?
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:
language: node_js
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.
Parameter | |
env | The build environment; this is the environment that the code is deployed on |
language | The language the code is written in |
[language_name] | Specify the language name as the key and the version is the value to supply a specific version |
services | Enable a service on the build environment, such as Docker or MongoDB |
install | Installation steps to prepare the build environment |
script | Steps to build the application |
jobs.include | Additional parameters for parallel builds and stages |
install
and 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 install
and 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:
Parameter/Phase | |
apt.addons | Add apt sources to install packages |
cache.components | Install components needed to cache build, in caching is enabled |
before_install | Commands run before install phase |
install | Installation commands |
before_script | Commands to run before application build |
script | Build script |
before_cache | Commands to run before caching build |
after_success/after_failure | Commands to run when the build succeeds or fails |
before_deploy | Commands to run prior to deploying code |
deploy | Method of deploying the code |
after_deploy | Commands to run after a successful deploy |
after_script | 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.
Looking to level up your tech skills? Check out this month’s free courses (no credit card required!), including YAML Essentials, GitHub Actions Deep Dive, and Introduction to Python Development.
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!