A Step-by-Step Guide to Continuous Integration for Your Node.js Application
Pavan Belagatti
Posted on February 2, 2023
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 to perform CI
- GitHub account - We will refer to this sample Node.js weather application in the tutorial
- Free weather API account to get the API keys
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,
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,
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.
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.
Step 3: Signup for Harness Continuous Integration Module:
After signing in, create a project and select the CI module.
Click on ‘Go to Module’ and ‘Get Started’ to create your pipeline.
Connect your repository that has all your code. Since our code is on GitHub, we will authenticate with GitHub.
Next, select your repository from the list
Since it is a Node.js project, select Node.js, and then continue to build the pipeline.
After you select Create Pipeline, you should see the skeleton of your CI 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.
Next, click ‘Continue’ or the ‘Infrastructure’ tab, and you should see the Harness cloud option. Make sure you select the Cloud option only.
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.
Configure your ‘Run’ step as shown below,
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.
Now, it is time to run your pipeline and see if the tests are passing. Finally, Save and Run the pipeline.
Once the pipeline executes the steps successfully, you should see the results of the tests.
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.
Click on ‘Add New Trigger’ and select ‘GitHub’.
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.
Once you finish setting up the details, click on ‘Create Trigger’.
You can see your newly created trigger in the ‘Triggers’ 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.
Congratulations! You have successfully set up the automated continuous integration pipeline for your project.
Posted on February 2, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
February 2, 2023