Jenkins and Its Setup
Jenkins and Its Setup
7e9c08f7af454874b72167b97efe7ea3
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 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 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.
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 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
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:
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.
Remember that changing the token name works like a password we have
to set it and then paste it with remote URL.
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.
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.
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
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.
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:
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.
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:
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.
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!
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)
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.
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.
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
Every stage has some item/jobs integrated with each other and work
with sequence, and we say it continues delivery pipeline.
After creating jobs reach them one by one and run the jobs to check
whether it is working or building up fine or not.
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)
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.
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:
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” :
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:
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 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 uninstall Jenkins
sudo service jenkins stop
sudo yum remove jenkins
sudo rm -r /var/lib/jenkins
References
https://docs.aws.amazon.com/aws-techn…