A Step-by-Step Guide to Continuous Integration for Your Node.js Application
Tags: javascript, devops, node, tutorial
Published: 2023-02-02 06:20:24

Are you looking for an easier way to manage your software development process? Are you tired of manual steps and time-consuming tasks? If so, a continuous integration pipeline may be your solution! In this blog post, we'll explore what a continuous integration pipeline is and how it can help streamline your development workflow with a sample Node.js application. 

## What is Continuous Integration (CI)?
Continuous Integration (CI) is a software development practice that encourages the frequent merging of code changes from multiple contributors into a single project. This process helps to reduce errors and increase the speed of development. CI leverages automation and standardization to ensure that code changes are tested and integrated quickly and efficiently. By automating tests, CI helps to ensure the quality of the codebase and reduce the risk of errors being introduced into the system.

Continuous integration is an integral part of the software development process, as it helps to ensure that each piece of code is tested throughout the entire process. CI improves overall engineering communication and accountability, which enables greater collaboration between development and operations in a DevOps team.

## Testing in a CI Pipeline
Testing in a CI Pipeline is vital to the overall software development process. Automation is an essential element of any successful CI/CD pipeline. Testing can be done at various stages in the CI pipeline, such as unit testing, integration testing, system testing, and acceptance testing. Each of these tests provides valuable feedback on the quality of the code and its ability to meet customer requirements. Automated testing enables teams to quickly validate code changes and ensure they are up to the highest standards. Automated testing also reduces the time it takes to execute tests, reducing the time it takes for changes to be deployed. Automated testing also helps identify bugs and other issues before they can become significant problems. By using automated tests, teams can release better quality software faster, ensuring that their products remain competitive in the market.

### Prerequisites
- Free [Harness cloud account](https://app.harness.io/auth/#/signup/?module=ci&utm_source=internal&utm_medium=social&utm_campaign=devadvocacy&utm_content=pavan_ci_article&utm_term=get-started) to perform CI
- GitHub account - We will refer to this [sample Node.js weather application](https://github.com/pavanbelagatti/weather-app-example) in the tutorial
- Free [weather API](https://openweathermap.org/api) account to get the API keys

### Tutorial
#### Step 1: Getting Your API Keys:
Go to the [OpenWeather website](https://openweathermap.org/api) and create a free account and get your API keys. Once you sign in to your account, you can easily access your API keys from the profile dropdown, as shown below,
![api keys](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sv8q5zpblu85tlqzmaod.png)

Click on ‘My API keys’, and you should see your API Keys listed.

#### Step 2: Making Changes to the Sample Application:
We have already created a [sample NodeJS weather application](https://github.com/pavanbelagatti/weather-app-example), and you can just fork it to get started. But make sure to add your weather API keys by creating a .env file and adding the below content to the file,
![env file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nd6ad83fay336zkcmh4a.png)

Add your API Keys in the ‘API_KEY’ field. 

If you are all set, go to the root folder of your path and run the command 
```
nodemon server.js
```
When you go to your http://localhost:5004/, you should see the below screen.
![localhost](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dcnb08542r11td7tlvfg.png)

You can fetch the weather forecast for a particular city by going to the local host on your terminal with the below format, this is what we have specified in our server.js file.

http://localhost:5004/weather?city=cityname

Replace cityname with any of the cities, like Bangalore.
![city weather](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/whguachags552xyqs5hu.png)

Step 3: Signup for Harness Continuous Integration Module:
After signing in, create a project and select the CI module.
![about the project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8hk55v35s1hpmpboxcub.png)

![CI module](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zyi0lvx59n0eka7q1lr8.png)

Click on ‘Go to Module’ and  ‘Get Started’ to create your pipeline. 
![get started](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ctrqvt7l8a932jfeqmms.png)

Connect your repository that has all your code. Since our code is on GitHub, we will authenticate with GitHub.
![scm integration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/djv9m28s0dab2isa94n6.png)

Next, select your repository from the list
![select repo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h52rsvq62xa9gg76yv4h.png)

Since it is a Node.js project, select Node.js, and then continue to build the pipeline.
![nodejs pipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j44ss9oe8v55hcpdt8qf.png)

After you select Create Pipeline, you should see the skeleton of your CI pipeline. 
![CI dummy pipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/didjqixdruc1prf340bp.png)

You can edit the title of your pipeline and customize it accordingly. When you hover over the ‘Overview’ tab, you can see the stage details.
![overview](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mbwaxn6hc6jt2xk4gpbh.png)

Next, click ‘Continue’ or the ‘Infrastructure’ tab, and you should see the Harness cloud option. Make sure you select the Cloud option only. 
![Infrastructure tab](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7llzuv9b7veef4wls5v4.png)

Next is setting up the ‘Execution’, and this is where we configure our test.
Click on the ‘Add Step’ and select the ‘Run’ option from Step Library. 
![add step stage](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ktfilee3x3p59q0wzo5a.png)

![step library](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tva0bcvylf8txbyxf355.png)

Configure your ‘Run’ step as shown below,
![configure run step](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m8q3pjv9l0ua45bvmdgy.png)

Add the commands `npm install` and `npm test` in the command section, apply changes and save the pipeline. At this point, your pipeline should look like this.
![run test](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w3gmgmnqmdu2uakri1fq.png)

Now, it is time to run your pipeline and see if the tests are passing. Finally, Save and Run the pipeline.
![run pipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/67hnr6xrmpodtgqx43ry.png)

Once the pipeline executes the steps successfully, you should see the results of the tests.
![successful CI execution](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q6na4dlfnriytm2mxy8i.png)

#### Step 4: Automate Your CI Process:
You can automate your CI process by adding Triggers. When someone pushes any new code to your repository, your pipeline should get triggered and run the defined tests. Let’s see how to do that.

In the pipeline studio, you can click the ‘Triggers’ tab and add your desired trigger. 
![set trigger](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p6s2w7kvn39kca6yc2yk.png)

![add new trigger](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/idsealn5lsywd2xaqyyo.png)

Click on ‘Add New Trigger’ and select ‘GitHub’.
![triggers list](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hpos5erbka9dnylqbo6g.png)

Add the required details and continue. As you can see, we are selecting ‘Push’ as our event. So whenever any push happens to our repository, the pipeline should trigger and run the tests. 
![trigger configuration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sqq0c2ww8qvfezr7i6tv.png)

Once you finish setting up the details, click on ‘Create Trigger’. 
![github trigger setup](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t799k42uhnbq4l4ez5bn.png)

You can see your newly created trigger in the ‘Triggers’ tab.
![trigger tab](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bbqijj1uooy82w3ccebd.png)

Now, whenever anybody pushes any code changes to your main repository, the pipeline automatically gets triggered and runs the tests. 

Let’s go and add some text to your `README.MD` file, and as soon as you add your text, the build gets triggered and runs the tests specified. 
![Build started](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3x397s082irmhl9gcfx8.png)

**Congratulations!** You have successfully set up the automated continuous integration pipeline for your project.