Jenkins Project
Table of contents
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
Create EC2 instances as
- Jenkins-server
- Jenkins-agent-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
Generate SSH keys on “Jenkins-server” by running
ssh-keygen
the command.Now go to the “.ssh” folder and there will be public and private keys.
Add public key from “Jenkins-server” to “Jenkins-agent-1” under location “.ssh/authorized_keys”
Now, go to the Jenkins dashboard, and click on “Manage Jenkins”.
Now, click on “Manage Nodes and Clouds”
Now, click on “+ New Node”
Add details of your new node according to screensort and click save.
Node Console status.
Write Jenkins File Commit in your GitHub Repository
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" } } } }
The above file will do Clone Code, Build and Test, Push to Docker Hub & Deploy Application.
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
Now create Pipeline Project by clicking New Item.
Enter Description and select "Pipeline" as the project type and enter ok.
In the "Pipeline" section, select "Pipeline SCM" enter Git repo URL and click on "Save" to save your job configuration.
Created a new Pipeline job in Jenkins. Now run your Pipeline by clicking on the "Build Now" button on the job's dashboard page.
First Build was Fail because Docker-compose not install on the Agent node so before clicking on Build Now Check all Dependencies
Verify Container running on Agent-node.
Also verify the app on the browser by entering <you agent node IP>:8000.
Thankyou.....❤❤❤❤❤❤❤
Subscribe to my newsletter
Read articles from Dhwarika Jha directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by