7-setting-up-a-cicd-pipeline-for-a-nestjs-project-on-azure.html

Setting Up a CI/CD Pipeline for a NestJS Project on Azure

Continuous Integration (CI) and Continuous Deployment (CD) are essential practices in modern software development. They help automate the process of building, testing, and deploying applications, ensuring faster releases and higher-quality code. In this article, we'll explore how to set up a CI/CD pipeline for a NestJS project on Azure, complete with step-by-step instructions, code snippets, and actionable insights.

What is CI/CD?

Continuous Integration (CI) is the practice of automatically integrating code changes from multiple contributors into a shared repository several times a day. This process includes running tests to ensure that new changes do not break existing functionality.

Continuous Deployment (CD) extends CI by automatically deploying every change that passes the tests to production, facilitating rapid release cycles.

Why Use CI/CD?

  • Faster Release Cycles: Automate repetitive tasks to focus on coding.
  • Improved Code Quality: Catch bugs early with automated testing.
  • Consistent Environments: Eliminate discrepancies between development and production.

Prerequisites

Before diving into the setup, ensure you have:

  • An Azure account
  • Node.js installed (preferably version 14 or later)
  • NestJS CLI installed (npm i -g @nestjs/cli)
  • An existing NestJS project

Step 1: Create a NestJS Application

If you don't have a NestJS project yet, you can create one using the following command:

nest new my-nestjs-app

Change into the project directory:

cd my-nestjs-app

Step 2: Initialize a Git Repository

Initialize a Git repository in your project directory if you haven't already:

git init
git add .
git commit -m "Initial commit"

Step 3: Set Up Azure DevOps

  1. Create a New Project in Azure DevOps:
  2. Log in to Azure DevOps.
  3. Click on "New Project" and fill in the details.

  4. Set Up Repositories:

  5. Navigate to Repos > Files.
  6. Click on "Import Repository" and connect to your GitHub or Bitbucket repository, or create a new one.

Step 4: Create Azure Pipelines

Azure Pipelines will automate the CI/CD process. Here’s how to set it up:

  1. Create a Pipeline:
  2. In Azure DevOps, go to Pipelines and click "Create Pipeline."
  3. Choose "GitHub" or "Azure Repos Git" based on your source control.

  4. Configure the Pipeline:

  5. Select "Starter Pipeline" or "Existing Azure Pipelines YAML file."
  6. If you choose "Starter Pipeline," you can replace the default YAML with your NestJS CI/CD pipeline configuration.

Here’s a basic example of an Azure Pipelines YAML file for a NestJS project:

trigger:
  branches:
    include:
      - main

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: NodeTool@0
  inputs:
    versionSpec: '14.x'

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

- task: AzureWebApp@1
  inputs:
    azureSubscription: '<Your Azure Subscription>'
    appName: '<Your Azure App Service Name>'
    package: '$(System.DefaultWorkingDirectory)/**/*.zip'

Key Components of the YAML File

  • trigger: Specifies which branches trigger the pipeline.
  • pool: Defines the environment in which the pipeline will run.
  • steps: Lists the tasks to be performed:
  • NodeTool: Installs the specified version of Node.js.
  • script: Runs commands to install dependencies and build the application.
  • AzureWebApp: Deploys the built application to Azure App Service.

Step 5: Set Up Azure App Service

To host your NestJS application, you need to create an Azure App Service:

  1. Create an App Service:
  2. Go to Azure Portal and select "Create a resource" > "Web App."
  3. Fill in the details, including Subscription, Resource Group, and App Name.
  4. Choose the Node.js version that matches your project.

  5. Configure Application Settings:

  6. Navigate to your App Service.
  7. Under "Settings," select "Configuration."
  8. Add any necessary environment variables your NestJS app may need.

Step 6: Deploy Your Application

After setting up your pipeline and Azure App Service, commit your changes and push them to your repository:

git add .
git commit -m "Set up CI/CD pipeline"
git push origin main

This push will trigger the CI/CD pipeline. You can monitor the progress under the Pipelines section in Azure DevOps.

Troubleshooting Common Issues

  1. Build Failures:
  2. Check your YAML file for syntax errors.
  3. Ensure all dependencies are correctly specified in package.json.

  4. Deployment Issues:

  5. Verify that your Azure App Service is correctly set up.
  6. Check logs in Azure for any runtime errors.

  7. Environment Variables:

  8. Ensure that all required environment variables are set in Azure App Service.

Conclusion

Setting up a CI/CD pipeline for your NestJS project on Azure can significantly enhance your development workflow. By automating testing and deployment, you can focus more on coding and less on manual processes. This guide provides a comprehensive overview of creating a robust CI/CD pipeline tailored for NestJS applications, ensuring your projects are well-managed and deployed efficiently. Start implementing these steps today, and elevate your software development process!

SR
Syed
Rizwan

About the Author

Syed Rizwan is a Machine Learning Engineer with 5 years of experience in AI, IoT, and Industrial Automation.