# Setting up Continuous Integration(CI) for Node app with Travis-CI

> ***What is Continuous integration?***

> Continuous Integration is the practice of merging in small code changes frequently — rather than merging in a large change at the end of a development cycle. The goal is to build healthier software by developing and testing in smaller increments.

> \- [**Travis-ci**](https://docs.travis-ci.com/user/for-beginners/#what-is-continuous-integration-ci)

![](https://cdn-images-1.medium.com/max/2400/1*mxZjeJdgydFMqJVmnNQofg.jpeg align="center")

Photo by [Andrew Buchanan](https://unsplash.com/@photoart2018?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/s/photos/serial-numbers?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)

After setting up CI with Travis-CI for a Github repository when you push your code to the repository, Travis CI clones it to build and tests your code. If one or more of those tasks fail, the build is considered [broken](https://docs.travis-ci.com/user/for-beginners/#breaking-the-build). If none of the tasks fails, the build is considered [passed](https://docs.travis-ci.com/user/for-beginners/#breaking-the-build)*.* A new VM is used for every build.

Continuous deployment can also be set up with Travis-CI so that whenever a build is successful the code is deployed. It has support for all common platforms like AWS, Google Cloud, Azure and many more.

So, that was the introduction to Travis-CI. Let us now do the setup for the CI pipeline.

**Step1:** [Sign up to Travis-CI](https://travis-ci.org/signup) with your GitHub account.

**Step2:** After signing in to your account, look to the taskbar to your left for a small + sign to add the repository for which you want to set up your CI pipeline.

Something like this will open up:-

![](https://cdn-images-1.medium.com/max/1600/1*DN7PjnRN-mnn5oiDADXJQQ.png align="center")

I will be creating the CI for the project that I used for one of my previous blog on [integration testing for express mongoose app](https://medium.com/codechef-vit/integration-testing-for-express-mongoose-app-44390d29bb2). [Here is the repo link for the updated project.](https://github.com/subhrapaladhi/testing-express-mongoose)

**Step3:** Create a file “.travis.yml” in the root directory of your project.

Here is what .travis.yml contains. You may need to change a few things according to your project but a lot of things will be common. Travis-CI has great [documentation](https://docs.travis-ci.com/), take help from that.

### **Line wise explanation for .travis.yml**

1. Specify the language that your server is using. For node js projects use node\_js, not javascript.
    

```plaintext
language: node_js
```

2\. Specify the version of the language that you are using. I have used the version 12.6.2 of node js.

```plaintext
node_js:
      - "12.6.2"
```

3\. The “dist” property specifies the OS that will be used in the virtual machine as the base image. “trusty” is a very lightweight version of Linux that is well-tailored for small one-off tasks.

```plaintext
dist: trusty
```

4\. The next is “services”. It the external services like database or cache service that your application might be using. It will install an instance of that and start the service in the default port. Like port 27017 for MongoDB.

```plaintext
services:
      - mongodb
```

5\. The “env” property is used to set the environment variable. Here I have declared two env variables, NODE\_ENV and PORT.

```plaintext
env:
     - NODE_ENV: ci PORT: 3001 some_other_env_var: value
```

6\. There may be large files in your project like the node\_modules folder. They can be cached they speed up the pipeline. If any changes are made in the cache folder then the new version is used otherwise the cached version is used.

```plaintext
cache:
    directories:
       - node_modules
       - some_large_folder_not_frequently_changed
```

7\. Now we need to install the dependencies like the node modules. If no new modules have been added after the previous build, the cached node\_modules folder will be used otherwise the dependencies will be installed and the node\_modules folder will be replaced with the new one. You can also mention some other commands for dependencies that are not included in the npm.

```plaintext
install:
      - npm install
      - other install commands
```

8\. Finally, this command is used to run the test scripts.

```plaintext
scripts:
      - npm run test
```

**Step 4:** Now push your code to the repository and go to your Travis-CI account. Select the repository from the left taskbar and goto the “Build History” tab will contain all your past build(in red or green colour) and the currently running build( in yellow) for the repository. Here is what it looks like.

![](https://cdn-images-1.medium.com/max/1600/1*_1xrQqMzi_bkWCcKg3QDyg.png align="center")

Click on the current build and something like this will open up when the build is in process.

![](https://cdn-images-1.medium.com/max/1600/1*TupQ141LsbkvCbeaDR3pxA.png align="center")

Here is what a successful build looks like:

![](https://cdn-images-1.medium.com/max/1600/1*9V5d0TM7Igqebmxp3NQAQg.png align="center")

Here is what a failed build looks like:

![](https://cdn-images-1.medium.com/max/1600/1*aXTgRLOaLdp1izLP4XKwiw.png align="center")

---

#### Resources and References

* [Github Repo](https://github.com/subhrapaladhi/testing-express-mongoose)
    
* [Travis documentation](https://docs.travis-ci.com/)
