0% found this document useful (0 votes)
74 views45 pages

Jenkins and Its Setup

Jenkins is a tool for continuous integration and continuous delivery. It automatically builds code uploaded to version control systems and notifies developers of the build status. Jenkins can be installed on its own or deployed on a servlet container like Tomcat. When deploying Jenkins on Tomcat, the Jenkins.war file is placed in Tomcat's webapps folder. The Jenkins home directory contains configuration, plugins, jobs, and logs. This directory can be changed by moving these files to a new location and updating the JENKINS_HOME environment variable. Jenkins actions can also be controlled via a command line interface for easier, faster, and more efficient automation of continuous integration tasks.

Uploaded by

Abid Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views45 pages

Jenkins and Its Setup

Jenkins is a tool for continuous integration and continuous delivery. It automatically builds code uploaded to version control systems and notifies developers of the build status. Jenkins can be installed on its own or deployed on a servlet container like Tomcat. When deploying Jenkins on Tomcat, the Jenkins.war file is placed in Tomcat's webapps folder. The Jenkins home directory contains configuration, plugins, jobs, and logs. This directory can be changed by moving these files to a new location and updating the JENKINS_HOME environment variable. Jenkins actions can also be controlled via a command line interface for easier, faster, and more efficient automation of continuous integration tasks.

Uploaded by

Abid Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 45

● 1: What is Jenkins and why do we use it?

Jenkins is a Java application used in continuous integration and continuous


delivery.
Example: When a developer develops a code and uploads it into any
version control ( Github, TS), Jenkins automatically takes the code creates
a build, and then sends the notification to the user that Is build has been
created successfully or failed.

● 2: How to download and Install Jenkins:


1. You can use this website for understanding
(https://www.blazemeter.com/blog/jenkins-tomcat)
2. Go to Google, type “Jenkins” and then download the latest stable
build according to your operating system. It is better to download the
(.war) file which is on top with the name ( Generic Java package).
3. After the installation, open your cmd/ terminal, and then open that
location where the ‘Jenkins. war’ file present. In my case the
download location is cd/C: User/Admin/Download.
4. After reaching out to the exact location run this command ( java -jar
jenkins. war).
5. Make sure that the .war file is available in the location where you
downloaded it and open it in the cmd prompt.
6. When you run the command by pressing the enter button, the
downloading will start, and in the end you will get the password which
will look like this:

7e9c08f7af454874b72167b97efe7ea3

● After that open the browser and go to (localhost:8080), then on the


first screen you have to enter the password which you get from the
cmd prompt to open the Jenkins page.
● Then the second screen will appear for “Plugins”, where the options
comes to install the suggested plugins automatically and 2nd is the
customized plugin.
I recommend that you select Download the suggested plugins and then
click on Install, or the installation will automatically start
● The next screen appears where you have to set credentials or you
can start by clicking on the ‘Skip’ button.

● 3: Why should we deploy Jenkins on Tomcat?


We know that we can start Jenkins on its own standalone server and
Jenkins comes with its own servlet container which is ‘Jetty and Winstone’.
We can also deploy the Jenkins on other sublet containers like Tomcat.

Why do we use Tomcat? A big advantage of Tomcat is we can start/run


our application on a single server. We can do it also on Jenkins standalone
but here we are going to start with Tomcat!
We have also other options like
• Glassfish
• Tomcat
• JBoss
• IBM WebSphere
• Jetty
• Jonas
• Weblogic
• Apache Geronimo 3.0
• Liberty profile

We can apply Jenkins on any of these servlet containers. We use Tomcat


because it very famous servlet container

Prerequisites for installation of Tomcat:


● Tomcat ( above or version 5 and best is tomcat 9 ) Download and
install it from Google by typing “Apache Tomcat”, according to your
system specification
● Java (Java 7 or above ) should be installed on your computer.
● After unzipping the folder, Copy/Place the Jenkins. war file inside the
tomcat/webapps folder.
● To launch the tomcat you have to open cmd and go to the tomcat bin
folder
cd/User/Admin/ Downloads/apache-tomcat-10.1.13/bin
Then we have to make a file executable by running ( chmod +x *.sh)
in the cmd prompt
If chmod is not initiating in the cmd prompt then we don’t need to
make files executable.
● The next step is to start Tomcat by using (startup .sh), Make sure that
you are in the bin folder, and if you want to shut down Tomcat then
use (shutdown .sh)
● To check both Jenkins standalone and Jenkins on Tomcat
standalone are working the same. We can create items/jobs on both
1 is on Jenkins and one is on Tomcat Jenkins without any data, If
both items/jobs are displayed on both then it means it is working the
same. (It is not necessary to check, both work the same)
● If you want to run only Jenkins while Tomcat is also running then the
address to run Jenkins will be ( localhost:8081 ) and then the
command will be ( java -jar jenkins.war --httpPort=8081 ). Because
the ‘8080’ port is already in use, we have to change the port number.
● If you face any issue regarding starting Tomcat on a different port, if it
still shows any error and does not start. Then go to the Tomcat bin
folder open/run the `tomcat9` file and allow the permissions. The file
color will be reddish/red
● If we want to change the address of the Tomcat then go to the
Tomcat config folder, open the server file, and search for ‘Connector
Port” in the text of the server file, for the search dialog box Press
Control + F
● While adding a new user from the Tomcat config folder user file,
Make sure that add the “manager gui” in the role and the script
should be outside the comment at the end before the tomcat user text
like given:

● 4: How to Change the Home Directory in Jenkins?


Whenever we deploy Jenkins on any system, there is a folder that is
created by default and most of the time the name of the folder is ‘.jenkins’
and the default location is the user profile in the system.
This folder contains all the information on the logs, configuration, jobs, and
everything about Jenkins.
Jenkins Home Directory contains:
1. All configurations
2. Plugins
3. Jobs details
4. Logs

Why do we change?
-To move Jenkins home dir to a location that has enough disk space.
Because it was installed in the User Admin directory, and a lot of jobs, and
plugins, configurations the space becomes full in the C drive, and we have
to move it to another location to free up the space.
- Another reason for this is Project requirements.

Step 1: First go to the ‘Jenkins.war’ file in your Home directory and start
the Jenkins.war by the command ( java -jar jenkins.war ).
Then go to the browser and open the local host ( localhost:8080 ), Here we
did not run Jenkins on Tomcat.

Step 2: When the Jenkins dashboard window appears, then click on


‘Manage Jenkins’ from the left side menu. Then click on ‘Systems’ when
the Manage Jenkins window displays.

Step 3: Create a new folder where your Jenkins.war file is present and
then copy/cut/move all the data present in the (.jenkins) folder and paste it
into the folder that you created.

Step 4: After pasting/ moving everything, the next step is to Change the
env variable ‘Jenkins_Home’ path from the System environment variable
setting. If you don’t have ‘Jenkins_Home’ then create it under the system
variable and set the path to the new directory/ dir where you move the files.
If you are on Mac then after creating and moving files into the new directory
open the terminal and run this command
(exportJENKINS_HOME=/Users/Admin/Desktop/Tools/Jenkins/JenkisHom
e/ )

Step 5: Restart the Jenkins. For this go to that location by command


prompt where the Jenkins.war file present and then press ( Control +C ),
the Jenkis standalone server will stop and then restart the Jenkins from the
command prompt by using the ( java -jar jenkins.war ) command.
The other way to restart it, go to the browser where Jenkins is open, just
write this text above localhost:8080 ( /restart ), then the address will look
like this ( localhost:8080/restart ), and press enter.

Step 6: After restarting the Jenkins server, again go to ( Manage Jenkins >
System) and check whether the Home directory has changed or not. In
Windows, it changes easily but in Mac, if it does not change, then go to the
directory where all the Jenkins files present mean ( .jenkins), and in that
folder (.bash profile) folder will appear when the hidden files option is
enabled and hidden files are shown. Open that file (.bash profile) and enter
the text
(exportJENKINS_HOME=/Users/Admin/Desktop/Tools/Jenkins/JenkisHom
e/ ) and save it
After that restart the Jenkins again by browser and by terminal/cmd again,
then this issue will resolve

Step 6: We can also verify this by opening (/systeminfo) in the address and
then the full address looks like this ( localhost:8080/systemInfo ) and then
select ‘Environment variable’ from the above options then there is all the
variables will appear. We can also check it manually, this way (Open
Jenkins on localhost:8080 > Jenkins Manage > scroll down Click on
System Info > Select Environment variable from the top, and then you will
get all the options there. We can also check the plugin details from there.

● 5: How to use CLI (command line interface)?


In this tutorial, we will see how to use CLI ( command line ) to control the
Jenkins actions without looking into UI.

Why?
The question is why do we use the command line, even though we can do
everything through Jenkins UI to do all actions?
The reason is
1: Easier
2: Faster
3: Memory management (efficient -because it did not use UI)
4: Easy to do Continuous Integration.

Always remember that whenever you are starting to learn any tool, try to
use it through the command line, because it is best practice and the above
reasons are solved.

Step 1: Run Jenkins.war from cmd ( java -jar jenkins.war) and also on the
browser ( localhost:8080)

Step 2: Go to ‘ Jenkins cli’ in the Manage Jenkins window or enter the ‘/cli/’
above the main Jenkins URL, in my case, the URL of cli looks like this
(http://localhost:8081/cli/).

Step 3: Download the ‘jenkins-cli.jar’ by clicking on this button under the


‘Jenkins CLI’ section at - the start of the page.

Step 4: After downloading the ‘jar’ file, go to the location from the
command line where you downloaded it ( C:/User/Admin/Download ), and
run this command ( java -jar jenkins-cli.jar -s http://localhost:8080/ help).
You will get this command from the ‘Jenkins cli’ window where you are at
the start of the content. Example is given ->

Step 5: After this, we can open the Jenkins jar in the cmd line. For this go
to your browser where (localhost:8080) is running, and click on the user
profile from the header bar at Jenkins dashboard. Select ‘Configure’ from
the dropdown. Then scroll down to API token, click on ‘Add new token and
then click ‘Generate and copy the API token address
2nd step- Go to the location through the cmd line where you have
downloaded the ‘Jenkins-cli.jar file’ and run the command
(java -jar jenkins-cli.jar -s http://localhost:8080/ -auth
USERNAME:API_TOKEN help). Then hit the enter button.

Never forget to replace the USERNAME with your user name and API
token with that token which you copy from the profile Configure panel on
the Jenkins dashboard
If the API token is already generated and you cannot see it. Then delete
the token and regenerate it.
In my case, the link looks like this:
(java -jar jenkins-cli.jar -s http://localhost:8080/ -auth
zain :1147e50c754baabd3b3dedfd3b69641809 help)

By pressing enter after entering the above auth command you will get
everything on the cmd line like the function performed through the
command. You can also get this all cmd line on the (localhost:8080/cli)
page. It will look like this:
Available Commands are those shown in the cmd line

We have different commands here like restart, shutdown, stop-build, etc.


Remember that to use any command line you have to use auth
( USERNAME and API token) in your commands, otherwise, you can never
perform any action.
Restart the Jenkins through the command line: java -jar jenkins-cli.jar -s
http://localhost:8080/ -auth zain:114c7859aef88c9a1c78e0600e860328a6
safe restart
In Windows, this is not allowed to restart in this way, because for this we
need services running in the window but here we run it through cmd and
with .war file. So to restart that go to the location where Jenkins.war is
present and there we have to run ( Control + C ) to close and re-launch this
with the help of ( java -jar jenkins-cli.jar -s http://localhost:8080/ -auth
zain:114c7859aef88c9a1c78e0600e860328a6 ) from the cmd.

● 6: How to create Users + Manage + Assign Roles


Step 1: How to create a New User?
Start Jenkins.war> Go to Manage Jenkins when Jenkins starts at
‘localhost:8080’ > Scroll down to ‘Users’ and open it> Click on ‘Create a
User’ > Then enter the new user details and click Create a User.
After creating the user you can log in with a valid email and password.

Step 2: How to configure the User’s role?


For the configuration of any user, Click on the profile from the top right
corner of the Jenkins dashboard > Then there on that page, we can
configure any user like name, password, and API.

Step 3: How to create and manage the User’s role?


Descrip: The role is very important when creating a new user. If we do not
assign a role, the security will break, and it may cause data leakage. so, it
is better that every user can configure only that role which is assigned to
him/her.
For example, The Developer can see only dev projects, Manager can see
all projects, Tester can view only Testing projects.

For this process, we have to download “Roles Strategy Plugin” from


Google or navigate to this URL (https://plugins.jenkins.io/role-strategy/)
and then click on the ‘How to Install’ button from the top right corner.

Ways of Download: 1st you can download the file from the “Role strategy
Plugin” website main page under the ‘How to Install’ button and then put
this file into the .jenkins folder which is the main Jenkins folder > Plugin
folder and paste it there, and then restart your Jenkins.war again.
In my case, the address is:
C:\Users\Admin\.jenkins\plugins
2nd way is to download is to go to localhost8080 Jenkins UI and then > Go
to Manage Jenkins > Plugins > Select Available Jenkins > Then search for
“Role Strategy ” >Select which you want to Install > Click on Install
How to use ‘Role Strategy Plugin’?
After downloading and restarting Jenkins > go to Manage Jenkins>
Security > Scroll to the Authorization section and click on the dropdown,
there you will find the ‘Role Based Strategy’. Then select ‘Role Based
Strategy’ and Apply and then save the setting.

Then go to user and create a new user and remember credentials for
login> By saving and applying the “Role Based Strategy” setting from
Security the “Manage Roles” option will appear in the Manage Jenkins
window screen > Open Manage role setting> Then there is Global, item
and Agent role there> Setting up according to your needs
For more info watch the given video
https://youtu.be/QvFungzXI5s?si=g_4Wi0BFndcVUpft

● 7: Basic Configuration:
In this section, we will look into all common and useful options in the
Jenkins system setting that we can use.
For this go to Manage Jenkins > System

● Home Directory:
The home directory tells us where our Jenkins files are present in the
system/pc. If you want to change the Home directory then scroll up and
read the description.
● System Message:
The system message will help us to show any message on the dashboard
screen to notify your user/ employee. Write the message and then save
and apply it, the message will appear on the dashboard. The one awesome
thing here we can use HTML tags to change the appearance of display text
on the dashboard
Like

If
HTML
does
not
apply then go to Manage Jenkins > Security > Scroll to Markup Formatter
and change it to plaintext to HTML, then you can use this feature.
● # of executor:
The number of executors shows how many jobs run at one time. For
example, if we set 5 values here now 5 jobs will run on time.
● Labels:
Labels are basically used for Windows nodes /Windows connection.
● Quiet Period:
Quiet period is the number of seconds a particular Jenkins instance should
wait before triggering a job.
For example, your job is auto-schedule to run at some particular time or the
job can be triggered as soon as the build takes place.
If you go to job configuration then there you will see build triggers there. If I
set the 0 Quiet Period then the job will trigger as soon as there is a trigger
coming to Jenkins. So there is some time takes to commute all the files.
And we set some time period.
● SCM checkout retry count:
We see the option while creating a new project in the Build trigger section
of “Pool SCM” and also see this option in the “System” at the Manage
Jenkins screen.
“It tries to connect to the source code repository and try to get the latest
build information. It means if we fail what will the maximum try to recount
our Jenkins should do”
● Restrict Project Naming:
Restrict Project Naming remains Default. If I select the “Pattern” “Role
Based Strategy” option from its dropdown then it means the Project name
will be restricted as my selected option while creating the new Project.
For example, I select the “Pattern” option and set the configuration ( set
name preference and description and then go to the Project creating
screen. Now it will show the error if I set another name instead of the name
that I use in Restrict Project Naming’s Pattern option. But while using the
Pattern option don’t forget to set the name preference like this Test.*, then
it will work.

● Global Properties:
Setup Global properties are values that can be used for all jobs in Jenkins.
We can also set the tool's location there.
● Jenkins Location:
This section comes before Global Properties and here we can set the
Jenkins server address and many more.
● Getting Started with Jobs:

We will learn about:


1. How to create a basic JOB in Jenkins
2. Basic job configurations
2. How to run the Job remotely
4. How to chain Job Execution

1: How to create basic JOB in Jenkins:

For this go to New item from Dashboard screen main menu > Then set the
name and select ‘Freestyle or another option and click on OK ( I am using
freestyle) > Add description and you can also use HTML tags here.
The other option is there is ‘Source code Management’. We need some
plugins for this option.
The other option there is ‘Build Triggers’.
Build trigger has many types, which we will discuss later, and here we will
take a look at ‘Build periodically’. Building periodically means if we set time
in it then the build will scheduled at that time.
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 *
If we put five stars * * * * * with space in it then it means it will schedule for
every 1 minute.

The next step is “Build Steps” by selecting ‘Build Environment, For MacoS
use ‘Execute Shell’ and for Windows set the command in ‘Execute Window
command shell’
The next option in ‘Build Environment’ is Post-build Actions. This is a very
important step because after we create a build we have to do something in
the next, So here we select the option that what will be the next.
Then press Apply and Save, Our item will be created. Now go to the
dashboard screen and then you can build by clicking on the continue button
on the project name.

2: How to run the Job remotely:

To trigger the job remotely, Go to the item configuration that is already


created or create a new one > Then select the Build trigger from the side
menu or scroll down to it > Then select “Trigger builds remotely (e.g., from scripts)”
and then an Address will appear there you can copy it. Then open a new
tab then paste that link but change the ‘JENKINS_URL’ with your address and
remain the same, and change the token name with that which you give in
the Authentication text box - the address will look like this:
http://localhost:8080/job/Test/build?token=1234

Remember that changing the token name works like a password we have
to set it and then paste it with remote URL.

4: How to chain Job Execution:


Creating a chain in Jenkins job build means after each build the next build
will start to build. For this after creating a job with freestyle or with some
other option go to job configuration and then select ‘Source code
Management’ from the left side menu and then you will navigate to the
Build Triggers section > We can also navigate to it by scrolling down in
Configure.
Then in the ‘Build triggers’ section > Select ‘Build after other projects are
built and then we have to set the next item/ or job that we want to run after
this job/item. And then select ‘Trigger only if build is stable’ or another option
according to your need. Then scroll down to the end at the ‘Post-build
Actions’ section and then select ‘Build other projects’ from the dropdown
and then save and apply.

Note: Chain job execution is only applicable when there is more than 1
item/job created and we want to execute the next build after running the
one job/item completely and successfully.
For example, I have multiple items/jobs with the names ‘Test1’, ‘Test2’,
and ‘Test3’, now I want that if Test1 is successfully built/executed the Test2
execution should be started and once Test2 successfully completed then
Test3 execution should be started automatically. This is chain job
execution.

● 8: Jenkins integration with GIT (SCM)

Today we will learn


1. Create a Java program and run it through the command line
2. Create a Jenkins job to run the Java program
3. Add this program/project to Git
4. Jenkins - add git plugin
5. Configure Jenkins job to trigger the execution when a change is pushed
to GitHub
1: Create a Java program and run it through the command line:
Here we create a sample of a Java program and run it through the
command line, but we can use our program and run it.
We can use any editor like IDE ( Eclipse, IntelliJ ) or a simple Notepad.
First, write any Java code and save it in the (.java) file extension. Here I
write a code that will print ‘Hello this is a java project’ 10 times:

public class Hello{


public static void main(String[] args){
for(int i=1;i<=10;i++){
System.out.println("Hello this is a java project ... "+i);
}
}

Note: The class name and the file name that you saved should be the
same, otherwise it will show an error.
Then, Save the file on any location the open command line prompt for
Windows and shell for macOS.
Then go to the place where the file is saved through cmd. After reaching to
the file location run this command
(javac here put the name of the java file which you save)
In my case, the script looks like this ( javac hello.java).
After compiling the Java file, the next step is to run the file, To do this run
this command ( java and just file name ) in my case, this is like this ( java
hello), without extension name.
Remember that it will not run if you did not compile this before running.
The cmd scripts and output look like this:
C:\Users\Admin\Desktop>javac hello.java

C:\Users\Admin\Desktop>java hello
Hello this is a java project ... 1
Hello this is a java project ... 2
Hello this is a java project ... 3
Hello this is a java project ... 4
Hello this is a java project ... 5
Hello this is a java project ... 6
Hello this is a java project ... 7
Hello this is a java project ... 8
Hello this is a java project ... 9
Hello this is a java project ... 10
If you write your code in any IDE ( Eclipse, IntelliJ, Visual Studio) then go to
the place where the file is saved and run the same command again in the
cmd prompt.

2. Create a Jenkins job to run the Java program:


First, create a new job/item of freestyle > From the project configuration
page, scroll down to “Build Steps” or select ‘Build Environment’ from the
left side menu, and then you will redirect to ‘Build Steps’ > Select ‘Execute
Window Batch file’ from the dropdown for Window and shell for macOS.
Paste the given command according to your Java file location.
In my case, the location is this and all the other script remains the same.

C:\Users\Admin>cd Desktop

C:\Users\Admin\Desktop>javac hello.java

C:\Users\Admin\Desktop>java hello

Then apply and save. After that, perform the ‘Build Now’ functionality.
Note: Don’t forget to set up Maven and JAVA in Jenkins for this
go to Manage Jenkins > Tools> Scroll down to Java and set-up
then scroll down to Maven and setup, try to set it up manually
instead of Automatically.
Remember that: If you get an issue that the build failed, then
make sure you set the JAVA and MAVEN environment from the
Tools setting> The next thing to do is make sure you set the right
value in Execute Windows batch command at job/item configure page. The path
value should start with “cd”. In my case, it looks like this:
cd "C:\Users\Admin\Desktop"
javac hello.java
java hello

3. Add this program/project to Git/GitHub:


It is best practice to learn git commands. Then make sure your Java files
are in the folder, so we can initiate it combine/ initiate the complete folder.
For example: After executing the java file with the ‘javac command’ it will
create a new file that actually executes. Then add/move both files to one
folder
After that go to the folder location where the Java files are saved through
the cmd prompt.
Then first run the git command ( git init )
Then check the status of the files through the command ( git status )
Then add the folder to git run ( git add .)
After that check the status of the file again through ( git status ), you will
see the file name color will change
Then you have to commit the file to git through the command
( git commit -m “add any message/text here” )
After we have pushed our code to the GitHub repository, for that opens
your GitHub on Chrome.
Then create a new repository on GitHub, For that, click on new from the left
side menu at the dashboard window. Provide name and set security of
Public/private + Check the box of ‘Add Readme’ for a better approach then
scroll down and create the repository. Once the repository is created, click
on ‘Code’ to get the address of SSH. Copy it. Once you copy the remote
address of the repository. ( if you did not see any SSH key then watch the
video on how to create and set up an SSH key:
https://youtu.be/jfi9n4y-WFo?si=xpFSTt8XZR49Qq_1

After copying the address open the minimized command prompt and then
run this command
( git remote add origin [email protected]:ZainUlAbdeen821/Helloworld.git )

Note: git remote add origin will remain the same for all but the next SSH
address will change according to your created repositories.

Then run the command to push the code ( git push -u origin master ), the
code has been pushed successfully, to check and confirm this go to your
GitHub account and then go to the repository, and then change the branch
to ‘master’ because we have write master in push command. Here you will
see 2 branches one is main and one is master, master is created by us.
We can write any branch in the push command that we want to create in
our repository.

4. Jenkins - add git plugin:


First, check that Jenkins is installed on your Jenkins browser or not if not
then install it for this:
Run Jenkins server from your cmd prompt then open the Jenkins UI
dashboard > Go to Manage Jenkins > Plugins > Select Installed Plugins
option from top > Then check for Git Plugin > if available then OK if not
then go to Available and install git plugin for your Jenkins. Mostly it is
installed in the new Jenkins version.
After checking/ installing the Git plugin, go back to the Dashboard window,
select the item/job that you want to integrate with Git, and then open the
configure screen. Once the configure screen appears, Scroll down to
‘ Source Code Management ‘ section or select from the left side menu.
Then in the section, you will see the Git option. The Git option only displays
when you have the Git plugin installed in your Jenkins.

Then click/select Git from the ‘ Source Code Management’ section. When
you select it will ask you to provide your Repository address. Here you
have to provide the Repository SSH address. After entering the Repository
address scroll down in the Configure screen to the ‘Build Triggers section’
then we have to select ‘Poll SCM’ and set the time for the scheduled
execution. Here I set ( * * * * * ) 5 start which is not good practice but I am
just doing it to learn. Then after this ‘Apply and Save the changes:

Remember here we do 2 changes 1: Add SSH address in the ‘Source


Code Management ‘ section and select and set the ‘Poll SCM’ from the
‘ Build Trigger section ‘

Now all the things are set up, Now no new build will execute on Jenkins
yet, it will execute when we will do any changes then it will execute.
To check, all of this Open the cmd prompt of your PC/machine > Go to the
same folder location where the before file existed ( hello world.java + one
more ) through cmd > Now create any new file through this ( dir >
readme.txt ) then in my case whole command looks like this
( C:\Users\Admin\Desktop\git java>dir > readme.txt )

The new txt file will created now for more checking create one more file
with html extension ( dir > index.html ), with this new HTML file will be
created in the same folder.

Now to check the changes run the ( git status ) cmd in the same location to
check whether both files are there or not.
Then the files are displayed in ‘red’ color which means they are not added.
So, to add these run the cmd ( git add .)
Then check the status again with this cmd ( git status )
After that when files are added to git the color of files becomes ‘green’.

After checking the status, we have to commit these files on Git to do this,
run this cmd ( git commit -m “any message for note ” )
Then push the new files by this command (git push -u origin master ) then
for confirmation open the Jenkins build screen then you will see that the
new build execution appears there.

● 9: How to add GitHub credentials in Jenkins


First, check that the plugin with the name “Credentials Plugin” is
listed/available in the Installed section at Jenkins, If not, then download and
install it from the available section.
Then go to Manage Jenkins> Credentials > Click on System > Click on
Global credentials ( unrestricted ) when the next page appears > Then click
on ‘Add credentials ‘ from the top when the next screen appears> Now fill
your Github credential > Leave the ID section empty it will auto-generated >
You can give any description in the description section. Now finally it's set
up successfully.
Now to check whether it is working fine or not, create a new job/ with
freestyle then go to ‘Source code management ’ from an item/job configure
window / Then select ‘Git’ and add Git repository SSH address > Then click
on ‘Credentials’ dropdown where you will the credentials name which you
add.
We can also credentials by clicking on the ‘Add button ’

If you want to clone everything on your PC > Then create a folder in any
location then copy the location of the folder that you created on your PC >
Now open your Jenkins > Create or open the existing created item/job >
Open the item/job configuration > Go to General section by clicking/
scrolling > In General section > Scroll down to advance > Then select the
‘Use Custom workspace’ and then paste the folder path address in
‘Directory’ and name of your folder in ‘Display name’
Reference:

● 10: How to use Catlight (Jenkins build Monitor)


Catlight will notify you when builds, bugs & tasks need your attention.
Web: https://catlight.io/
When we are working with multiple jobs, it is difficult to go on the Jenkins
dashboard and check all jobs one by one jobs/items console. So, Catlight is
the best choice to get notified of every action. It also works with other tools
which we can see on its website.
To Install this, open the website and download it according to your
operating system.
After downloading and Installing open it.
Click on ‘Start Configuration ‘ then select Jenkins from the option
After that paste the Jenkins server address ( In my case it is -
http://localhost:8080/ ). Then put the login credentials of Jenkins > Then
click on connect. After successfully connecting it will show your jobs/items
list there.
Then from the list, we can select any of the items/jobs that we want to
monitor or we can select all.
Then, you will get every notification on your desktop of your Jenkins
builds/items. If you need any help regarding Catlight then go to the Catlight
website and click on support we can also read the blog.

● 11: What is Automated Deployment (Step by Step)


Automated Deployment is the process of Automating the deployment
process in a Continuous Delivery system:
Build > Deploy > Test > Release
It means this is a chain. Once the ‘Build’ is completed the ‘Deploy’ process
starts, and after the Deploy process next is the ‘Test’, once the Test is
completed the product ‘Release’ process starts

First, start the Jenkins from the cmd prompt and browser.
Second, download the ‘Deploy to container ’ plugin from the Jenkins Plugin
setting(which is present in Manage Jenkins). Then after selecting the
plugin make sure to select the option ( Install after restart ) and then restart
the Jenkins from cmd as well as from the browser.
Third, we have to create a Job/item in Jenkin with Freestyle project
( Jenkins dashboard > New item > enter the name of your item > select
Freestyle project and then create/ OK)
For example, we can download the sample Jenkins.war from Google or
through the given link to test the continuous delivery :
https://tomcat.apache.org/tomcat-7.0-doc/appdev/sample/

Fourth, we have to ‘Add Post-build Action’ which will deploy the war/ear
files to the container ( add values to the field ). For this after creating a
Freestyle item, scroll down to ‘Build Steps’ and then select the
Shell/window batch command according to your operating system. Then
give any command (e.g. date). After giving the command, scroll down to
‘Post-build Actions’ and select ‘Deploy war/ear to a container’ from its
dropdown.
Note: The ‘Deploy war/ear to container’ option only comes when you install
the Deploy to container plugin completely.

Then, we have to enter the file name like **/*war which can be copied by
clicking on ‘?’

Then here we have to make some changes because we are using the
‘sample.war’ file to test then we have to add it in the workspace so that it
can be used. For this, save to progress on the item configuration setting
then go to your ‘item location’ in your system. Follow this location or we
can also get the Jenkins folder information by going to ( Manage Jenkins >
System ) at the top Home directory section.

Then we have to move our ‘sample.war’ file to our workspace’s project


folder. For this, copy the sample.war file then go to the location ( Jenkins
folder > workspace > In the same item folder that you configure for
continuous delivery and then paste the sample.war file there )
Note: We are doing the war file copy-paste scenario for the sample
project. We will not do this when we are working on the original project.
After adding the file, go to your project from Jenkins GUI browser > Open
the Config window of the project then scroll down to ‘Post-build action’
And then from there, you have to choose a container by clicking on the
container dropdown.
Choose “Tomcat” the latest/second last remote version from the container
dropdown.
Note: The Tomcat is already installed, the document is written above.
Fifth, We have to add user credentials in tomcat-user.xml, for this go to the
location of Tomcat:
Open your tomcat folder > conf > tomcat-users XML.
Now open this XML file with any editor and then we can use the existing
user or add a new user. Scroll down to the first user and then for using the
existing one edit them. if you want to add new then copy the existing script
and paste and edit the pasted file. Like this:
Here I added a new user with the name and password “deployer”.
Make sure that add the “manager GUI” in the role and the script should be
outside the comment at the end before the tomcat user text.
Then start the Tomcat from cmd and close the general localhost8080 from
the browser.
Go to the Tomcat bin folder through the cmd prompt and run (startup .sh)
to run the Tomcat. In my case the location is

C:\Users\Admin>cd Downloads

C:\Users\Admin\Downloads>cd apache-tomcat-9.0.80

C:\Users\Admin\Downloads\apache-tomcat-9.0.80>cd bin

C:\Users\Admin\Downloads\apache-tomcat-9.0.80\bin>startup .bat

Make sure that the Jenkins and Tomcat are running on different ports
otherwise, it will not work. Tomcat always uses the 8080 port so, change
the port address of Jenkins.war
If we want to change the address of the Tomcat then go to the Tomcat
config folder, open the server file, and search for ‘Connector Port” in the
text of the server file, for the search dialog box Press Control + F
After all, the process continues the Job/item configuration again. Open the
job again in which we select ‘Tomcat latest version’ and then give the user
configuration (Click on the Add+ button > Select Jenkins from drop down,
then a new dialog box will appear. Add a Tomcat Username and password
there. Add some description and leave the ID section empty, then save it).
After saving this will look like, and dont’t to add the URL of the Tomcat
server there!

And then click on Apply and Save.


Then click on Build Now to start the delivery. If you face any issues and are
not able to deploy the sample.war file check console of the build.

Note: if you face any issue with the window batch command in the Job
configuration then remove the command like we add ‘’date”. Remember
that the file that you want to upload should be available in the Jobs
repository ( for example we have added a sample.war file for checking,
then we have to add this file in the Jenkins Jobs folder and in that folder
which we want to deploy like I want to deploy in the ‘continuesdelivery’
item/job section, so I have the file in that job/item location.
Now if you face any issue with Tomcat users in deploying and get logs
about user's permissions, then go to
Tomcat folder > conf> tomcat-user file and edit this > Then change the user
role that we have added ( if it is at manager-script then change it into
manager-gui)

● 12: Notifications - How to send Email from


Jenkins:

To get the email setting go to manage Jenkins> System > Scroll down to
‘Email Notification’ section at the end.
Now here we have to add Gmail/ other email server addresses in the
‘SMTP’ server’ text box.
We use mostly Gmail for notifications so to find the SMTP server address
of Gmail, go to this web: (www.arclab.com/en/kb/email/list-of-smtp-and-
pop3-servers-mailserver-list.html)
Then here you will find all the email service provided’s SMTP addresses.
Here we will use the Gmail SMTP address. Copy this > open the Jenkins
Email notification setting, and paste the SMTP address in the SMTP server
text box. Then click on the ‘Advance’ button here first check and tick these
boxes “ Use SMTP Authentication and Use SSL”. We use SSL in the case
of Gmail.

Gmail Setting for Jenkins Email Notification( Do first before Jenkins


Email setup)
Open your account setting which is used for the Jenkins notification. Then
make sure that which account is used for Email notification its 2-step
verification should be on. Because when 2-step verification is on then you
can see the ‘App Password’ option.
Open the search bar on the Dashboard of the Google account and Search
for ‘App password’ then create a name and then the password will appear
and you have to copy this password.

Continue Jenkins:
Then in the user name enter the ‘Gmail’ address
like ([email protected])
In the password section ( add that password which you copy from the App
password)
Then in SMTP Port ( go to the above link where we copy the SMTP server
of gmail there you will find also the port number. Basically, Gmail uses the
‘465’ port number. This port number is also mentioned on the above web
link.
Then we can also add an email under the test email text box, for that, we
want to check whether it is working fine or not. Or send an email or not.
If you get email on Test email then it means it is working fine.

After the implementation of the Email notification scenario, we can set it to


any job for getting email. To apply at any Job/item:
Open Job/item configuration > Scroll down to end at “Add post-build action” and
there select ‘Email Notification’. We can select the given options that which
email you want to receive on our email from Jenkins.

There are also some other Plugins that are used for some custom Emails.
For more information about them visit the given video and watch it from
2:55
https://youtu.be/DULs4Wq4xMg?si=y5Fouq-fqKHRiPTi

● 13: What is Pipeline in Jenkins (Deveops)?


A pipeline is a workflow with a group of events or jobs that are
chained and integrated with each other in sequence. Every job in a
pipeline has some dependency on one or more other jobs

Every pipeline consists of these major stages:

Every stage has some item/jobs integrated with each other and work
with sequence, and we say it continues delivery pipeline.

All the systems work together in a sequence in an integrated build


like the above picture is a continuous delivery pipeline.

● 14: How to set up DELIVERY PIPELINE in


Jenkins?
For the delivery pipeline, we must first trigger our jobs in a
sequence( upstream and downstream jobs ).
For this, we have to create three Freestyle jobs with some Execute
shell commands for Mac and Execute window batch commands for
Window from the Builld Steps section (one is leading and 2 others are
upstream and downstream jobs). The Execute shell command looks
like the given image, and we have to do it for all three jobs with some
different echo text. So, we can indicate them easily.
1st Item/Job: I created 1st FreeStyle job with the name
“SampleBuild” and added the Execute window batch command (
@echo off date /t echo "Our Sample Job"”) and saved
2nd item/Job: I created a 2nd FreeStyle job with the name of
“SampleDeployJob” and added the Execute window batch command
( @echo off date /t echo "Deploy Job successfully"”) and saved
3rd item/job: I created a 3rd FreeStyle job with the name of
“SameTestJob” and added Execute window batch command @echo
off date /t echo "Test Job completed successfully" )

After creating jobs reach them one by one and run the jobs to check
whether it is working or building up fine or not.

After checking the jobs/items we have to chain our jobs in sequence


now.
To do this go to our 2nd job which is (SampleDeployJob) configure
screen scroll down to the Build Trigger section and select the “Build
after other projects are built” option from the given options. Like given

We are not doing this on the first job because the ‘Build Trigger’
functionality did not work on the first job so that’s why we set it on our
second ( SampleDeployJob).
At the (SampleDeployJob) configure screen we have to add our first
job (SampleBuild) in Build Trigger’s Build after other projects are built
option. Like the given image:

Tick check the box “Trigger only if the build is stable” then apply and
save.

After, for the third job (SampleTestJob), we have to use our second
(BuildDeployJob) at the ‘Build Trigger’s Build after other projects are
built ” option like the above method.
Now when we run our first Job ( SampleBuild), the other jobs should
be executed. For this run/Build the first job and check whether others
are starting the execution or not.

After our jobs are chained successfully, the next step is to Install
Delivery Pipeline Plugin (Manage Jenkins > Plugins > Available
Plugins> Search and install Delivery Pipeline)

After installing go to the dashboard screen and here we will create a


view for our pipeline of jobs

To create a view click on the + button available on the dashboard

Then select ‘Delivery Pipeline View’ enter the name of the view and
then click on Create.
Once the view is created> Go down/scroll to the end at the Pipeline
section like the given image, and then click on the +Add button under
the ‘Components’
Then enter the name of the pipeline and select your initial job (in my
case it is SampleBuild) and then apply and save.

Once the view is created, this will look like this:

Then we can also edit the view from the given left side menu by
clicking on the ‘Edit View’ button.
Here in the ‘edit view’ screen, we have different settings. If you want
to see details of this see the given video from 7:20 minutes
https://youtu.be/ndLbn24OwQg?si=LEbWmBwURHDDrnAT
Or if you want to know about all the button information then we can
click on (?) the button given next to every title of the button like this:

● 15: How to set up BUILD PIPELINE in Jenkins?


1st step is to build a pipeline is to chain all the jobs in sequence. In the
previous lecture 14 we learned how we can chain the jobs in sequence.
2nd step is to download and install the build pipeline plugin
Manage Jenkins> Plugins> and download and install it.
3rd step is to create/build a pipeline view from the Jenkins dashboard of
(build the view) by clicking on the Plus (+) button

Enter the name and select (“Build Pipeline View”) and then click on Create
Then other settings remain the same, just set your initial job where the
pipeline starts from under ‘Pipeline Flow” :

After selecting the initial job, Apply and save.


For more information watch the video by clicking on the link:
https://youtu.be/zf6ogW0HKLY?si=PN5ExNIonl4QOH3T
Difference between delivery and build pipeline:
At a high level, you can say the Build pipeline has a smaller scope in terms
of the entire process of software dev and delivery whereas the delivery
pipeline has a much broader scope. Also when we say delivery, build
pipeline will be one of its components.
Build Pipeline provides a view of the upstream and downstream jobs setup
for the build process (typically)
Whereas the Delivery Pipeline gives the visualization of the complete
Delivery Process that may include Build, Deploy, and Test. In the delivery
pipeline view, you can see each of these sections in a separate box.

● 16: What is BLUE OCEAN (How to get started)?


Blue Ocean is a new User Interface for Jenkins which provides an
interactive view for Jenkins Pipeline (and jobs).

How to get started with Blue Ocean?


1: Make sure that the Jenkins version we are using is above the 2.7
version. For checking the Jenkins version look at the bottom of the Jenkins
dashboard screen
2: The second step is to install the ‘Blue Oceas’ plugin from the plugin
menu screen (Install Blue Ocean, not Blue Ocean Editor like that
After installing, Restart Jenkins, if you get any error in installing the plugin
or some of them didn’t install, go to Google and install the required version
of the plugin and install it at Jenkins.
3: After installing and restarting Jenkins, Move to the ‘Blue Ocean’ screen
by clicking on the ‘Blue Ocean’ button in the left menu on the Jenkin
Dashboard screen.
Here you will see all our jobs are displayed, and We have the option to
create a new pipeline there. If you want more details about the job click on
any job, then you will see details about it.
If the user wants to go back to classic Jenkins, he can easily go there by
clicking on the ‘Arrow’ icon available on the top right side.

● 17: How to trigger the Job with Email?


So in this section, we will get to know how we can trigger the job with
Email.
1: For this first, we have to download and install the ‘Poll Mailbox Trigger’
Plugin( Manage Jenkins > Plugins > Available Plugins )
To check the plugin is installed completely, Create a new freestyle job, and
after creating > scroll down to the Build trigger section at the Job
configuration screen. If you see that the option ‘Poll mailbox’ is there then it
means the plugin installed successfully like this:
Then to use this plugin click on the checkbox of it. There you will see
options like this:

For Host go to this website:


(https://github.com/jenkinsci/poll-mailbox-trigger-plugin)
scroll down and copy the information according to your mail domain. I am
using Gmail so I will copy the Gmail Hostname:

Note: ( If you get any issue in Gmail setting security, then you have to use
the above method where we set up Gmail for these options ( reminder: 2-
factor authentication Gmail account at lecture 12 section)
For more in-depth details, visit https://youtu.be/DWlAQe57uz8?
si=4L2922uE8AlLd6RT
● 18: How to create Build Monitor View?
What is?
Build Monitor Plugin provides a highly visible view of the status of selected
Jenkins jobs. It easily accommodates different computer screen sizes and
is ideal as an Extreme Feedback Device to be displayed on a screen on
your office wall.
For this,
1: Install the ‘Build Monitor View’ Plugin in your Jenkins.
2: For use, Go to Dashboard, click on the ‘+’ button above the Jobs, and
then there you will see the Build Monitor View option.

Enter the name that you want and click ‘Create’. From the next page, you
will get the Configuration setting. Setup according to your needs.
● 19: How to install Jenkins on Amazon AWS EC2
Linux?
The installation is very easy on Linux through Amazon.
Here are the notes:

1. How to download and install Jenkins on aws ec2 Linux


2. How to access Jenkins from the browser
3. How to start and stop Jenkins
4. How to start Jenkins on a different port
5. How to uninstall Jenkins

Step 1: Connect to your Linux machine


Setup JAVA PATH
Set up Custom TCP port: 8080 in AWS Security Groups

Step 2: Update Packages


sudo yum update

Step 3: Check Java is installed. If not install java


java -version
sudo yum install java-1.8.0

To check and select one out of multiple Java versions available


sudo /usr/sbin/alternatives --config java

Step 4: Download the latest Jenkins code package


sudo wget -O /etc/yum.repos.d/jenkins.repo
http://pkg.jenkins-ci.org/redhat/jenk...

Step 5: Import a key file from Jenkins-CI to enable installation from the
package
sudo rpm --import http://pkg.jenkins-ci.org/redhat/jenk...

Step 6: Install Jenkins


sudo yum install jenkins

Step 7: Start jenkins


sudo service jenkins start

Step 8: Access the Jenkins server using the public DNS of your ec2 on port
8080
https://{ec2-public-dns}:8080
example: http://3.89.79.74:8080/

Note: Here you might have to allow port 8080 in your security group
settings

Useful tips

To start jenkins on a different port


Update port number in /etc/sysconfig/jenkins

To fetch the initial admin password


sudo su -
cd /var/lib/jenkins/secrets/
cat initialAdminPassword
0bcbbcab7f984af7b4171b55e9201d04
To stop Jenkins
sudo service jenkins stop

To uninstall Jenkins
sudo service jenkins stop
sudo yum remove jenkins
sudo rm -r /var/lib/jenkins

References
https://docs.aws.amazon.com/aws-techn…

Here is a video link which can help us more to understand:


https://youtu.be/jmm8DsosBqw?si=h7qXwI7z4tfs5Ndg

● 20: What is Jenkinsfile | How to create


Jenkinsfile?
To get the knowledge about Jenkins pipeline see the video and then you
will get a complete idea about it:
https://youtu.be/RsD2nzPY0is?si=9GByAPuZgiWLmIdq

● 21: How to get Jenkinfile from the SCM (source


code management system like Git)?
In the above video, we see how we can create a Jenkinsfile within
Jenkins and use the ‘Pipeline’ plugin to create a continuous delivery
system and create a pipeline.
In this tutorial, we will see how we can get our Jenkinfile from any
source code management system( SCM):
Watch this given video:
https://youtu.be/8IWH1cYVZt4?si=yDeem9Gb2Q-LRTiZ

You might also like