forked from Sonal0409/25July-Batch-DevOps-Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsPipeline-Jenkinsfile_Notes
More file actions
243 lines (136 loc) · 4.66 KB
/
JenkinsPipeline-Jenkinsfile_Notes
File metadata and controls
243 lines (136 loc) · 4.66 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
Code Based pipeline
******************
Set up tools in global tool configuration:
1. jdk ==> myjava
2. maven ==> mymaven
Every job : common step
SCM ==> https://github.com/Sonal0409/DevOpsCodeDemo.git
Every job : maven command
Job1:
maven command
compile the code => compile
connect job 1 with job2
Job2: Unit testing
maven command
test the code => test
post build action:
convert junit report
Connected to job 3
Job3: Code coverage
maven command
coverage the code => cobertura:cobertura -Dcobertura.report.format=xml
post build action:
convert coverage report ==> cobertura
Connected to job 4
Job4: package
maven command
package the code => package
Connected to job 5
Job5: Code review
maven command
code review => pmd:pmd
post build action:
convert pmd.xml report
We cna do all of this process in a signle by write code
With Jenkins 1 version : scripted pipeline syntax:
node{
was complex
jenkins admin has be really good in scripting because:
> there is proper structure to the script
> the entire script is as per the admin
> script are written in DSL which is groovy based
> when you executed the script, jenkisn doesnot validate the code at first
> if the pieline stop in middle, you have start the pipeline again from the very step, which is time consuming
In jenkins 2 you will write the code using Declarative pipeline syntax
> write script for pipeline is very easy
> Declarative pipeline syntax is also groovy based and very easy to write
> Declarative pipeline syntax is well defiend and there is a structure/template that admin has to follow in order to write the code
> Jenkins provides a snippet generator tool, that automatically geenrates your pipeline code
> beacuse we define everything in a proper structure, its easy to read and easy to write.
> if you dont follow the structure given by jenkins, you will get error
> when you start exeucting the pipeline ==>
> jenkisn will validate the code give you errors
> jenskin will exeucte the code
> in case jenkisn pipeline stops in the middle, you can fix the issue and restart from that particular stage itself
> piepline is execution multiple stages/tasks
> these tasks can be executed in sequence or in parallel
> jenkisn can read or fetch the pipeline code form github and execute the pipeline --> Jenkinsfile
Structure of writing Jenkins declarative syntax
****************************
key 'value'
declarative syntax consist of various blocks
1. all the code will be placed inside the pipeline block
2. In the pipeline block you will create tools block, defining what tools to be used in the pipeline
pipeline{
tools{
jdk 'myjava'
maven 'mymaven'
}
agent any
stages{
stage('Clone repo')
{
steps{
git 'https://github.com/Sonal0409/DevOpsCodeDemo.git'
}
}
stage('Compile the code')
{
steps{
sh 'mvn compile'
}
}
stage('Code Review')
{
steps{
sh 'mvn pmd:pmd'
}
}
stage('test the code')
{
steps{
echo 'Testing'
sh 'mvn test'
}
post{
success{
junit 'target/surefire-reports/*.xml'
}
}
}
stage('Coverage of the code')
{
steps{
sh 'mvn cobertura:cobertura -Dcobertura.report.format=xml'
}
post{
success{
cobertura autoUpdateHealth: false, autoUpdateStability: false, coberturaReportFile: 'target/site/cobertura/coverage.xml', conditionalCoverageTargets: '70, 0, 0', failUnhealthy: false, failUnstable: false, lineCoverageTargets: '80, 0, 0', maxNumberOfBuilds: 0, methodCoverageTargets: '80, 0, 0', onlyStable: false, sourceEncoding: 'ASCII', zoomCoverageChart: false
}
}
}
stage('Package the code')
{
steps{
sh 'mvn package'
}
}
}
}
Jenkins file:
***************
Save the script in GITHUb - in a repo
> Jenkins to pick the script from github and run it
> go to the repo
> cretae a new file with name as Jenkinsfile
> In the file place your code
How will jenkisn pickup this code now?
> create a job in jenkins -- template pipeline
> select option pipeline script from SCM
> sCM - git
> give repo path
press save
> Whenever there is an update on the script in github, this jenkins code should run automatically
> set up a trigger on the job
You can find the repo with jenkinsfile here:
https://github.com/Sonal0409/DevOpsCodeDemo/blob/master/Jenkinsfile