Thursday, October 3, 2024

Mastering Node.js CI/CD: Build & Deploy with Azure DevOps to Azure Web App

 In today’s fast-paced world of software development, Continuous Integration and Continuous Deployment (CI/CD) have become essential for delivering robust applications. In this guide, I’ll walk you through building and deploying a Node.js web application using Azure DevOps pipelines, integrated seamlessly with an Azure Web App for hosting.

Why CI/CD Matters

Gone are the days of manual deployments, where every bug fix or feature release took hours, sometimes days. CI/CD pipelines automate the entire build, test, and deployment process, significantly improving productivity, reducing risks, and ensuring quicker releases.

For a visual walkthrough of the concepts covered in this article, check out my YouTube Video:-

Key Benefits of CI/CD:

  • Automated Testing: Ensures that only quality code gets deployed.
  • Speed: Fast deployment, reducing time-to-market.
  • Reliability: Fewer manual errors and increased consistency in deployment.

Step-by-Step Guide to Build & Deploy a Node.js App

Prerequisites:

  • Node.js installed on your local machine.
  • Azure DevOps Account: Sign up at Azure DevOps.
  • Azure Subscription: You need access to deploy the application in Azure Web App.
  • Basic Git knowledge for repository management.

Step 1: Setting Up Your Node.js Application

First, create a basic Node.js application. If you already have one, skip this step.

mkdir nodejs-app
cd nodejs-app
npm init -y
npm install express

Create a simple server.js file to set up your application:

const express = require('express');
const app = express();
const port = process.env.PORT || 3000;

app.get('/', (req, res) => {
res.send('Hello World from Node.js App!');
});

app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});

Now, push this code to a Git repository (Azure Repos, GitHub, or any Git service supported by Azure DevOps).

Step 2: Setting Up Azure DevOps Pipeline

  1. Create a New Project in Azure DevOps. After signing in, go to “New Project” and give your project a name.
  2. Connect Your Git Repository. Navigate to the “Repos” section or add your external Git repo if hosted elsewhere.
  3. Go to Pipelines and click on “Create Pipeline.”
  4. Choose your code repository.
  5. Select “Node.js” as the template. This will create a basic pipeline script.

Your basic azure-pipelines.yml might look like this:

trigger:
- main

pool:
vmImage: 'ubuntu-latest'

steps:
- task: NodeTool@0
inputs:
versionSpec: '14.x'
displayName: 'Install Node.js'

- script: |
npm install
npm run build
displayName: 'Install dependencies and build'

- script: npm test
displayName: 'Run tests'

- task: ArchiveFiles@2
inputs:
rootFolderOrFile: '$(Build.SourcesDirectory)'
includeRootFolder: false
archiveType: 'zip'
archiveFile: '$(Build.ArtifactStagingDirectory)/drop.zip'
replaceExistingArchive: true

- publish: $(Build.ArtifactStagingDirectory)/drop.zip
artifact: drop

This pipeline installs Node.js, runs the tests, and creates an artifact that we’ll later deploy to Azure.

Step 3: Deploying to Azure Web App

  1. In Azure DevOps, add a new stage for deployment in the release pipeline.
  2. Go to Pipelines > Releases, and create a new release pipeline.
  3. Add an Azure App Service Deployment Task. Choose the subscription, app service name, and set the artifact as the zip file generated in the build pipeline.
  4. Configure the deployment to use the correct web app service in Azure.

Your deployment stage YAML code might look like this:

- task: AzureRmWebAppDeployment@4
inputs:
azureSubscription: '<Your Azure Subscription>'
appType: 'webApp'
appName: '<Your Web App Name>'
packageForLinux: '$(System.DefaultWorkingDirectory)/**/*.zip'

Step 4: Test the Pipeline

Once your pipeline is set up, test it by making changes to your code and committing it to the main branch. The pipeline will automatically trigger the build and deploy the latest version to your Azure Web App.

Step 5: Monitor and Scale

After deployment, you can monitor the performance of your Node.js application using Azure’s built-in tools. The Azure App Service provides real-time performance monitoring, logs, and scaling options, allowing you to automatically scale based on traffic.

Conclusion

Mastering CI/CD with Azure DevOps unlocks a powerful workflow that makes your Node.js development faster, more reliable, and more scalable. By automating testing, building, and deploying to Azure Web Apps, you save time and reduce errors, ensuring your application is always up-to-date with the latest features.

Whether you’re just getting started or looking to improve your existing CI/CD process, this guide provides the foundation to get your Node.js apps up and running smoothly with Azure DevOps.

Connect with Me:

No comments:

Post a Comment

Wireless Security Configuration: Protect Your Network Now!

Introduction: In today’s connected world, wireless networks are as common as smartphones, and they’re often the gateway to our personal, pr...