Skip to main content

Command Palette

Search for a command to run...

Jenkins

Published
10 min read
Jenkins

What is Jenkins?

Jenkins is an open-source automation server that is used to automate software development processes, such as building, testing, and deploying software. It is a widely used tool in the software development industry to support continuous integration and continuous delivery (CI/CD) practices.

Jenkins provides a web-based interface that allows users to create and manage build jobs, which are automated tasks that can perform various tasks, such as compiling source code, running unit tests, deploying applications, and more. Jenkins supports integration with various version control systems, such as Git, SVN, and Mercurial, which allows it to automatically trigger build jobs when changes are made to the source code repository.

Jenkins also provides a vast library of plugins that can be used to extend its functionality and integrate with various other tools and systems, such as Docker, Kubernetes, AWS, and more. This flexibility and extensibility make Jenkins a popular choice for automating software development processes in a wide range of industries and organizations.

In summary, Jenkins is a powerful automation server that helps software development teams to automate their build, test, and deployment processes, enabling faster and more reliable software releases.

What are Masters and Agents?

Master Node: In Jenkins, a master node is the main node that controls the entire build process. It manages the build queue, schedules build jobs, and distributes the build workload to slave nodes. The master node is responsible for storing the configuration information of all the jobs, plugins, and users of the Jenkins system.

The master node is the central point of control for a Jenkins instance and is typically where the Jenkins server is installed. It can run on a separate machine or can be installed on the same machine as the slave nodes.

The master node also communicates with the slave nodes to distribute build jobs to them. The slave nodes are responsible for running the actual build processes, while the master node handles the coordination and management of those processes.

Jenkins allows you to configure multiple master nodes to provide redundancy and load balancing, but typically, only one master node is used in a Jenkins instance.

Agents Node: In Jenkins, an agent node, also known as a slave node, is a worker machine that is used to execute build jobs on behalf of the master node. The agent node is a separate computer or virtual machine that is connected to the Jenkins master node via a network connection.

When a build job is triggered on the Jenkins master node, the master node sends the job to one of the available agent nodes for execution. The agent node then performs the build process and reports back the results to the master node.

Using agent nodes allows you to distribute the build workload across multiple machines, which can improve the speed and efficiency of your build processes. You can configure Jenkins to use multiple agent nodes, and you can also set up specific agents with different capabilities or configurations to handle different types of build jobs.

Agent nodes can be configured to run on various operating systems, such as Windows, Linux, or macOS, and can be set up with specific software or tools required for the build process.

In summary, the agent node is a critical component of a Jenkins instance that allows you to scale your build processes by distributing the workload across multiple machines.

How to connect agents to the Master node.

To connect an agent node to a Jenkins master node, you need to follow these steps:

  1. Generate an SSH key pair on the agent node using the ssh-keygen command. This command will create a public key and a private key, which will be used to authenticate the agent node to the Jenkins master node.

  2. Copy the public key from the agent node to the Jenkins master node. You can do this by using the ssh-copy-id command, which will copy the public key to the authorized_keys file on the master node.

  3. On the Jenkins master node, navigate to the "Manage Jenkins" page and click on the "Manage Nodes and Clouds" option.

  4. Click on the "New Node" button to create a new agent node.

  5. Enter a name for the node, and select the "Permanent Agent" option.

  6. Enter the connection details for the agent node, such as the hostname or IP address, and the port number for the Jenkins agent service.

  7. Choose the "Launch agent via SSH" option as the launch method, and enter the username and private key file path for the agent node.

  8. Save the node configuration, and Jenkins will attempt to connect to the agent node using the specified connection details and launch method.

  9. Once the agent node is connected and online, you can use it to run build jobs and distribute the workload across multiple machines.

In summary, connecting an agent node to a Jenkins master node using the SSH keygen method involves generating an SSH key pair on the agent node, copying the public key to the master node, and configuring the agent node settings on the master node to use the SSH launch method with the appropriate username and private key file path.

How to code build, Push, Pull.

How to Run on Agent

Production Pipeline.

GitHub>>Code Clone>>Code Build & Test>>Sonarqube>>Deploy

Tools: Below tools will work under Jenkins

  1. GitHub

  2. Docker

  3. DockerHub

  4. EC2 Machine (Deployment)

To create Production Pipeline and the below tools will be required.

  1. GitHub : We will use this for code

  2. Docker : Build and Test

  3. DockerHub : Code will push to Docker hub from docker

  4. EC2 Machine (Deployment) : Code will deploy on EC2 instance

Note: All the above work will be performed with the help of Jenkins

Jenkins will instruct GitHub to get code then Jenkins will instruct Docker to build the code and push it to DockerHub then Deploy on EC2.

Jenkins will run on a dedicated server and all the above tools will run on the agents or slaves but all the slaves will be connected to the master node.

Note: All the Jobs or definitions will be on Master where Jenkins is installed and agents will be used to run these jobs.

Note: All Slave or agent nodes should have Java installed.

And these tools will work together in an infinite state and in an automated way, I mean continuous Integration and continuous deployment as below.

As we believe in hands-on instead of theory, Let's jump to the Terminal and create two servers one is the Master and the second one is an agent and will install Jenkins on the Master.

If you do know, How to create EC2 instances and how to Install Jenkins refer to my below blog.

Now I will connect my Master node to my Slave node and will create key for the same.

Below are the commands we need to run on the Master on /.ssh path

cd .ssh/
ssh-keygen

It will ask for a name, if you want to give a name you can give othrwise just press enter three times and the system will generate two files on the same location.

To see you can list the files.

ls

Open the id_rsa_pub with the help of cat command and copy it and paste it into your agent's authorized_key file refer below screenshots for the references.

Master:

Agent:

Now you Master and Agent can SSH.

Let's try on machine to connect with below commands.

ssh ubuntu@PUBLICIP_AGENT

I was able to connect and exit to get out from it.

Now we are ready for Jenkins.

Come to Jenkins and create Distributed build by clicking on Set up an agent as below.

Provide Agent name and click on create button.

Provide Description, Remote root directory as below

Go down and provide labels,Usage,Launch Method as below.

Note: Host IP is agent's IP, we will create a connection between the Agent and the Master node.

Now Let's create credentials and click on Add button>>Jenkins button.

Now I share my private key of Master node to Jenkins and will as below values from drop-downs.

Note: in the Private key you need to copy id_rsa file and paste here.

Just click on Add button and now you will see your connection details in the credentials drop-down and select Non Verifying Verification strategy as Host Key verification strategy then save it.

Note: We have not installed Java on our agent so we will install Java now because we cannot connect without Java.

After Installing Java on Agent now we are able to Make connection refer below.

Note: If your generatd key is not working then you can create new key with the help of below screenshot and paste private key in your Agent's authentication_key and private in your Jenkins credentials.

Now, Lets create CICD pipeline.

Come to Dashboard and Create a Job.

Provide Item Name and select Pipeline click Ok

Provide Descripion, Select GitHub project as below and provide your project's GitHub URL as below.

Select GitHub hook trigger for GITScm polling in Build Triggers.

Now, Let's write pipeline script.

pipeline {    //pipeline declaration

    agent { label 'Test-Agent' }   //agent declaration
    stages{     //stages declaration
        stage('code'){    //code stage
            steps {
                git url: 'https://github.com/velosadmin/react_django_demo_app.git', branch: 'main'

Project URL from your GitHub repo. 
            }
        }
         stage('build and test'){   //build and test stage declaration
            steps {
                echo 'build and testing'  
            }
        }
         stage('deploy'){  //deploy stage
            steps {
                echo 'deploying....'
            }
        }
    }
}

Now Save it.

You will get below screen.

Now go to GitHub and create Webhook.

You can refer my below blog to take reference to create Webhook.

https://saifali2017.hashnode.dev/creating-freestyle-jenkins-pipeline

After Webhook connection , you will get below screen on your Jenkins Dashboard.

Now, let's change something on our code at GitHub and commit then build will check if GITSCM Polling is triggering or not.

As you can see our CICD pipeline was created.

Now, Let's Jump to the Agent server and install docker and will check if the code is there or not.

See below got the code.

Install Docker on Agent.

Docker is installed, Will install Docker Compose.

Now, Build the project on docker, again jump to the pipeline script and make the below changes.

pipeline {
    agent { label 'Test-Agent' }  
    stages{
        stage('code'){
            steps {
                git url: 'https://github.com/velosadmin/react_django_demo_app.git', branch: 'main'
            }
        }
         stage('build and test'){
            steps {
                sh 'docker build . -t node-todo-app'
            }
        }
         stage('deploy'){
            steps {
                sh 'docker-compose down && docker-compose up -d'
            }
        }
    }
}

After these changes, Save it.

Your Pipeline isready and application is deployed.

Now, we will take one more extra step and we will create environment variable and will push image to dockerhub.

Go to Jenkins>>Manage Jenkins>>Available Plugins>>Search Envrionment Injector>>Select the Plugin and Install with Reboot.

After restart of your Jenkins you cann see installed Pug in Installed Plugin refer below.

Now , We need to create credentials.

Steps: Manage Jenkins>> Manage Credentials>> Global>>

\>>Add Credentials>>select Username with Password>> then provide DockerHub User name and password ,id and description.

Click on Create, You will get the below screen.

Now we will use these credentials in our pipeline script.

I am pasting pipiline script again here for your reference.

pipeline {
    agent { label 'Test-Agent' }  
    stages{
        stage('code'){
            steps {
                git url: 'https://github.com/velosadmin/react_django_demo_app.git', branch: 'main'
            }
        }
         stage('build and test'){
            steps {
                sh 'docker build . -t velosadmin/node-todo-app-cicd:latest'
            }
        }
        stage('login and push image'){
            steps {
                echo 'loggin into DockerHub and pushing Image...'
                withCredentials([usernamePassword(credentialsId:'dockerHub',passwordVariable:'dockerHubPassword',usernameVariable:'dockerHubUser')])
                {
                    sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPassword}"
                    sh "docker push velosadmin/node-todo-app-cicd:latest"
                }
            }
        }
         stage('deploy'){
            steps {
                sh 'docker-compose down && docker-compose up -d'
            }
        }
    }
}

and again biuld it.

Your Pipeline is ready.

Happy Learning

Saif Ali

More from this blog

Untitled Publication

22 posts