What are the steps to set up a continuous integration pipeline using Azure DevOps for a Node.js project?

12 June 2024

With the increasing importance of continuous integration in the software development process, many developers are seeking effective ways to implement this practice in their projects. Microsoft Azure DevOps offers an excellent platform for setting up continuous integration pipelines. In this article, you'll learn how to use Azure DevOps to create a continuous integration pipeline for a Node.js project.

Step 1: Create a New Project in Azure DevOps

First things first, you need a project in Azure DevOps where you will set up your continuous integration pipeline. To create a new project:

  • Navigate to Azure DevOps and sign in to your account. If you don’t have an account, you can create one for free.
  • Click on the 'Create Project' button, enter a name and description for your project, and select 'Private' or 'Public' as per your preference.
  • Click 'Create' to finalize the creation of your new project.

Azure DevOps projects are where your pipelines, code repositories, and other resources reside. They're the foundation of your DevOps service.

Step 2: Import Your Node.js Code to Azure Repos

After setting up a project, you need to import your Node.js code to Azure Repos. It's where Azure stores and manages your codebase.

  • Navigate to your project page and click on 'Repos'.
  • Select 'Import repository' and enter your existing code repository's URL.
  • Click on 'Import' to bring your Node.js code to Azure Repos.

The imported code will serve as the basis of your pipeline. You can also add more code or modify the existing one as per your project requirements.

Step 3: Set up a Build Pipeline

The next step is to set up a build pipeline. It's where Azure will compile and test your code, ensuring that it's ready for deployment.

  • Navigate to your project page and click on 'Pipelines'.
  • Click 'New Pipeline' and select 'Azure Repos Git' as your code source.
  • Select your repository and Node.js as your template.
  • In the reviewing stage, add a script task. It will test your code. For example:
- script: npm install
  displayName: 'Install dependencies'
- script: npm test
  displayName: 'Run tests'
  • Click 'Save and run' to set up the build pipeline.

Now, each time you make changes to your code, Azure will automatically build and test it.

Step 4: Set up a Deployment Pipeline

Once your code is built and tested, it needs to be deployed. Azure uses deployment pipelines to automate this process.

  • Navigate to your project page and click on 'Pipelines'.
  • Click 'New Pipeline' and select 'Azure Repos Git' as your code source.
  • Select your repository and the App Service build and deployment handler.
  • In the reviewing stage, add a task to deploy your app. For example:
- task: AzureRmWebAppDeployment@4
  displayName: 'Azure App Service Deploy'
    azureSubscription: '<Your Azure Subscription>'
    WebAppName: '<Your Web App Name>'
    packageForLinux: '$(Build.ArtifactStagingDirectory)/**/*.zip'
  • Click 'Save and run' to set up the deployment pipeline.

Now, every time Azure builds your code successfully, it will automatically deploy it to your chosen service.

Step 5: Monitor Your Pipeline

The final step in setting up your continuous integration pipeline in Azure DevOps is to monitor it. Azure DevOps provides several tools that can help you keep a close eye on your pipeline's performance.

On the pipeline page, you can view a detailed report of each pipeline run. It includes the result of the run (whether it succeeded or failed), the changes that triggered the run, and more. By monitoring your pipeline, you can quickly react to any issues and ensure the continuous delivery of high-quality software.

By following these steps, you can successfully create a continuous integration pipeline using Azure DevOps for a Node.js project. The process might seem complex at first, but with practice, it will become a part of your normal development workflow.

Step 6: Set up a Service Connection in Azure DevOps

To enable your Azure DevOps project to interact with other Azure services, you need to set up a Service Connection. This process allows your pipelines to access and deploy resources to your Azure subscription.

Start by navigating to your project settings in Azure DevOps. Then locate and click on 'Service Connections'. Click 'New Service Connection' and choose 'Azure Resource Manager' from the dropdown list. This type of connection is most suitable for .Net Core or ASP. Net projects, but can also be used for Node.js projects.

Upon selection, you will be prompted to fill in your Azure subscription details. If you do not have an Azure subscription, you'd need to create one. Fill this form diligently and click 'Next'.

In the next form, you will choose the Resource Group under which your project falls. The Resource Group is a logical container for resources that are deployed within an Azure subscription. Once you've selected the appropriate resource group, click 'Save'.

After saving, Azure will create a new service connection for your project. This connection is important as it enables Azure Pipelines to deploy your Node.js app to Azure App Service or any other Azure services your project might require.

Remember to give your service connection a meaningful name. This will make it easier for you to identify and manage in the future.

Step 7: Validating Your Node.js CI Pipeline in Azure DevOps

After setting up the continuous integration pipeline for your Node.js project in Azure DevOps, it is crucial to validate it. By doing this, you can ensure that the pipeline works as expected, and that any code changes will trigger a build and subsequently a deployment.

To validate your pipeline, make a simple change in your Node.js code and commit the change. Once you commit and push your changes, go to the 'Pipelines' section of your Azure DevOps project. You will see that a new build starts running automatically, thanks to the continuous integration you've set up.

Once the build completes successfully, it will trigger the deployment. You can click on the build and deployment to see the details. If the build or deployment fails, Azure Pipelines will provide logs to help you debug the issue.

In conclusion, setting up a continuous integration pipeline using Azure DevOps for a Node.js project involves creating a project, importing your code, setting up build and release pipelines, creating a service connection, and finally monitoring and validating your pipeline. It might seem like an overwhelming task at first, but once you get the hang of it, you will appreciate the benefits of continuous integration.

Using Azure DevOps for your CI/CD not only automates your build and release process but also ensures that your development process is efficient, and your software is always in a releasable state. Moreover, with Azure DevOps, you can scale your operations as your project grows.

Remember, the goal of implementing continuous integration using Azure DevOps is to identify and address bugs quicker, improve software quality, and reduce the time it takes to validate and release new software updates. By following the outlined steps, you can achieve these and more. Happy coding!

Copyright 2024. All Rights Reserved