Azure Tips and Tricks – Setup an Azure Pipeline with Node.js

This post has been republished via RSS; it originally appeared at: Azure Developer Community Blog articles.

Setup an Azure Pipeline with Node.js

 

 

Build a Node.js application with Azure Pipelines

A great way to improve the quality of your application is to automatically build it whenever you make changes. This enables you and your team to see if the application still works and fix errors quickly. You can easily set up a build pipeline with Azure Pipelines, which is part of Azure DevOps.

 

In this post, we'll create an Azure Pipeline that builds and tests a Node.js application.

Prerequisites

If you want to follow along, you'll need the following:

 

Create an Azure Pipeline that builds and tests a Node.js application

Let's automate the build process of a Node.js app! For this example, we'll use an existing Node application from GitHub. Before we can start, we need to fork it:

  1. Go to https://github.com/MicrosoftDocs/pipelines-javascript and sign in with your GitHub account
  2. Click the Fork button at the top of the repository. This will fork the repository to your account so that we can use it in our pipeline

Now we'll create the pipeline to build and test the Node application:

  1. Go to the Azure DevOps portal and sign in
  2. Create a new project and click on it when you are done

manojsrinivas_0-1656024132245.png

 

(Azure DevOps project)

 

  1. In the "Welcome to the project!" pane, click on Pipelines to start creating a new Azure Pipeline. This will start a wizard
  2. First, we need to indicate where our code is. It is in GitHub, so select GitHub

manojsrinivas_1-1656024132299.png

 

(Code choices in the Azure Pipelines wizard)

 

  1. Next, you'll see a list of repositories in your GitHub account. Select the forked repository that contains the sample Node.js application a. You might have to authorize Azure DevOps to use the GitHub repository. If so, click Approve & Install
  2. In the next step, select Node.js to configure the pipeline for Node.js

manojsrinivas_2-1656024132330.png

 

(Configuration choices in the Azure Pipelines wizard)

 

  1. The next screen shows you the YAML code that defined the pipeline. You can learn more about YAML here. This code determines that node.js will be used and that the pipeline will perform a build. This code will only build the code and not execute any unit tests. Let's change that:
    a. In the script section of the YAML code, add the following to enable the pipeline to run the unit tests in the code:

    npm test

    b. Below the scripts section, add the following code to publish the test results to the build results:

    - task: PublishTestResults@2 inputs: testResultsFormat: 'JUnit' testResultsFiles: '**/TEST-*.xml'
     

manojsrinivas_3-1656024132281.png

 

(YAML code of the Azure Pipeline)

 

  1. Now click Save and run to save the pipeline and start it. The pipeline will now start building and testing the code. When it is done, you'll see the results like in the image below:

manojsrinivas_4-1656024132347.png

 

(Results of the Azure Pipeline)

 

  1. The build was successful! In the screen of the build results, you can click the Tests tab to see the results of the Unit Tests
  2. In the test result screen, you can see the Unit Tests that ran (which is only one test in this case), and what the result of the run was

manojsrinivas_5-1656024132314.png

 

(Test results of the Azure Pipeline)

 

That's it. We have set up a pipeline that automatically builds and tests the application when changes are made to it. From here, you can set up more pipelines, like a release pipeline, that takes the build result and deploys it to a server. You can learn more about that here.

Conclusion

Setting up a Continuous Integration (CI) process, which builds your code automatically, is one of the simplest ways to increase the quality of your applications. It is very easy to set up a CI pipeline with Azure Pipelines, regardless of the programming language that you use or where your code is. To learn more, create a trial account today and go and check it out

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.