A Step-by-Step Guide to Continuous Integration for Your Node.js Application

Pavan Belagatti - Feb 2 '23 - - Dev Community

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

Tutorial

Step 1: Getting Your API Keys:

Go to the OpenWeather website 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

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, 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

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


Enter fullscreen mode Exit fullscreen mode

When you go to your http://localhost:5004/, you should see the below screen.
localhost

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

Step 3: Signup for Harness Continuous Integration Module:
After signing in, create a project and select the CI module.
about the project

CI module

Click on ‘Go to Module’ and ‘Get Started’ to create your pipeline.
get started

Connect your repository that has all your code. Since our code is on GitHub, we will authenticate with GitHub.
scm integration

Next, select your repository from the list
select repo

Since it is a Node.js project, select Node.js, and then continue to build the pipeline.
nodejs pipeline

After you select Create Pipeline, you should see the skeleton of your CI pipeline.
CI dummy pipeline

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

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

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

step library

Configure your ‘Run’ step as shown below,
configure run step

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

Now, it is time to run your pipeline and see if the tests are passing. Finally, Save and Run the pipeline.
run pipeline

Once the pipeline executes the steps successfully, you should see the results of the tests.
successful CI execution

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

add new trigger

Click on ‘Add New Trigger’ and select ‘GitHub’.
triggers list

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

Once you finish setting up the details, click on ‘Create Trigger’.
github trigger setup

You can see your newly created trigger in the ‘Triggers’ tab.
trigger tab

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

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

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .