Skip to main content

Command Palette

Search for a command to run...

Project: Streamlining CI/CD with Declarative Pipeline.

Published
3 min read

Introduction:

In the dynamic world of software development, efficient and automated CI/CD (Continuous Integration/Continuous Deployment) processes are paramount. This blog will guide you through setting up a declarative pipeline for a Node.js application using Jenkins and Docker, spanning across a master server and a connected agent. Let's dive into the step-by-step process.

Step 1: Setting Up the Master Server:

  • Install Java and Jenkins: Begin by installing Java and Jenkins on your master server. These are essential prerequisites for running Jenkins and managing the build pipeline.
sudo apt update

sudo apt install openjdk-17-jre

java -version

curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee \
  /usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
  https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
  /etc/apt/sources.list.d/jenkins.list > /dev/null

sudo apt-get update

sudo apt-get install jenkins

sudo systemctl start jenkins.service

sudo systemctl status jenkins
  • Create DockerHub Credentials: Navigate to Jenkins' Global Credentials and create DockerHub credentials. These credentials will be used to interact with DockerHub during the build process.

Step 2: Creating the Node-Todo-CI/CD Pipeline:

  • Pipeline Configuration:

    • Give your pipeline a descriptive name and provide a brief description.

    • Specify the Git project URL and choose the appropriate branch.

    • In the pipeline section, select "Pipeline script from SCM" and update the Git details with the repository URL, branch, and script path.

git repo: https://github.com/vishalparit10/node-todo-cicd.git

  • Configuring Jenkins to Run on Agent:

    • Set up a password-less SSH connection between the master and the agent.

      • On master generates key # ssh-keygen

      • key will be generated under /root/.ssh

      • copy public key from master and paste inside the last line of Authorized key under Agent machine.

    • Install Java, Docker, and Docker Compose on the agent machine.

sudo apt update

sudo apt install openjdk-17-jre

java -version

sudo apt install docker.io

sudo apt install docker-composer
  1. Connecting Jenkins to the Agent:

    • Navigate to Jenkins Dashboard > Manage Jenkins > Setup Agent.

    • Provide the agent name, remote root directory, and label matching the Jenkinsfile.

    • Set the launch method to "via SSH."

    • Enter the agent's IP address and credentials.

    • Create SSH credentials for the agent by generating an SSH key on the master and copying the public key to the agent's authorized_keys file.

  2. Launching the Agent:

    • Configure the agent to use the created credentials and select "non-verifying" for host key verification strategy.

    • Save the configuration and launch the agent from the Jenkins dashboard.

Step 3: Running the Pipeline:

  • Build and Deployment:

    • Once the agent is connected, go back to the pipeline and click "Build Now."

    • Observe the pipeline building successfully, and the Node.js application is deployed.

Conclusion:

Implementing a declarative pipeline for your Node.js application using Jenkins and Docker, coupled with an efficiently connected master and agent, can significantly enhance your CI/CD workflow. This streamlined process ensures consistent and reliable software delivery. By following these steps, you've successfully set up a robust CI/CD environment for your Node.js project.