What is Jenkins ?
You had often get to hear about Jenkins, specially if you are aspiring DevOps. Or maybe you are hearing first time ! Anyways, Jenkins is a server for CI/CD (i.e Continuous Integration and Continuous Delivery/Deployment) in DevOps. It helps automating building, testing, and deployment of application by writing a script called Pipeline.
Why Jenkins is essential in DevOps ?
As we know, DevOps mindset was born to reduce efforts and Lack of coordination between Development and Operation teams. Therefore, we need something that solve this issues and make the process faster and reduce time to market.
Jenkins makes building, testing, and deploying application rapidly and efficiently by automating all these process through a pipeline syntax. This helps to find and fix issues faster, and speed up release ensuring high quality software with minimum manual efforts.
What is CI/CD, Pipeline ?
Continuous Integration (CI) :
This handles integration part like Developers integrate their code, Verifying all functionalities if all things are working properly or not through tests such as unit tests and integration tests. Automating these process is known as Continuous Integration.Continuous Delivery (CD) :
After Integration process, code is ready to deploy. If we manually do deployment by clicking the button, it is known as Continuous Delivery. This automate Building, Testing but requires manually deployment.Continuous Deployment (CD) :
If we automate deployment process also then it is known as Continuous Deployment. This automate all process from Building, Testing till Final Deployment.What is pipeline ?
Pipeline is a series of stages, where every stage has steps that perform specific tasks. Basically, It is a flow of building, testing and deploying code.There are 2 types of pipeline :
Scripted Pipeline :
Scripted pipeline means you write a syntax (i.e. Groovy Syntax) directly in Jenkins UI. These is more flexible way of writing pipeline. Its structure looks like this :
Declarative Pipeline :
Declarative pipeline means you create a dedicated file Jenkinsfile. It is more structured way of writing pipeline and uses yaml like structure, but it can also contain scripted syntax. Its basic structure looks like this :
Installation of Jenkins in Linux (Ubuntu) :
First you need to install java in your system as Jenkins can’t run without it
so go to your terminal and follow these commands
sudo apt update sudo apt install fontconfig openjdk-17-jre java -version
and you done with java installation.
Now, for jenkins follow these commands
sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \ https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key 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
Check if it is installed or not by
jenkins --version
and also check if it is running or not by
systemctl status jenkins
It should display active (running) in green as seen in image
You have done with installation.
Note :
If you can’t see active (running) like above image, it means you may not installed java or run command
sudo systemctl start jenkins && sudo systemctl enable jenkins
Jenkins don’t have Software Application that you can directly open from your computer. It is a server that runs through Web on port 8080, we will next see how to run it.
Run Jenkins
Check your IP address by
hostname -IJenkins Fundamentals - Part 1
It will give you list of active IP addresses, pick one of it eg.
173.17.0.1
and go to your browser and go tohttp://<your-ip-address>:8080
and your Jenkins should display.Or you can just simply go with
http://localhost:8080
this will also work if you are on local computer.If you are on AWS instance, then you need to add port 8080 to your inbound rules. To do this, go to your Instance → Instance ID → Security → Security groups → Edit inbound rules → Add rule → Give port range 8080, Select Source Anywhere-IPv4 → Save rules.
Now go to Instance → Instance ID → Copy your Public IPv4 address Eg.
13.232.184.153
and go to browserhttp://<your-ip-address>:8080
and your Jenkins will display.
Setup Jenkins
You can see that /var/lib/jenkins/secrets/initialAdminPassword
copy it, go to your terminal and sudo cat /var/lib/jenkins/secrets/initialAdminPassword
this will give a password.
This password you should paste here in Administrator password and press Continue.
Then you will able to see Interface like this, click on Install suggested plugins
It will start downloading required plugins, etc.
After it completes, you have to create User
Then you’ll see Jenkins interface like this
Here you are now completely ready to use Jenkins and make your deployment easy and efficiently.
Write Your First Pipeline
We will take a simple project and do practice making its build, test, and deploy through pipeline. If you see there is an Create a job button and in left New Item, both are same, we start by clicking on it.
You have to give name to your pipeline as per your wish, select item type Pipeline, and press OK
Then description if you want to give, and select GitHub Project and paste your project repository’s link. There are many other options as well, but for now we are just creating basic demo pipeline.
Scroll down and you’ll see Pipeline script writer, in this section you have to write your pipeline.
pipeline{} : whole pipeline syntax will be written in between these curly braces, it indicate pipeline syntax
agent : It declares on which agent (server) project will run, here i wrote any means I didn’t declared any specific server.
stages{} : stages contains different individual stage, like build stage, test stage, etc. All stage are declared in between stages{}
stage(‘…’){} : stage contain name of the stage and steps that perform specific tasks
steps{} : In steps, commands, scripts, are written For Eg. To build application image, we wrote
sh “docker build -t node-app .”
sh specify that its a shell command.
In this pipeline, we clone our project’s code from github in first stage(‘Code clone’). In second stage(‘Build’) we build docker image of the project. In third stage(‘Test’) we test the code by commands, but for now there is no test cases in our project so we just simply printed “This is a test stage”. In forth stage(‘Deploy’) we deployed the project by docker-compose. After saving pipeline click on build now
May be you didn’t understood some names (Eg. docker-compose, docker image, etc), don’t worry this is just to explain the basic syntax of pipeline.
If your project have no issues in any of stage, then it will successfully deploy on server and you’ll get to see stage view like this
If you aren’t able to see this stage view, you have to download a plugin for this named “Pipeline stage view”
Go to Dashboard → Manage Jenkins → Plugins → Available plugins → search for Pipeline: Stage View → select it and press Install → and Restart Jenkins → Done.
We will next see some more advanced concepts. Follow the blog and be ready with Jenkins setup.