Anatomy of a Travis CI File
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.
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.
Parameterenv
The build environment; this is the environment that the code is deployed onlanguage
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 versionservices
Enable a service on the build environment, such as Docker or MongoDBinstall
Installation steps to prepare the build environmentscript
Steps to build the applicationjobs.include
Additional parameters for parallel builds and stagesinstall
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.addons
Add apt sources to install packagescache.components
Install components needed to cache build, in caching is enabledbefore_install
Commands run before install phaseinstall
Installation commandsbefore_script
Commands to run before application buildscript
Build scriptbefore_cache
Commands to run before caching buildafter_success/after_failure
Commands to run when the build succeeds or failsbefore_deploy
Commands to run prior to deploying codedeploy
Method of deploying the codeafter_deploy
Commands to run after a successful deployafter_script
Final commands to run prior to finishing the Travis runFor 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!
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK