forlooptanzania/ride-my-way

Getting started with Travis CI

Opened this issue · 0 comments

Getting started with Travis

What / Who Is Travis? Why do I need it?

Have you ever written code for an app, It works. Then you added more code to do more things. The app gets more complex and bigger as time goes. Imagine you have like 100 functionalities in your app. You want to check them all. How are you gonna do it.?

Meet travis

Your own personal butler to test your code for you. hehehehe But seriously what is travis ?
Well travis is a service that deals with two things continuous integration (CI) and continuous deployment (CD).

We will focus on CI, for now
The idea behind continuous integration is that CI will automatically run your tests ( unit tests, etc ) and other checks for you.

Why should you care?

You are not a caveman. If you have the time and patience to go through all 100 functionalities, knock yourself out.
If you are lazy, smart and a bad ass, you will use CI.
cavemen

Travis CI to the rescue

As the name suggests,CI stands for Continuous Integration which is an integrated tests that runs continuously on every time when someone pushes a commit (or mostly when opening a Pull Request).
It helps us to automate the testing process. But this doesn't stop you from doing your tests localy.

Setup

The warmup

By now you have noticed that I have mentioned tests a couple of times. Yes tests are a pre requisite to this. You need to have atleast a few tests setup to proceed.

  • First, sign up at their website
  • Link your GitHub account and enable it for the repository you would like to implement this.

Ready set go!

Travis requires instruction to be told what to do. We achieve this using a travis.yml file.

  • On the root folder of your repo create a .travis.yml file.
  • This is very important if you put it anywhere else it will not work

This is where it gets interesting

The .travis.yml file is a configuration file. it contains key and value pairs that have a certain meaning. Here is how it looks like but not limited to.

language: node_js
node_js:
  - "version number"
script:
  - "do something"

This is like the bare minimum you need. More config can be found here

So what does this mean?

  • First you specify the language you are using for your project
  • You can also specify the version (optional)
  • script step this is how you tell travis to do something. Essentially this is where you tell travis to run the test script as specified in you package.json file. eg. npm run test.

How i did it

language: node_js
node_js:
  - "8"
script:
  - cd backend && yarn install && yarn test

What is happening here?

  • Im telling travis that im using nodejs
  • Im specifying node version in my case version 8
  • Im telling travis to navigate to the backend folder, install my project dependencies and then run the tests.

Moment of truth

  • Save the travis.yml file, commit and push.
  • If you have tests setup and have setup travis correctly you should notice that builds are triggerd once you push to your repo or do a PR .
  • Alternatively if you visit https://travis-ci.com/yourname/Reponame you will see details of your build.
    61a91c903f7e56676c3da19936c3c104

See travis docs