-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathJENKINS_Day 3.txt
272 lines (232 loc) · 13.4 KB
/
JENKINS_Day 3.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
JENKINS_Day 03
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1. Build after other projects are built
2. Trigger builds remotely
3. Recovering Jenkins password
4. Throttle Builds (Self Explore)
5. Project 2
5.1 How to change port number of Tomcat server
------------------------------------------------
1. Build after other projects are built
------------------------------------------------
=> Upstream and Downstream Jobs
job1 - Build Now - Success - Trigger Next Job
job2 - Automatic Trigger - Success - Trigger Next Job
job3 - Automatic Trigger - Success
The execution of one job is dependent on the successful execution of the previous job
The execution of Job2 is dependent on Job1
The triggering of job2 is configured in job1 --- Job1 --- Post Build Actions --- Build Other Projects --- Configure the Job2 triggering
The execution of Job3 is dependent on Job2
The triggering of job3 is configured in job2 --- Job2 --- Post Build Actions --- Build Other Projects --- Configure the Job3 triggering
-----------------------------------
2. Trigger builds remotely
-----------------------------------
Project lead ---- Vacation ---- DevOps engineers has created the job but they dont have the permission to execute the job. Only the project lead is having the permission to execute the job.
Even if the person is not having access to the Jenkins console, we can still build the job using 'Trigger builds remotely' option.
URL --->
http://15.168.8.103:8080//job/trigger%20remote%20job/build?token=kastro
when we hit the above URL, Jenkins will ask for username and password. You can enter username and password of Jenkins and then the job will get executed. This scenario is not recommended because it is asking for username and password of Jenkins.
My situation is i dont have the username and password of Jenkins, still i want to execute the job. To do this we have to install a plugin which is known as 'Build Authentication Token Root'
-------------------------------------------
3. Recovering Jenkins password
-------------------------------------------
----------------------------------------------
4. Throttle Builds (Self Explore)
----------------------------------------------
In Jenkins, "Throttle Builds" is a feature provided by the "Throttle Concurrent Builds" plugin. This plugin allows you to limit the number of concurrent builds that are allowed to run for a particular job or a group of jobs. This can be useful for managing resource usage and preventing overloading of your build infrastructure.
How to Use Throttle Builds:
Install the Plugin:
Go to Jenkins dashboard.
Navigate to Manage Jenkins -> Manage Plugins.
In the Available tab, search for "Throttle Concurrent Builds Plugin" and install it.
Configure Throttling:
Open the configuration page for the job you want to throttle.
Scroll down to the Throttle Concurrent Builds section.
Check the Throttle builds option.
Configure the throttling settings according to your requirements, such as the maximum number of concurrent builds, per node and/or per project limits, and any categories if applicable.
Define Categories (if needed):
Go to Manage Jenkins -> Configure System.
Scroll to the Throttle Concurrent Builds section.
Define the categories and the limits for each category.
--------------------
5. Project 2
--------------------
Build and Deployment Process
Jenkins + Git + GitHub + Maven + Tomcat
Tasks:
---------------
========
Part 1
========
1. Create an EC2 Instance - Connect to the instance using MobaXTerm Tool.
========
Part 2
========
JENKINS INSTALLATION ON LINUX EC2 INSTANCE
~ Add Jenkins repo. to our YUM repo:
sudo wget -O /etc/yum.repos.d/jenkins.repo \
https://pkg.jenkins.io/redhat-stable/jenkins.repo
~ Import a key-file from Jenkins-CI to enable the installation from package:
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
sudo yum upgrade
~ Installing Java
sudo amazon-linux-extras install java-openjdk11 -y
java -version
~ Install Jenkins:
sudo yum install jenkins -y
~ Lets start, enable, and check the status of Jenkins
sudo systemctl enable jenkins
sudo systemctl start jenkins
sudo systemctl status jenkins
You should see Active & Running in Green Colour, which means the Jenkins
service is successfully installed
~ Check the jenkins accessibility. By default the Jenkins will run on Port No. 8080.
So, open Port No. 8080 for EC2 instance to access Jenkins
"Check" the instance ----> Security ----> Click on the link under SGs ----> Add Rules ----
> Custom TCP, 8080, Anwhere ----> Save Rules.
Open new tab: <Paste the Public IP of Instance>:8080 ----> You will see the Jenkins Home Page
----> Now we have to Unlock the Jenkins ----> You will see a code in the red colour ----> Copy
the code in red colour ----> Go to MobaXTerm ----> sudo cat <paste the red color code> ---->
You will see a password. Copy that password and paste in jenkins home page ----> Click on
suggested plugins ----> You will see "Create First Admin User" ----> Enter Username, Password,
Name, Email Id ----> Save and Continue ----> Jenkins URL:http://52.30.109.196:8080/ ---->
Save and Finish ----> You will see "Jenkins is ready!" ----> Click on "Start using jenkins" ---->
Now you can see the jenkins home page and here you can create Jenkins Jobs.
========
Part 3
========
TOMCAT WEBSERVER INSTALLATION ON LINUX EC2 INSTANCE
https://tomcat.apache.org/download-90.cgi ----> Downloads ----> Tomcat 9 ----> Copy the link
of tar.gz file ----> https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.73/bin/apache-tomcat-
9.0.73.tar.gz ----> sudo wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.73/bin/apachetomcat-
9.0.73.tar.gz ----> ls -l ----> You will the tomcat file in red colour. This is a tar.gz file and
we have to extract the tar.gz file ----> tar -xvf <enter the name of tomcat file in red colour> ---->
All the files will get extracted ----> ls -l ----> You will see the list of extracted files ---> cd
<enter the name of tomcat file in blue colour>
Lets start the tomcat server:
Inorder to start the tomcat server, we need a file called "startup.sh"
This "startup.sh" file will be there in "bin" folder ----> cd bin ----> ls -l ----> you will see
startup.sh file ----> ./startup.sh ----> You will see "Tomcat Started"
Lets access the tomcat server:
Since, the tomcat also runs on Port No. 8080, we cannot access tomcat as on the same port no.
jenkins is also running.
Now, we have to change the Port No. of Tomcat. (Tomcat Port No. 9090)
How to change the port no. of Tomcat?
------------------------------------------------------
In order to change the port no. of Tomcat, we will configure a file known as "server.xml" This
server.xml file will be available in "conf" folder.
cd .. ----> pwd ----> /home/ec2-user/apache-tomcat-9.0.73 ----> ls -l ----> cd conf ----> ls -l ---->
you will see server.xml file ----> vi server.xml ----> scroll down till you see "connector
port=8080" ----> press "i" ----> Change the port no to 9090 instead of 8080 ----> escape ---->
:wq
Check the Tomcat accessibility. By default the Tomcat will also run on Port No. 8080. But on the
same port no. jenkins is also running.
So we have to add the port no. to 9090 in ec2 instance to access Tomcat:
"Check" the instance ----> Security ----> Click on the link under SGs ----> Add Rules ----
> Custom TCP, 9090, Anwhere ----> Save Rules.
Shutdown the tomcat server ----> cd bin ----> ./shutdown.sh ----> ./startup.sh
Open new tab: <Paste the Public IP of Instance>:9090 ----> You will see the Tomcat Home Page
Go to browser and check for tomcat accessibility. You are now able to access Tomcat and Jenkins
in the same instance.
Here we can access only the Home page of Tomcat.
Inorder to access complete tomcat server, we need to do the configuration in "context.xml" file.
apache-tomcat-9.0.73/webapps/manager/META-INF/context.xml
cd apache-tomcat-9.0.73/webapps/manager/META-INF/context.xml ----> vi context.xml ---->
You will see a "valve" tag. You have to edit "Allow" tag ----> ".*" />
Configuring the Users in Tomcat
---------------------------------------------
we need to edit the tomcat-users.xml to configure the users.
The "tomcat-users.xml" file is available in "conf" folder
<role rolename="manager-gui" />
<user username="tomcat" password="tomcat" roles="manager-gui" />
<role rolename="admin-gui" />
<user username="admin" password="admin" roles="manager-gui,admin-gui"/>
cd bin
./shutdown.sh
./startup.sh
Access Tomcat on Browser ----> Click on Server Status ----> Enter the username (admin) and
password (admin)
Install git on VM
---------------------------------
Install GIT in EC2 Instance where Jenkins is running
sudo yum install git -y
Jenkins Port No.: 8080
Tomcat Port No.: 9090
========
Part 4
========
Inorder to do the automation, Jenkins is going to execute some script to deploy the war file into
the Tomcat Webserver.
___________________________________________________________
Step 1: Adding the manager-script role in tomcat-users.xml file
___________________________________________________________
In order to do the automation, Tomcat user should have a "manager-script" permission. There
should be one user in the Tomcat with the "manager-script" permission. Then with that user
credentials we can automate the deployment process.
Goto MobaXTerm and Connect to the Instance ----> ls -l ----> cd apache-tomcat-9.0.73 ----> ls -l
----> cd conf ----> ls -l ----> You can see tomcat-users.xml ----> vi tomcat-users.xml ----> Scroll
down till you see "role rolename="manager-gui"", go to the last but-one line and type the below
<role rolename="manager-script" />
Goto the last line of "user username="admin"", and type manager-script at the last, just before
inverted quotations ----> esc ----> :wq
___________________________________________________________
Step 2: Installing "deploy to container" plugin in Jenkins software
___________________________________________________________
Jenkins Dashboard ----> Manage Jenkins ----> Plugins ----> Available Plugins ----> In the search
bar, type 'deploy to container' and check the same ----> Click on "Install without restart" ---->
Click on "Go back to the top page.
___________________________________________________________________________
Step 3: Creating a Jenkins job and for that Jenkins job we will give permission to deploy
___________________________________________________________________________
Add JDK, GIT, MAVEN in Jenkins Browser Page by going into Tools Configuration
Repo URL: https://github.com/KastroVKiran/maven-app-1.git
========
Part 5
========
1) Create Jenkins Job
-> New Item.
-> Enter Item Name (Job Name) Name: git-maven-tomcat-integration
-> Select Free Style Project & Click OK
-> Enter some description
-> Go to "Source Code Management" Tab and Select "Git"
-> Enter Project "Git Repo URL" i.e https://github.com/KastroVKiran/maven-app-1.git
Branch name: */main
-> Go to "Build Triggers" tab. Here i will trigger the job manually.
-> Go to "Build Steps" tab
- Click on Add Build Step and Select 'Inovke Top Level Maven Targets'
-> Select Maven and enter goals
- Maven Version: Maven-3.9.6
- Goals: clean package
-> Click on 'Post Build Action' and Select 'Deploy war/ear to container' option
-> Give path of war file (You can give like this also : **/*.war )
-> Enter Context Path (give project name Ex: java_web_app)
-> Click on 'Add Container' and select Tomcat version 9.x
-> Credentials ---> Click on "Add" ---> A dia ---> Username: admin, Password:
admin, ID: Tomcat-Credentials, Description: tomcat
credentials ---> Add
-> Credentials ---> Select "Tomcat-Credentials"
-> Enter Tomact Server URL (http://ec2-vm-public-ip:tomcat-server-port)
-> Click on Apply and Save
Note: To do any edits to the job, click on "Configure" and edit accordingly
2) Run the job now using 'Build Now' option and see see 'Console Output' of job
3) Once Job Executed successfully, go to tomcat server dashboard and see application should be
displayed.
4) Goto Tomcat browser and click on "maven-web-app"
Click on the application name (it should display application)
For ex, assume that i have modified the code in github repo; does the changes gets reflected in
our app? Lets see
Goto repo https://github.com/ashokitschool/maven-web-app.git ---> src/main/webapp/index.jsp -
--> Change the code accordingly ---> Commit changes ---> Goto Tomcat browser and reload --->
You dont see any changes.
So what we need to do is; we need to rebuild the job ---> Goto Jenkins UI ---> Build Now --->
Goto Tomcat Browser ---> Refresh the page ---> You will see the changes.
But here there is a problem, whenever there is a code change, how DevOps engineers knows it?
So, here we need to do automation in such that, whenever there is a change in the code in github
repo, automatically those changes should reflect in the app. Lets do that.
Jenkins UI ---> Configure ---> Build triggers ---> Poll SCM ---> Schedule: * * * * * ---> Apply -
--> Save
Goto repo ---> src/main/webapp/index.jsp ---> Change the code accordingly ---> Commit
changes ---> Goto Tomcat browser and reload ---> You see changes now (wait for a minute)
Goto Jenkins UI ---> You will see the next build triggered automatically.
This is called Continous Integration and Continous Deployment
Do again somechanges in repo. and look for changes in app browser tab