4

Anatomy of a Travis CI File

 2 years ago
source link: https://acloudguru.com/blog/engineering/anatomy-of-a-travis-ci-file
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Elle Krout
Nov 9, 2021 9 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?

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

Get started with ACG and transform your career with courses and real hands-on labs in AWS, Microsoft Azure, Google Cloud, and beyond.


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.

ParameterenvThe build environment; this is the environment that the code is deployed onlanguageThe 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 versionservicesEnable a service on the build environment, such as Docker or MongoDBinstallInstallation steps to prepare the build environmentscriptSteps to build the applicationjobs.includeAdditional 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/Phaseapt.addonsAdd apt sources to install packagescache.componentsInstall components needed to cache build, in caching is enabledbefore_installCommands run before install phaseinstallInstallation commandsbefore_scriptCommands to run before application buildscriptBuild scriptbefore_cacheCommands to run before caching buildafter_success/after_failureCommands to run when the build succeeds or failsbefore_deployCommands to run prior to deploying codedeployMethod of deploying the codeafter_deployCommands to run after a successful deployafter_scriptFinal 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!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK