-
Notifications
You must be signed in to change notification settings - Fork 78
/
JenkinsNotes
748 lines (473 loc) · 18 KB
/
JenkinsNotes
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
Edureka Notes - Sonal Mittal
Jenkins Set up:
Create an ec2 Instance or use the same instance used in GIT sessions.
If git is not present:
Git installation:
yum install git -y
Check the version of GIT
…
git --version
…
SEE THAT YOU ARE IN ROOT DIRECTORY
# cd
# pwd
************************************
JAVA Installation
SEE THAT YOU ARE IN ROOT DIRECTORY
…
sudo amazon-linux-extras install java-openjdk11
…
IF Command to install java on CENTOS 8
..
sudo yum install java-11-openjdk-devel
…
Command to install java 11 on Ubuntu
…
apt-get update
apt-get install openjdk-8-jdk
…
On the browser open https://www.jenkins.io/
Go to downloads section
Select centos OS
Go to Ec2 machine and execute 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 jenkins -y
Execute command to start jenkins server:
systemctl start jenkins
systemctl status jenkins
Installation is complete on the server
We need to now set up jenkins dashboard
For this, take public ip address of ec2 server, copy it and go to your browser and give
publicipaddress:8080
On the ec2 server, execute below command
$ cat /var/lib/jenkins/secrets/initialAdminPassword
Copy the password and paste in the browser (jenkins)
Click on continue
Click on Jenkins suggested plugin tab(on left side)
On the next page
Username: admin
Password : admin
Retype password: admin
Email: admin@gmail.com
Click on continue
Click on finish
You will be on the jenkins dashboard
****************************************************
Create a Jenkisn job that will clone a github repository in jenkins workspace
Create a new job in jenkins
Click on + sign to create new item/job/project
Give a name to the job : CloneRepo
Select freestyle project and click on OK button
On the project click on Source code management
Select git option
Give git hub repo path
https://github.com/Sonal0409/myproject05Aug
Branch name as ===> Master
Save the job
Click on Build now
Repository will be cloned in jenkins workspace
You can go to job → workspace folder to see the files
****************
Day 2:
Demo 1: Create a Jenkisn job that will clone a github repository in jenkins workspace
Create a new job in jenkins
Click on + sign to create new item/job/project
Give a name to the job : CloneRepo
Select freestyle project and click on OK button
On the project click on Source code management
Select git option
Give git hub repo path
https://github.com/Sonal0409/myproject06nov.git
Branch name as ===> Master
Save the job
Click on Build now
Repository will be cloned in jenkins workspace
You can go to job → workspace folder to see the files
*****************************
Create a new job → general section→ click on advance → custom workspace
Give directory as /tmp/myworkspace
Save job → build
*********************************
Wipe out the workspace
On the job → go to build environments → Select first checkbox →Delete workspace before build
****************************************
Jenkins Triggers.
To automatically execute a jenkins job, you will use Jenkins triggers
Based on:
Periodic time- schedule
Whenever there is a commit in github repo
A parent job can trigger execution of child job
Execute a job from remote script
You will go to build triggered section in the job to set up a trigger for automatic execution of the job
Trigger prieodically
Go to Job Clone repo --> build trigger--> build preodically --> give */2 * * * * --> Save and build now
All build will be generated automatically every 2 mins
Build Triggers--> Poll SCM --> * * * * *
Build will be generated each time there is a chnage in the repository only.
Webhook:
Webhooks allow Jenkins to be notified when certain events happen on the repo. When the specified events happen, we’ll send a POST request to each of the URLs you provide.
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
Day 3: Build pipeline using Jenkins, Maven & GIT
***************************
Build too : Maven
***************************
> Powerful project management tool, This tool will help a developer to write his source code and test scripts and maintain them locally
> maven at heart is a plugin based tool (plugin execution framework)
> It is used for Java based code
> All the work in maven is done by plugins (extensions, which when downloaded and executed will enhance the features of actual tool)
> Compile the code → Maven plugin
> 2 types of plugin in Maven:
> Core Build plugins
Tasks Plugin Command (GOAL)
Compile the code Compiler compile
Execute Test cases Surefire test
Package the code package package
Delete previous
build files Clean clean
Check dependencies install install
Deploy the package to
Artifactory deploy deploy
> Reporting Plugins
Plugins that are needed for validation and review the code and generate reports
Reports by maven plugins are generated in the format : .xml, .csv, .html or .txt
Tasks Plugin Command(GOAL)
Code review
(Static Analysis tool) pmd pmd:pmd
Test reports surefire-reporting test
Code Coverage cobertura(deprecated)
JACOCO
Code review
& Code Coverage Sonarqube sonar:sonar
Code Review with PMD: The PMD Plugin allows you to automatically run the PMD code analysis tool on your project's source code and generate a site report with its results.
Code Coverage : Using code coverage tool against your compiled classes it helps you determine how well the unit testing and integration testing efforts have been, and it can then be used to identify which parts of your Java program are lacking test coverage.
A developer will create a Maven project locally in his/her IDE and write source code and test cases inside it
Traditionally, they will build all the above steps in their local machine.
Every project will include following folders:
/src/main/java ⇒ main java source code
/src/test/java ⇒ test cases will be present
In a maven project, you will have the main POM.xml
Which created by Java developer
This file will be prepared before you write the code
This file will be updated while the code
In this file Java developer will write :
> name of the artifact which will be build
> type of artifact
> Dependencies ⇒ softwares that will download automatically for code to work
****
Build section:
> name and version of plugins
Which all plugin we will use for build process
SetUp
**************************
Jenkins--> Manage Jenkins-->Global Tool configuration
Under GIT==> leave it same
Under maven ==> let's install it automatically
Type name as mymaven
check the install automatically box.
Save the changes
****************************************
CREATION OF COMPILE JOB
***********************************
1. new Job --1.complie-->freestyle project
2. Source code management --> select git
--->give git path ===> https://github.com/Sonal0409/DevOpsCodeDemo.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 warning next gen plugin--> install it.
Retsrat in case plugin fails installation.
Now go to the job Code Review==> go to post build actions ==> select recordcomplier warnings and static result analysis
Under tool ==> select PMD
under report file format ==> give path of pmd.xml file ie: target/pmd.xml
or as mentioned in message below : copy **/pmd.xml ( no quotes)
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= 3.unitTest==> freestyle project
==> source codemanagement==>select git==> give git repo name
https://github.com/Sonal0409/DevOpsCodeDemo.git
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 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 4 jobs so that each job can be triggered:
Go to manage plugins → available section
Search for build pipeline plugin
Click on the checkbox and click on Install without restart
Plugin will be installed. It will display all connected jobs on the dashboard
This plugin falls in the category of Jenkins view. It creates a build pipeline view
Lets build the pipeline:
********************************
*****************
Day4:
********************
******************************
Pipeline as a code
**************************
Lets create a pipeline using code:
Create a new job ==> select project as pipeline
pipeline{
tools{
// what tool version to use for build stages
maven 'mymaven'
}
agent any
stages{
stage ('CloneRepo')
{
steps{
echo 'This is stage 1'
git 'https://github.com/Sonal0409/DevOpsCodeDemo.git'
}
}
stage ('Compile')
{
steps{
sh 'mvn compile'
}
}
stage ('CodeReview')
{
steps{
sh 'mvn pmd:pmd'
}
post{
success{
recordIssues(tools: [pmdParser(pattern: '**/pmd.xml')])
}
}
}
stage ('Test')
{
steps{
sh 'mvn test'
}
post{
success{
junit 'target/surefire-reports/*.xml'
}
}
}
stage('package')
{
steps{
sh 'mvn package'
}
}
}
}
Day 5: Agenda:
> Jenkinsfile
It is a simple text file with no extension
This file contains declarative pipeline code for CI/CD pipeline
The name of this file is always Jenkinsfile
This will is maintained in the root directory of your project repo on SCM tool
Every team member can now make changes to the pipeline code without logging on the jenkins server.
In the github we can also maintain every version of pipeline code
We write the pipeline code in IDE and push the Jenkinsfile in the repository
In jenkins you will create a job, that will fetch the Jenkinsfile and jenkins will execute the pipeline for you.
Demo:
Click on new Item → give the job name as JenkinsfileDemo → Select pipeline project→ press OK
In the job template → click on pipeline tab → select the dropdown option as Pipeline script from SCM
Select SCM as → git
Repository URL? ⇒ https://github.com/Sonal0409/DevOpsCodeDemo.git
We will also add a trigger → Go to Build triggers tab above→ POLL SCM → * * * * *
So that if there is any commit, my pipeline should get executed everytime.
> MultiBranch Pipeline
Basically we have many branches in github for a repository
Each branch may have separate set of tasks/jobs to be executed
Whenever a branch is created on the repo, Make sure the branch has a Jenkinsfile init
In the Jenkinsfile you write the code which is needed to execute certain tasks related to the branch
we will set up a job in jenkins that will scan every branch of your github repository
Whichever branch has the jenkinsfile present on it
> for that branch a jenkins pipeline job will be created with same name as branch name and Jenkinsfile code will be executed
Whichever branch of your repo does not have Jenkinsfile on it. No job will be created for it.
Demo:
Click on new item→ create job -> give name as MultibranchdemoJob → select the template as Multibranch Pipeline → press Ok
In General section → give Display name as : Multibranch Pipeline
In branch Sources section → Click on Add source → select git
Give git Project Repository -> https://github.com/Sonal0409/MultiBranchDemo.git
Nomore changes and save the job.
Jenkins will automatically scan the branches of the repo and create pipeline jobs.
Click on multibranch pipeline job name and you will see the 3 jobs created for 3 branches.
> DSL scripts for automation:
Jenkins DSL is a programmatic way to implement the jenkins JOB
In order to write DSL script, you will have to use DSL plugin which will automatically create jobs in jenkins for you
Using the DSL plugin you will be able to create jenkins job on the fly just with help of simple groovy code.
Groovy scripts are very easy to write and read, it is like java
Demo:
Install DSL plugin
Go to Manage Jenkins → Manage plugins → Available → search for DSL → select the plugin Job DSL plugin → click on install without restart.
Now Go to Manage jenkins → Configure global security →Scroll down to CSRF protection and
Uncheck → Enable script security for Job DSL scripts
Important:
**************
We have to create a job with name as seedjob → in this job we will write the DSL scripts
The operation of seed job is to create new jobs programmatically
Seed job will always be a Freestyle job
In the seed job under Build steps you will write the DSL scripts and execute the seed job.
*******************
DSL script1:
***************
Write DSL script to automatically create a job with name as Firstjob and description as “my first DSL job”
Demo:
Create a Seed Job in jenkins
New item→ SeedJob → freestyle project–press OK
Go to Build steps → Process Job DSLs —>
Select the option -> Use the provided DSL script
Define variable in groovy and use it in Job
def jobname = 'Firstjob'
job(jobname){
}
*******
Create job with Description
def jobname = 'Firstjob'
job(jobname){
description('my testjob')
triggers{
cron ('* * * * *')
}
}
**************
ADD SCM option
def jobname = 'Firstjob'
def repo = 'sonal0409/DevOpsClassCodes'
job(jobname){
description('my testjob')
scm {
github repo
}
}
****************
Add triggers & build step
def jobname = 'Firstjob'
def repo = 'sonal0409/DevOpsClassCodes'
job(jobname){
description('my testjob')
scm {
github repo
}
triggers{
cron ('H/15 * * * *')
}
steps {
shell ("echo 'Hello World'")
}
}
*********************
Delete job created programitically
jenkins.model.Jenkins.theInstance.getProjects().each { job ->
if (job.name.contains('job')) {
println job.name
job.delete()
}
}
OR
jenkins.model.Jenkins.theInstance.getProjects().each { job ->
if (job.name.contains('Job')) {
println job.name
job.delete()
}
Upstream/Downstream pipeline using DSL script job
job('job-checkout') {
scm {
github('sonal0409/DevOpsClassCodes', 'master')
}
publishers {
downstream 'job-compile', 'SUCCESS'
}
}
job('job-compile'){
scm {
github('sonal0409/DevOpsClassCodes', 'master')
}
steps{
maven('compile')
}
publishers {
downstream 'job-test', 'SUCCESS'
}
}
job('job-test'){
scm {
github('sonal0409/DevOpsClassCodes', 'master')
}
steps{
maven('test')
}
publishers {
downstream 'job-package', 'SUCCESS'
}
}
job('job-package'){
scm {
github('sonal0409/DevOpsClassCodes', 'master')
}
steps{
maven('package')
}
}
********************************************