forked from Sonal0409/DevOps_ClassNotes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMVNJENKINS_NOTES.txt
More file actions
616 lines (390 loc) · 15.7 KB
/
MVNJENKINS_NOTES.txt
File metadata and controls
616 lines (390 loc) · 15.7 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
Slide 65 for Maven
Git installation:
yum install git -y
If using Git class instance-- go to root Directory
SEE THAT YOU ARE IN ROOT DIRECTORY
# cd
# pwd
************************************
JAVA Installation
SEE THAT YOU ARE IN ROOT DIRECTORY
# yum install java-1.8.0-openjdk-devel
# alternatives --config java
# vim /etc/profile
i
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.265.b01-1.amzn2.0.1.x86_64
export PATH=$JAVA_HOME/bin:$PATH
ESC :wq!
# source /etc/profile
****************************************************
INSTALL JENKINS
Go to https://pkg.jenkins.io/redhat-stable/
and copy these commands
# sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
# sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
yum install epel-release # repository that provides 'daemonize'
sudo amazon-linux-extras install epel
yum install jenkins
# systemctl start jenkins
# systemctl status jenkins
# clear
# cat /var/lib/jenkins/secrets/initialAdminPassword
Click on install plugins
Give username and password details
Jenkisn is ready to use
Creation of a Job:
************************************
#1 Jenkins --> New Item --> Job1 --> Build --> shell comand---> echo 'this is my first job'
Save --> click on Build Now --> click on build number --> click on Console to see output
#2 Jenkins --> New Item --> Job2 --> Build --> shell comand--->
date >> /tmp/date.txt
echo 'this is my second job'
Save --> click on Build Now --> click on build number --> click on Console to see output
***********************************
Creation of new User
*************
Jenkins--> Manage Jenkins--> Create/Add Users ----> left side ---> add User
Add Name as DEV-->Last name -->Password--> Confirm Password--> Email --> submit
New user created.
*****************
MANAGE AUTHORIZATION
Matrix based Security
Goto MangeJenkins--->Global security --> Authorization Section---> select Matrix based Security
---> Click on Add Users --> write admin---> give adminsitation access
----> click on ADD --> write dev--> give Read access and job read access
--> logout as admin -- login as Dev --> no jobs, only read access
--> logout as Dev ---> login as Admin
Project based Matrix Security
Goto MangeJenkins--->Global security --> Authorization Section---> Select Project based Matrix Security
--> Click on Add Users --> write admin---> give adminsitation access
----> click on ADD --> write dev--> give Read access
--> go to Job1 --> configure--> general --> Check Enable project based security
--> Add user as Dev --> give him access to read, build, write, workspace, build
--> go to Job1 --> configure--> general --> Check Enable project based security
--> Add user as Dev --> give him access to read
Save file
Logout as admin--> login as Dev --> you will see both the jobs , job2 will be only read access.
Logout as dev and login as Admin
**************************************
Plugins
******************
AUDIT TRAIL :
-->Manage Jenkins-->Manage plugins--> go to Available tab -->give audit trail
---> click on checkbox-- Install without restart -- Installation complete
--> Manage Jenkins--> Configure System--->Scroll down to AUDIT TRAIL
--> From drop-down select --> log file
---> log location : /tmp/jenkinsAudit.log
---> Log file size --> 25
---> Log file count --> 5
---> Log Seperator ==> leave it blank or give any special character.
Go to job 1 and rerun it
Now go to server
# cd /tmp
# ls
Audit log file will be available with name as ==> jenkinsAudit.log.0
# cat jenkinsAudit.log.0 ==> to see contents of log.
***********************
EMAIL NOTIFICATION
*********************
--> Manage Jenkins--> Extended Email Notifications
Give:
Smtp server ==> smtp.gmail.com
port : 465
Click on advance
SMTPUsername:
Password: --> --> click on chnage password to enter yur password.
Check SSL checkbox
Save the chnages
Now go back to Jenkins--> Job1 --> configure--> post build actions-->edit email notification-->
---> recipient list as ==> batch123@....
--> scroll down select Attach build log
--> Click on ADVANCE
--> scroll down to TRIGGER
--> Remove Failures
--> Click on Add trigger button below and select Always
---> select recipents list and remove developers.
Save and build now on Job.
Email will be sent with log
*****************************************
TRIGGERS
TRIGGERS
If we want to automatically trigger a Job
Build triggers for any job:
1. Trigger builds remotely (e.g., from scripts)
Select the above very first option
give Authentication token as : token1
any unique token number
And then copy the path provided below in this format:
JENKINS_URL/job/job4/build?token=TOKEN_NAME
update the above url in this way:
Jenkins_URL: Curent url :http://3.140.252.165:8080
Token_NAME: token number/name provided above
Take this path and execute on anyother browser.
http://3.140.252.165:8080//job/job4/build?token=token1
Login as admin admin
you can run that job.
2. GitHub hook trigger for GITScm polling
> Select the above option
Save the job and now go to git hub
Always save the job and then only go to git hub, perform these steps
go to the repository setting
select webhooks on left side
delete if any exisitng webhook
click on createwebhook on right side
provide following information:
Payload URL : jenkinsurl/github-webhook/ ===> http://3.140.252.165:8080/github-webhook/
Content type : select application/json
Secret: no need of any value
Which events would you like to trigger this webhook?
select first option -- Just the push event.
Select Active
click on add webhook.
now make some changes in repo,
go to jenkins--> you will see a new build has been created
3. Trigger prieodically
Create anew Job--> build trigger--> build preodically --> give */2 * * * * --> Save and build now
All build will be genrated automatically every 2 mins
4. Trigger by POLL SCM
Create a new Job-->Source code management--> Add git path
https://github.com/Sonal0409/GITHUBJENKINSDEMO
Build Triggers--> Poll SCM --> * * * * *
Build will be generated each time there is a chnage in the repository only.
*********************************
*********************
If we want to automatically trigger a Job
1. Trigger prieodically
Create anew Job--> build trigger--> build preodically --> give */2 * * * * --> Save and build now
All build will be genrated automatically every 2 mins
2. Trigger by POLL SCM
Create a new Job-->Source code management--> Add git path
https://github.com/Sonal0409/GITHUBJENKINSDEMO
Build Triggers--> Poll SCM --> * * * * *
Build will be generated each time there is a chnage in the repository only.
**************************************
MAVEN INSTALLATION
****************************************
# yum install maven
# mvn --version
# vim /etc/profile
i
export MAVEN_HOME=/usr/share/maven
ESC :wq!
# source /etc/profile
# echo $MAVEN_HOME
***************************************
Clone GIt DEVOPS repository
mkdir myproject
cd myproject
mkdir projects
cd projects
git clone https://github.com/Sonal0409/DevOpsClassCodes
ls
cd DevOpsClassCodes
ls
********************************
RUN MAVEN COMMANDS
# mvn compile
# cd /root/myproject/projects/DevOpsClassCodes/target/classes
ls
cd com
ls
cd edurekademo
ls
cd utilities
ls
cd
pwd
****************************************
*********************************
Delivery Pipeline in Jenkins
*******************************
Jenkins--> Manage Jenkins-->Global Tool configuration
Go to JDK section
In this give:
1. Uncheck -- install automatically
2. JDK name as myjava
3. Go to server type
# echo $JAVA_HOME ==> will give java location
JAVA_HOME =
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.265.b01-1.amzn2.0.1.x86_64
Save it.
Under GIT==> leave it same
Under maven ==> lets install it automatically
Type mymaven and check the install automatically box.
Save the changes
****************************************
CREATION OF COMPILE JOB
***********************************
1. new Job --complie-->freestyle project
2. Source code management --> select git
--->give git path ===> https://github.com/Sonal0409/DevOpsClassCodes.git
3. build ==> select invoke top level maven targets
select maven version===> mymaven
goal ==> compile
4. save ==> build now
So all the compiled files will be present at this location
==> look for this line in console output at the end on jenkins
Compiling 13 source files to /var/lib/jenkins/workspace/compile1/target/classes
5. Go to workspace on the left side of jenkins Job compile
In the folder go to ==> target folder==> go to classes/com/edurekademo
==> click on edurekademo==> got to utilities==> all class files will be there which have recently been compiled.
******************************
2nd JOB ==>Code Review
*********************
Jenkins--> new item--> Name= code Review==> freestyle project
==> source codemanagement==>select git==>
give git repo name https://github.com/Sonal0409/DevOpsClassCodes
Step 2: build ==> invoke top level maven target==>
select maven version==>mymaven ==> goal = pmd:pmd
save==> build now
==> click on build number and see the console
Goto Workspace on the left side of jenkins Job code review
In the folder go to ==> target folder ==> you will find pmd.xml file.
CONVERT FILE TO TREND REPORT
*******************************
manage jenkins--> manage plugins--> available --> Search for pmd plugin--> install it.
Now go to the job Code Review==> go to post build actions ==> select Publish PMD anaylsys report
under PMD results==> give path of pmd.xml file ie: target/pmd.xml
Save the file and build now.
After the build is successfull, you will see PMD Warnings
So you will see 12 new warning have been generated on the code.
Scroll down
Under package click on first one to check warnings.
*********************************
JOB3: Testing --Unit Test report
********************************
1. jenkins--> new item--> Name= unitTest==> freestyle project
==> source codemanagement==>select git==> give git repo name https://github.com/Sonal0409/DevOpsClassCodes
2. build ==> invoke top level maven target==> select maven version==>mymaven ==> goal = test
3. Save and Build now.
4. Check the workspace
5. surefire-reports will be present
But we cannot understand them easily. So lets generate understandable reports by using Junit reports option under post build actions
6. Go back to job==> post build actions==> select junit test result job
==> give target/surefire-reports/*.xml ===>under test report xml
This is path where xml files are there.
Here *.xml is as we want to use all the xml files
7. Save and build again
Go to build number ===> on left side you will see Test Result
Click on it and you can see all pass and fail details of the tests
**************************
JOB4 : Metric check
************************
1. Go to jenkins--> new item--> Name= Metriccheck==> freestyle project
==> source codemanagement==>select git==> give git repo name https://github.com/Sonal0409/DevOpsClassCodes
2. build ==> invoke top level maven target==> select mymaven
goal = cobertura:cobertura -Dcobertura.report.format=xml
3.==> save==> build now
==> click on build number and see the console
4. go to Workspace ==> target/site/cobertura/coverage.xml
The xml file will be there, But we cannot understand them easily. So lets generate understandable reports by downloading cobertura plugin
Go to manage jenkins--> manage plugins--> cobertura --> install it
Go back to the job--> post build actions--> select publish cobertura coverage report
Under cobertura xml pattern give ==> target/site/cobertura/coverage.xml ==> you can take the path from below displayed text.
Save and build again
Click build number ==> on left side you will find Coverage Report==> click on it ==> we cna see the coverage report.
**********************************
Job5 Package Job
***************************
Go to jenkins--> new item--> Name= Package==> freestyle project
==> source codemanagement==>select git==> give git repo name https://github.com/Sonal0409/DevOpsClassCodes
build ==> invoke top level maven target==>mymaven
goal = package
==> save==> build now
==> click on build number and see the console
==> go to workspace ==> target/addressbook.war
**************
Connecting all the 5 jobs so that each job can be triggered:
*****************
******************************
Pipeline as a code
**************************
Lets create a pipeline using code:
Create 1 job and add multiple task (using code) in the job and run multiple tasks in 1 job
This code is written using pipeline DSL(domain specific language]
It is derieved using groovy
Create a new job ==> select project as pipeline
pipeline{
tools{
jdk 'myjava'
maven 'mymaven'
}
agent any
stages{
stage('checkout'){
steps{
git 'https://github.com/Sonal0409/DevOpsClassCodes.git'
}
}
stage('compile'){
steps{
sh 'mvn compile'
}
}
stage('codeReview'){
steps{
sh 'mvn pmd:pmd'
}
}
stage('unitTest'){
steps{
sh 'mvn test'
}
}
stage('metricCheck'){
steps{
sh 'mvn cobertura:cobertura -Dcobertura.report.format=xml'
}
}
stage('package'){
steps{
sh 'mvn package'
}
}
}
}
************************************
Master and Slave Set up
restart the instance.
Restart the jenkins server
now take public ip and loginto jenkins
Step 1: Go to Manage Jenkins ---> CONFIGURE SYSTEM ---> Check the Jenkins URL is same as URL of browser --> Save
Step 2: Go to Manage Jenkins---> CONFIGURE GLOBAL SECURITY --> Scroll down to AGENTS-->select RANDOM radiobutton--> Save
Step 3: Go to Manage Jenkins---> MAnage Nodes and Clouds --> New Node
Select New Node--> give name as Winslave--> select radio button permanamet Agent
Name : winslave
Description: give windows 10 machine
==> # of executors = 1 ==> means number of jobs that we want to run on this slave node. Let it be 1 for now
===> Remote root directory ==> C:\jenkinsdir
Location of a directory folder on your windows machine , so that jenkins can create a workspace folder and store all the files. As we have seen earlier while executiong jobs.
For this --> got to C drive and create an emapty folder.
C:\jenkinsdir
====> Label : win_slave ==> this is like a tag to slave machine. This is how we will identify the slave machine--> remeber this
===> Usage:
Select 2nd option: Only build jobs with label expressionmatching the job :
This means for which ever job we will give label as win_slave that job only will run on this job.
===> Launch Method : select option : Launch agent by connecting to master.
Scroll down
Add tools
Add git path as C:\Program Files\Git\cmd\git.exe
Add java path as C:\Program Files (x86)\Java\jdk1.8.0_191
SAVE the job.
Now go to win_slave ==> download the agent.jar file and put it in your jenkinsdir folder.
Copy the path and change the public ip in below path
java -jar agent.jar -jnlpUrl http://3.139.60.71:8080/computer/winslave/slave-agent.jnlp -secret 61e8e20badd531b48caeffac8d686f182eb2d77a1c303ee39c5db9d00117c0be -workDir "C:\jenkinsdir"
Go to bash and start jenkins, refresh instance
Go to cmd prompt
change to cd c:\jenkinsdir
copy the above jenkins agent path
System will get connected.
***************************************
RUN a job on Agent
Go to Dashboard
Select the package job---> configure --> GENERAL --> click on Restrict where this project can be run and enter win_slave
SAVE THE JOB
CLICK on BUILD NOW -- to start the build
CLick on build number--> got to console --to check it is executing on AGent
Go to C folder-->jekinsdir---> C:\jenkinsdir\workspace\package\target==>
you will see addressbook.war file.
On linux:
$ which git