DEV Community

RAM PANDEY
RAM PANDEY

Posted on

An advanced setup for Travis CI

In this blog post, I will guide you through the basics of the YAML file and then we will write a sample .travis.yml file which I try to make as general as possible so that you can use Travis CI more efficiently. Also, this blog post is for those people who are familiar with the concept of CI/CD and testing.

What is YAML?

YAML is a recursive acronym for “YAML Ain’t markup language”. Like JSON it is a data serialization language( i.e it can be used to store or transmit data across networks)

Some differences between YAML and JSON

differences between YAML and JSON

YAML syntax

I will cover some basic syntax that will get you started but if you would like to learn more, refer this link

The basic syntax of YAML is

Key value pairs are separated using ":"

key: value
Enter fullscreen mode Exit fullscreen mode

You can comment using "#"

# this is a comment in YAML
key: value
Enter fullscreen mode Exit fullscreen mode

This knowledge is enough for our example. Now let us start with our .travis.yml file

The Travis config file

Please keep a note on this as YAML files have strict rules for indentation.

The programming tool

We start configuring our .travis.yml file with the language we will be using. For example, I will be creating this Travis file for my MERN stack app so I will choose my language as nodejs for testing my backend code.

language: node_js
node_js:
  - "11"
Enter fullscreen mode Exit fullscreen mode

Now here we tell the Travis CI that we want our environment to be nodejs we also specify the specific version of it. I used v11 you can use any version you want, you can test for multiple version by mentioning them below like

language: node_js
node_js:
  - "11"
  - "12"
Enter fullscreen mode Exit fullscreen mode

You can also test this for the current stable version of nodejs like

language: node_js
node_js:
  - "stable"
Enter fullscreen mode Exit fullscreen mode

The Build Environment

We can specify the distro/OS/Build environment we would like Travis to execute our tests in. I would recommend using trusty as this distro is lightweight and more reliable. Although this is the default distro set by Travis CI, I will show you the config so that you can use any distro you want and is offered by Travis.

dist: trusty
Enter fullscreen mode Exit fullscreen mode

Other distros offered by Travis are

  • Ubuntu Xenial 16.04
  • Ubuntu Precise 12.04

Services

Many times we have various services interacting with our project. We need to include them to completely test our projects. One option you can choose is to integrate an online set up of the services but that would increase the time taken by the tests plus if the service is paid you will have to pay the extra cost for every request to your service. So Travis ci provides a solution for this by providing you the option to install the service you need with the build and configure it according to your needs.
There are many services we can configure but this post is not about it so
I will show you how to configure MongoDB for CI.

services: mongodb
Enter fullscreen mode Exit fullscreen mode

You have to use the key services for any service you want to tell Travis to install the dependencies. Now to configure it you can do it the same way you do locally eg-

var url = "mongodb://localhost:27017/mydb";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  console.log("Database created!");
  db.close();
});
Enter fullscreen mode Exit fullscreen mode

that's it you have to connect to the port no 27017 on localhost. Just follow the same default connection instruction for the service you would like to connect. You can look up here for other services.

Environment variables

You can set up the environment variables in which you would like to test your project using the env key eg -

env:
  - NODE_ENV=ci PORT=3000
Enter fullscreen mode Exit fullscreen mode

cache

You can cache directories in your build so that you do not have to download it on every build as that would cost you time so you can specify the directories you want to cache using

cache:
  directories:
    - node_modules
Enter fullscreen mode Exit fullscreen mode

Here I would like to cache my node_modules directory for every build so I don't have to download it on every build and when I add or delete a node_module Travis would just compare the package.json and make the necessary changes and cache it again

Scripts

There are various dependencies that you would like to be installed before or after the configuration of Travis CI. Or you may need to run various scripts before the build starts executing your tests. You can install or run them by using the keyword before_install to run commands before execution and install to install the dependencies after the Travis finishes to configure the build
eg -

before_install:
  - sudo apt-get install -y libxml2-dev
install:
  - npm install
Enter fullscreen mode Exit fullscreen mode

So here before installing dependencies, I would like to add a dev library and after that, I would like to run the script to install dependencies for my project.

Also, you can run various scripts during the execution of the tests with the script keyword eg -

script:
  - npm run start 
  - npm run dev
Enter fullscreen mode Exit fullscreen mode

So these are some advanced setup you can configure Travis with to make your life easy and use CI efficiently.

I would love to hear some more advanced setup from you guys that you use or your feedback for this blog I would appreciate it. Thanks for reading and I would love to hear if I was able to impart new knowledge in you through this blog post.

You can connect with me here at twitter or LinkedIn.

I live by the name of @ram2510 on the internet

Top comments (2)

Collapse
 
gklijs profile image
Gerard Klijs

You can add things like

addons:
  chrome: stable
  apt:
    sources:
      - google-chrome
    packages:
      - chromium-browser=74.0.3729.169-0ubuntu0.16.04.1
      - chromium-chromedriver

To install stuff, in this case chrome and the chrome we driver to be able to do tests in the browser.

Collapse
 
rampa2510 profile image
RAM PANDEY

Thanks for sharing also you can install different versions of services other than the default versions of the services you integrate using addons