Skip to main content

Command Palette

Search for a command to run...

Project : Effortless Web App Deployment with Jenkins Master and Worker Nodes

Updated
4 min read

Today we will deploy node.js app on EC2 Server using Jenkins master and Worker nodes.

We will be using this project : https://github.com/patelajay745/node-todo-cicd

Pre-requisites

You should have two EC2 instances with a fresh Ubuntu 22.04 Linux installation. To get an agent working make sure you install Java ( same version as jenkins master server ) and Docker on it.

Let's start.

This is my instance for master node(jenkins_server)

This is my instance for agent node(jenkins_agent)

Now we need to do ssh-keygen for secure communication between two instances.

Go to your jenkins_server type "ssh-keygen" it will ask path and passphrase. we can keep it black to save it as default.

now go to .ssh folder and you can see two files: id_rsa(private key) and id_rsa.pub(public key)

now copy your public key . you can use "cat" to see and copy it.

now go to your jenkins_agent instance and go to .ssh folder.

edit your authorized_keys using "vim" and paste your copied public key from jenkins_server.and save it.

Now everything is set up. Now we will use our Jenkins to create our pipeline. Before that we need to do some setting in our Jenkins so Go to your Manage Jenkins.

click on Manage Nodes and Clouds.

Create a New node. Give a name to it and select permanent Agent.

Give a description if you want. You should set "Remote root Directory", where all code from GitHub will be saved. I will use "/home/ubuntu/agent1"

You should give a label to this agent(it is very important as we will run our pipeline by this label only)

In the Usage you should select "Only build jobs with label expressions matching this node"

now go to Launch method below and select "Launch agent via SSH"

Enter your public IP of your jenkins_agent in Host

In credentials, you should click on Add. which will open a new window like below.

select SSH username with private key. give id to it.

username should be "ubuntu" and in the private key section add your jenkins_server private key. and save it.

in the "Host Key Verification Strategy," you should select "Non verifying verification strategy" and create your node. your Node will look like below screenshot.

if you check the console of agent then it will show agent successfully connected and online.

Now It's time to create Our declarative pipeline . click on "New Item" and select pipeline. click ok.

you can give description to it and url of GitHub project. we are using https://github.com/patelajay745/node-todo-cicd

we need to enter our Groovy pipeline script.

pipeline {
    agent { label "agent1" } 

    stages {
        stage('Checkout') {
            steps {
                git url : "https://github.com/patelajay745/node-todo-cicd/", branch : "master"
            }
        }
        stage('Build and Test') {
            steps {
                sh "docker build . -t node-app-new:latest"
            }
        }
        stage('Push To DockerHub') {
            steps {
                withCredentials([
                    usernamePassword(
                        credentialsId: "DockerHub",
                        passwordVariable: 'PASSWORD',
                        usernameVariable: 'USER'
                    )
                ]){ 
                    sh "docker tag node-app-new:latest ${env.USER}/node-app-new:latest"
                    sh "docker login -u ${env.USER} -p ${env.PASSWORD}"
                    sh "docker push ${env.USER}/node-app-new:latest"
                }

            }
        }
        stage('Deploy') {
            steps {
                sh "docker-compose down && docker-compose up -d"
            }
        }
    }
}

Let me explain it.

agent: Specifies the agent or node where the pipeline will be executed. In this case, the label "agent1" is used to identify the specific node.

  1. stages: Defines a sequence of stages in the pipeline. Each stage represents a specific phase of the build and deployment process.

  2. stage('Checkout'): The first stage is named "Checkout" and is responsible for cloning the source code repository from GitHub using the git step.

  3. stage('Build and Test'): This stage builds the Docker image for the Node.js application using the docker build command. It tags the image as "node-app-new:latest".

  4. stage('Push To DockerHub'): This stage handles pushing the Docker image to DockerHub. It uses the withCredentials block to securely provide DockerHub credentials (username and password) stored in Jenkins credentials store. It tags the image with the username and pushes it to DockerHub.

  5. stage('Deploy'): The final stage is responsible for deploying the application using Docker Compose. It first shuts down any existing containers using docker-compose down and then starts new containers in detached mode using docker-compose up -d.

we have used environmental variable using "Environment Injector Plugin"

Now save it and click on build now . if everything is okay then your stage will look like below.

and if everything is build successfully then you can check your jenkins_agent public Ip:port on which your application is running.

That's it.It was all for Day 28 of #90daysofdevOps.You have created your CI/CD pipeline using Jenkins master and agent node.

I hope this blog helps you to create and clear your doubt about it, if now then please comment below , I will happy to help you out. See you with another challenge.

#Jenkins #Docker #CI/CD #DevOps #WebAppDeployment #Nodejs #Automation #SoftwareDevelopment #ContinuousIntegration #ContinuousDeployment #PipelineAsCode #DevOpsTools #SoftwareDelivery #TechBlog

More from this blog

Ajay Patel

116 posts

Project : Effortless Web App Deployment with Jenkins Master and Worker Nodes