0
0
Node.jsframework~5 mins

CI/CD pipeline basics in Node.js

Choose your learning style9 modes available
Introduction

A CI/CD pipeline helps automate the process of testing and delivering code changes quickly and safely.

When you want to automatically test your code after every change.
When you want to deploy your app to a server without doing it manually.
When you want to catch errors early before they reach users.
When multiple developers work on the same project and need smooth updates.
When you want to save time by automating repetitive tasks like building and testing.
Syntax
Node.js
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        // commands to build your app
      }
    }
    stage('Test') {
      steps {
        // commands to test your app
      }
    }
    stage('Deploy') {
      steps {
        // commands to deploy your app
      }
    }
  }
}

This example shows a simple pipeline structure with three stages: Build, Test, and Deploy.

Each stage contains steps that run commands or scripts.

Examples
A pipeline with only a Build stage that prints a message.
Node.js
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo 'Building the app...'
      }
    }
  }
}
A pipeline with a Test stage that runs tests using npm.
Node.js
pipeline {
  agent any
  stages {
    stage('Test') {
      steps {
        sh 'npm test'
      }
    }
  }
}
A pipeline with a Deploy stage that copies files to a server.
Node.js
pipeline {
  agent any
  stages {
    stage('Deploy') {
      steps {
        sh 'scp -r ./dist user@server:/var/www/app'
      }
    }
  }
}
Sample Program

This pipeline installs dependencies, runs tests, and deploys the app to a server automatically.

Node.js
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo 'Installing dependencies...'
        sh 'npm install'
      }
    }
    stage('Test') {
      steps {
        echo 'Running tests...'
        sh 'npm test'
      }
    }
    stage('Deploy') {
      steps {
        echo 'Deploying app...'
        sh 'scp -r ./dist user@server:/var/www/app'
      }
    }
  }
}
OutputSuccess
Important Notes

Each stage runs only if the previous stage succeeded, helping catch errors early.

You can customize stages and steps to fit your project needs.

Using a CI/CD tool like Jenkins, GitHub Actions, or GitLab CI makes running pipelines easy.

Summary

A CI/CD pipeline automates building, testing, and deploying code.

It helps deliver software faster and with fewer errors.

Simple pipelines have stages like Build, Test, and Deploy.