Jenkins (CI/CD) pipelines

Vishugoyal
13 min readMar 20, 2021

Session 1

Jenkins is a free and open source automation server. It helps automate the parts of software development related to building, testing, and deploying, facilitating continuous integration and continuous delivery. It is a server-based system that runs in servlet containers such as Apache Tomcat.

This tool is more about to use integrate the technology….
The manual thing to be operated is not good by the human beings(worst things)
* Lot of time taken..
* Lot of error come and make mistake by the human being.

Adv.. >> It automate the thing which helps a lot and make less the number of the error.

Track of the code :
First of all market tell what they need the resources according these dev. teams create the product. The doc. file was created which was send the developer team according to this software dev. team work then the code was pushed the github make some repo. there and create a check points and save our code then after the code was intergrate to the server then the tool was helps a lot like jenkin which automate the code and push to the server with use of the technology like jenkins and many more.

Then the one of the team was come on and check that code was working properly or not if there is any problem then they will send back to the dev team and this process was done till the work was done properly..

Session 2

INSTALLATION OF JENKINS:

https://pkg.jenkins.io/redhat-stable/
FOLLOW THE ABOVE LINK FOR THE COMMAND

First of all, you download the WinSCP to connect our OS.
https://winscp.net/eng/download.php for window
run the cmd
#ifconfig
it will provide you a IP address of the system
connect to winscp to the linux os
download java jdk and jenkins.rpm file
http://bit.ly/jenkins-drive
go the software section and download it

first of all the system or OS should be yum configured

If you follow the above 5 steps then this help you to a configur a yum command in your linux

$sudo wget -O /etc/yum.repos.d/jenkins.repo $https://pkg.jenkins.io/redhat-stable/jenkins.repo

$sudo rpm — import https://pkg.jenkins.io/redhat-stable/jenkins.io.key

use of putty gen : Convert .pem key to .ppk key format
Then use the cmd yum install jenkins

Now open the terminal see the ip and port number
$ifconfig enp0s3
$netstat -tnlp |grep java

$systemctl status Jenkins
$systemctl enable Jenkins
$systemctl disable firewalld

open firefox and put in the search bar
ip:port no
The interface was opened the copy the file name and paste the terminal
$cat filename
Then it will give a password of the jenkins for an instance

Then set a new password and name in the jenkin

………………..Now enjoy your jenkins plateform……………

Download a plugins :

Plugins are the primary means of enhancing the functionality of a Jenkins environment to suit organization- or user-specific needs. There are over a thousand different plugins which can be installed on a Jenkins controller and to integrate various build tools, cloud providers, analysis tools, and much more.

Go the dashboard and then click manage jenkins then manage plugins and then click to available section and search the github plugin and github
download it with restart or not

If the dev. team made a code file then this file was push to open source github profile then the connection between the github and the jenkins plays the plugins a lot

Session 3

Something about the triggers option in jenkins

In a very easy language we can say that job which is automatically do anything for ourself with some set of operations.
we will run a jobs manually in jenkins there is challenges facing without trigger i.e. human beings build the job manually. So challenges was resolved by the help of trigger. It has option to set the periodically run or build the job.

To create a new job or items

click the new item or create a new job.

Fill the item name and then click freestyle project and the click to the OK button.. and we use github plugin to intergrate our code with jenkins

Some about the github :

Create a account on github and also download the git bash and git
open the git bash

open the git bash

create a new repo in github and then clone this repo by the help of cmd in git bash.

click new add new repo.

fill the repository name , remember for starting the mode of the repository is public and click on the readme file and the click on the create repository..

Click the top middle right green box code.

Now you copy the link of the repository in //http mode only .
Now switch the git bash program.
Type the cmd
#git clone link-of-repository-you-copy-from-github

Now move to that folder in that you repo..

Now create new file.py

now you write the code in your file
press “i” to change the mode of the file in the insert mode then write the code and then press esc then type “:wq” to save the file

To use the #cat cmd to read the file

#git status
#git add my_code_file.py
#git commit -m “new code is added” my_code_file.py

Before pushing the code on the github
#git config user.name “your username
#git config user.password “your password

Now the time to push your code on the github profile

USE the cmd
#git push

Finally your code push on the github profile and added on the online open source tool

How to configure the web server

we have a private IP in our VM so that why other person can’t connect my web page that why we need a public IP. we go to the third party software like TUNNEL and VPN

This types of the program helps you a lot and provide a public IP which helps to connect the outer world with your webpage.

now we will do a practical with the product like ngrook

https://ngrok.com/download go to this link and download the software

lets try to build a small and basic job in jenkins

Without Triggers

Triggers :

Triggers helps a lot to run the job automatically means build the job
Types of triggers

  • Trigger build remotely (eg: from script)
  • build after other project are build
  • build periodically
  • github hooks trigger from gitscm polling
  • Poll Scm
You can see the build triggers option in job's -> Configure setting.

These are the most common Jenkins build triggers:

  • Trigger builds remotely
  • Build after other projects are built
  • Build periodically
  • GitHub hook trigger for GITScm polling
  • Poll SCM

1. Trigger builds remotely :

If you want to trigger your project built from anywhere anytime then you should select Trigger builds remotely option from the build triggers.

You’ll need to provide an authorization token in the form of a string so that only those who know it would be able to remotely trigger this project’s builds. This provides the predefined URL to invoke this trigger remotely.

predefined URL to trigger build remotely: JENKINS_URL/job/JobName/build?token=TOKEN_NAMEJENKINS_URL: the IP and PORT which the Jenkins server is running
TOKEN_NAME: You have provided while selecting this build trigger.
//Example:
http://e330c73d.ngrok.io/job/test/build?token=12345

Whenever you will hit this URL from anywhere you project build will start.

2. Build after other projects are built

If your project depends on another project build then you should select Build after other projects are built option from the build triggers.

In this, you must specify the project(Job) names in the Projects to watch field section and select one of the following options:

1. Trigger only if the build is stable
Note: A build is stable if it was built successfully and no publisher reports it as unstable2. Trigger even if the build is unstable
Note: A build is unstable if it was built successfully and one or more publishers report it unstable3. Trigger even if the build fails

After that, It starts watching the specified projects in the Projects to watch section.

Whenever the build of the specified project completes (either is stable, unstable or failed according to your selected option) then this project build invokes.

Build periodically:

If you want to schedule your project build periodically then you should select the Build periodically option from the build triggers.

You must specify the periodical duration of the project build in the scheduler field section

This field follows the syntax of cron (with minor differences). Specifically, each line consists of 5 fields separated by TAB or whitespace:

MINUTE HOUR DOM MONTH DOW

MINUTEMinutes within the hour (0–59)HOURThe hour of the day (0–23)DOMThe day of the month (1–31)MONTHThe month (1–12)DOWThe day of the week (0–7) where 0 and 7 are Sunday.

To specify multiple values for one field, the following operators are available. In the order of precedence,

  • * specifies all valid values
  • M-N specifies a range of values
  • M-N/X or */X steps by intervals of X through the specified range or whole valid range
  • A,B,...,Z enumerates multiple values

Examples:

# every fifteen minutes (perhaps at :07, :22, :37, :52)
H/15 * * * *
# every ten minutes in the first half of every hour (three times, perhaps at :04, :14, :24)
H(0-29)/10 * * * *
# once every two hours at 45 minutes past the hour starting at 9:45 AM and finishing at 3:45 PM every weekday.
45 9-16/2 * * 1-5
# once in every two hours slot between 9 AM and 5 PM every weekday (perhaps at 10:38 AM, 12:38 PM, 2:38 PM, 4:38 PM)
H H(9-16)/2 * * 1-5
# once a day on the 1st and 15th of every month except December
H H 1,15 1-11 *

After successfully scheduled the project build then the scheduler will invoke the build periodically according to your specified duration.

GitHub webhook trigger for GITScm polling:

A webhook is an HTTP callback, an HTTP POST that occurs when something happens through a simple event-notification via HTTP POST.

GitHub webhooks in Jenkins are used to trigger the build whenever a developer commits something to the branch.

Let’s see how to add build a webhook in GitHub and then add this webhook in Jenkins.

  1. Go to your project repository.
  2. Go to “settings” in the right corner.
  3. Click on “webhooks.”
  4. Click “Add webhooks.”
  5. Write the Payload URL as
http://e330c73d.ngrok.io/github-webhook
//This URL is a public URL where the Jenkins server is running

Here https://e330c73d.ngrok.io/ is the IP and port where my Jenkins is running.

If you are running Jenkins on localhost then writing https://localhost:8080/github-webhook/ will not work because Webhooks can only work with the public IP.

So if you want to make your localhost:8080 expose public then we can use some tools.

In this example, we used ngrok tool to expose my local address to the public.

To know more on how to add webhook in Jenkins pipeline, visit: https://blog.knoldus.com/opsinit-adding-a-github-webhook-in-jenkins-pipeline/

Poll SCM:

Poll SCM periodically polls the SCM to check whether changes were made (i.e. new commits) and builds the project if new commits were pushed since the last build.

You must schedule the polling duration in the scheduler field. Like we explained above in the Build periodically section. You can see the Build periodically section to know how to schedule.

After successfully scheduled, the scheduler polls the SCM according to your specified duration in scheduler field and builds the project if new commits were pushed since the last build.

Build periodically:
Under Build TriggersBuild periodicallySchedule you can create a schedule (or multiple schedules) for Jenkins to build periodically or on a specific date/time. It might be tests that should be run periodically (every morning for example) or a DB clean up Jenkins job or any other Jenkins job. In this mode we are facing some challenges like it always download the code when the schedule was run .

Now we created a project with trigger build periodically

Same steps as above but when we configure then in the trigger option should be clicked on the build periodically

Syntax of the schedule of the build periodically
min hours date month day
* * * * * means every one min
* 10 * * * means every 10 min

Poll Scm :
It is similar to build periodically but Poll SCM periodically polls the SCM to check whether changes were made (i.e. new commits) and builds the project if new commits where pushed since the last build, whereas build periodically builds the project periodically even if nothing has changed.But in this trigger we face some challenges i.e this trigger run always but download the code only when some changes take place. This is a save of time and source of CPU.

Now we created a job with the use of the plugin git
Steps already done do again to create a new job
* click on the new item in Dashboard
*Enter an item name
*click on the Freestyle project
*click ok

When you move on the configure page some steps
* Click the Source Code management tab
* if you already download the git plugin then the option was come under the source code management like NONE and GIT click on the git option..
* And further you fill you repository link in the url bar of the SCM under
Preinstall git plugin and also install git in your base os
by using yum install git

When you build now the job then the new history was created and in the workshop space have download all the file which was on your git repo.

Github hooks trigger from gitscm polling :
In Build Triggers select GitHub hook trigger for GITScm polling. When Jenkins will receive PUSH GitHub hook, it will trigger Git SCM polling logic which will start a new Jenkins build, with the updated code. We use ngrok to expose local jenkins to the internet, so that github can send the webhooks
It only download the code when some changes take place and build the job.

HOW TO RUN PYTHON JOB IN JENKINS

First of all, in your base OS have install the pip in which your jenkins run like your rhel8 and also install pyhton3 in your base OS

First install python 3 then install pip command in your base os

now you run a single command in your base os
pip install Appium-Python-Client

then in execute shell you write the cmd like
python3 -m unittest <python_file_name.py>

--

--

Vishugoyal

I am pursiing B-tech. and also very fond of learning new technology under the guidance of Vimal Daga sir (World Record Holder).