Jenkins Project

Dhwarika JhaDhwarika Jha
3 min read

Jenkins Master (Server)

Jenkins’s server or master node holds all key configurations. The Jenkins master server is like a control server that orchestrates all the workflow defined in the pipelines. For example, scheduling a job, monitoring the jobs, etc.

Jenkins Agent

An agent is typically a machine or container that connects to a Jenkins master and this agent actually executes all the steps mentioned in a Job. When you create a Jenkins job, you have to assign an agent to it. Every agent has a label as a unique identifier.

When you trigger a Jenkins job from the master, the actual execution happens on the agent node that is configured in the job.

A single, monolithic Jenkins installation can work great for a small team with a relatively small number of projects. As your needs grow, however, it often becomes necessary to scale up. Jenkins provides a way to do this called “master to agent connection.” Instead of serving the Jenkins UI and running build jobs all on a single system, you can provide Jenkins with agents to handle the execution of jobs while the master serves the Jenkins UI and acts as a control node.

Pre-requisites

Let’s say we’re starting 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.

Note:- While creating an agent, be sure to separate rights, permissions, and ownership for Jenkins users.

Deploy web app using Jenkins master and worker node.

Fork GitHub link:- https://github.com/dwarika9167/django-todo-cicd

  1. Create EC2 instances as

    • Jenkins-server

  • Jenkins-agent-1

  1. you need to have a Jenkins server set up. If you don't have one already, you can download and install Jenkins from the official website: https://www.jenkins.io/download/ also install Java on both the Master and agent

  2. Generate SSH keys on “Jenkins-server” by running ssh-keygen the command.

  3. Now go to the “.ssh” folder and there will be public and private keys.

  4. Add public key from “Jenkins-server” to “Jenkins-agent-1” under location “.ssh/authorized_keys”

  5. Now, go to the Jenkins dashboard, and click on “Manage Jenkins”.

  6. Now, click on “Manage Nodes and Clouds”

  7. Now, click on “+ New Node”

  8. Add details of your new node according to screensort and click save.

  9. Node Console status.

  10. Write Jenkins File Commit in your GitHub Repository

    1.  pipeline{
           agent { label "devserver" }
           stages{
               stage("Clone Code"){
                   steps{
                       git url: "https://github.com/dwarika9167/node-todo-cicd.git", branch: "master"
                   }
               }
               stage("Build and Test"){
                   steps{
                       sh "docker build . -t node-app-test-new"
                   }
               }
               stage("Push to Docker Hub"){
                   steps{
                       withCredentials([usernamePassword(credentialsId:"dockerHub",passwordVariable:"dockerHubPass",usernameVariable:"dockerHubUser")]){
                       sh "docker tag node-app-test-new ${env.dockerHubUser}/node-app-test-new:latest"
                       sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPass}"
                       sh "docker push ${env.dockerHubUser}/node-app-test-new:latest"
                       }
                   }
               }
               stage("Deploy"){
                   steps{
                       sh "docker-compose down && docker-compose up -d"
                   }
               }
           }
       }
      
  11. The above file will do Clone Code, Build and Test, Push to Docker Hub & Deploy Application.

  12. To Push on Docker Hub we have to tell Docker Hub Credientaial to Jenkins

    • Step to enter DockerHub Credentials.

      Manage Jenkins --> Credentials-->Stores scoped to Jenkins-->global fill details according below Screenshot

  1. Now create Pipeline Project by clicking New Item.

  2. Enter Description and select "Pipeline" as the project type and enter ok.

  3. In the "Pipeline" section, select "Pipeline SCM" enter Git repo URL and click on "Save" to save your job configuration.

  4. Created a new Pipeline job in Jenkins. Now run your Pipeline by clicking on the "Build Now" button on the job's dashboard page.

  5. First Build was Fail because Docker-compose not install on the Agent node so before clicking on Build Now Check all Dependencies

  6. Verify Container running on Agent-node.

  7. Also verify the app on the browser by entering <you agent node IP>:8000.

    Thankyou.....❤❤❤❤❤❤❤

1
Subscribe to my newsletter

Read articles from Dhwarika Jha directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Dhwarika Jha
Dhwarika Jha