Wiki source code of Jenkins Shared Library

Last modified by Boris Folgmann on 2025/04/29 12:03

Show last authors
1 {{toc/}}
2
3 = DevOps-as-a-Service Open Source Pipeline Library =
4
5 DevOps-as-a-Service provides a standardized Jenkins Shared Library to be used for all your builds. See [[https:~~/~~/prd.sdc.t-systems.net/bitbucket/projects/DEVOPSAAS/repos/sdcloud-caas-jenkins-libs/browse>>url:https://prd.sdc.t-systems.net/bitbucket/projects/DEVOPSAAS/repos/sdcloud-caas-jenkins-libs/browse||shape="rect"]] for the source code and additional documentation like e.g. release notes.
6
7 == Getting Started ==
8
9 Since the Shared Library is globally configured on all Jenkins instances managed by DevOps-as-a-Service you just need to place the following very short Jenkinsfile in the root folder of your git repository to automatically build your maven or node project or simply build a container using a Dockerfile.
10
11 {{code}}
12 @Library('sdcloud') _
13
14 sdcPipeline()
15 {{/code}}
16
17 Calling sdcPipeline() starts an automatically configured, standardized build and deployment pipeline.
18 The pipeline requires a node labeled with 'docker' to execute successfully. To run on other types of Jenkins agents set the //useNode //argument as described below.
19
20 == Pipeline features ==
21
22 What the pipeline currently does is visualised in the following image which shows an example for a maven-based Java project.
23
24 [[image:attach:image2022-5-17_17-51-43.png||height="250"]]
25
26 1. Checking out the source code from git.
27 1. If a pom.xml is found your favorite JDK is selected, by default jdk11. Then a maven build is done.
28 1. If there's no pom.xml but a package.json is found a nodejs build is done.
29 1. Then the following stages are executed in parallel
30 11. Analysis: For maven projects the Java source code is checked by checkstyle, pmd and spotbugs. Furthermore the job output will be checked for any warnings generated by maven, javac or javadoc. If Python modules (.py files) exist in the git repository they will be analyzed using pylint and flake8. If pylint or flake8 are not available on the Jenkins agent the steps will be skipped. Python files that are generated or downloaded into the workspace will not be checked. The results will be displayed on the classical Jenkins build page after the build.
31 11. Security: If it's not a feature or bugfix branch a dependency check is done which checks if e.g. libraries are used which have known vulnerabilities. The results will be displayed in Jenkins after the build.
32 11. Docker: this will also work for projects which are neither maven or nodejs. A Dockerfile is enough to trigger this part of the pipeline.
33 111. If a Dockerfile is found a docker image is built.
34 111. The image is started as an isolated container on the Jenkins agent.
35 111. Any loglines written to stdout or stderr by the container will be displayed.
36 111. A smoke test is performed which is a simple query for a valid answer on the exposed port of the container.
37 111. If the smoke test was successful and the build was not done for a pull request the docker image will be pushed to the docker registry.
38 111. For easy identification of the image 3 image tags are defined:
39 1111. BRANCH_NAME-BUILD_NUMBER (e.g. 'production-1014')
40 1111. BRANCH_NAME-GIT_HASH (e.g. 'develop-8a7c4f2')
41 1111. BRANCH_NAME-latest (e.g. 'feature-PKEY-42-latest')
42 1111. (If BRANCH_NAME is defaultBranch the prefix 'BRANCH_NAME-' will not be included.)
43 111. When a chart/Chart.yaml is found and  it's not a pull request a Helm Chart will be created and pushed to the Helm Chart repository.
44 1. Yamllint will check all YAML files in the workspace for errors and warnings. This is done at this place since the Create Helm Chart stage modifies or creates YAML files which should be also checked before the pipeline proceeds.
45 1. When depolyHelmChart is set to true the Helm chart will be deployed to the Kubernestes cluster and namespace of your choice. Not done in the example diagram.
46
47 == {{id name="pipeline_customization"/}}Pipeline Customization ==
48
49 By passing named arguments to sdcPipline() e.g. sdcPipeline(imageName: 'nginx-demo', imageTag: 'v1.4') you can change the default configuration of the pipeline.
50
51 (% class="table-bordered" style="width:100.0%" %)
52 (% class="active" %)|=(((
53 Category
54 )))|=(((
55 Argument
56 )))|=(((
57 Default
58 )))|=(((
59 Description
60 )))
61 |=(% rowspan="4" %)(((
62 Job execution
63 )))|(((
64 decorateOutput
65 )))|(((
66 true
67 )))|(((
68 Enables timestamps and colorization for the console output when set to true.
69 )))
70 |allocateNode|true|(((
71 Specifies if a node should be allocated for executing the sdcPipeline code.
72
73 If you set this to false you will need to put your sdcPipeline() call into a node {} block.
74 )))
75 |(((
76 useNode
77 )))|(((
78 'docker'
79 )))|(((
80 Specifies which node should be allocated and used by the pipeline if allocateNode is true.
81
82 Pass a label or name of a configured Jenkins agent.
83 )))
84 |(((
85 defaultBranch
86 )))|(((
87 'master'
88 )))|(((
89 This branch will be the integration branch for work. At some points the pipeline will behave differently, if the default branch or another branch ist currently being built.
90 )))
91 |=(% rowspan="5" %)(((
92 Programming language and build tool
93 )))|(((
94 jdk
95 )))|(((
96 'jdk11'
97 )))|(((
98 JDK to use for all Java operations. Refers to a symbolic name of a Java tool configuration in Jenkins.
99 )))
100 |(((
101 mvnCommand
102 )))|(((
103 'mvn clean verify'
104 )))|(((
105 maven command to execute for building maven projects.
106 )))
107 |(((
108 mavenSettingsConfig
109 )))|(((
110 ''
111 )))|(((
112 Select a maven settings element from File Config Provider. The settings element in the settings.xml file contains elements used to define values which configure Maven execution in various ways, like the pom.xml, but should not be bundled to any specific project, or distributed to an audience. These include values such as the local repository location, alternate remote repository servers, and authentication information.
113
114 See also [[doc:Jenkins.Using Maven Artifact Repositories in Jenkins.WebHome]].
115 )))
116 |(((
117 nodejs
118 )))|(((
119 'nodejs'
120 )))|(((
121 Node.js to use for all node operations like e.g. calling npm. Refers to a symbolic name of a nodejs tool configuration in Jenkins.
122 )))
123 |(((
124 npmCommand
125 )))|(((
126 'npm install && npm run build ~-~-prod'
127 )))|(((
128 npm command to execute for building Node.JS projects.
129 )))
130 |=(% rowspan="10" %)(((
131 Docker build
132 )))|(((
133 dockerBuildPath
134 )))|(((
135 '.' (current directory)
136 )))|(((
137 Path to use for building the Docker container.
138 )))
139 |(((
140 dockerBuildArgs
141 )))|(((
142 '' (empty string)
143 )))|(((
144 Any additional arguments for Docker build.
145 )))
146 |(((
147 dockerfile
148 )))|(((
149 dockerBuildPath/Dockerfile
150 )))|(((
151 Dockerfile to use for building the Docker container.
152 )))
153 |(((
154 imageName
155 )))|(((
156 artifactId from pom.xml for maven projects
157
158 name of the build job for all other project types
159 )))|(((
160 Name to be used for the built Docker image.
161 )))
162 |(((
163 imagePath
164 )))|(((
165 Name of the project folder in Jenkins. That's the PROJECTKEY from the self-service portal
166 )))|(((
167 Path to prepend before the imageName.
168 )))
169 |(((
170 imageTag
171 )))|(((
172 nothing
173 )))|(((
174 Additional tag to set on the container. It will be automatically prefixed with the branch name, except for the defaultBranch.
175 )))
176 |(((
177 gitHashLength
178 )))|(((
179 7
180 )))|(((
181 Number of digits from the git hash that will be used as an image tag on the container image to label the build.
182 )))
183 |(((
184 gitCredentialsId
185 )))|(((
186 'PROJECTKEY-git' (credential which was added to your project folder in Jenkins when the project was set up by the self-service portal)
187 )))|(((
188 Name of credentials used to connect to Bitbucket API. May be used to connect to alternative SCM tools in the future.
189 )))
190 |(((
191 pullDockerRegistry
192 )))|(((
193 '[[https:~~/~~/registry-CUSTOMER.devops.t-systems.net>>url:https://registry-CUSTOMER.devops.t-systems.net||shape="rect"]]' (docker registry of your DOaaS instance)
194 )))|(((
195 If you need to pull base images during the Docker build from a docker registry that requires authentication you have to use this to specify it.
196 )))
197 |(((
198 pullDockerRegistryCredentialsId
199 )))|(((
200 'doaas-PROJECTKEY+jenkins-push-harbor' or 'PROJECTKEY-jenkins' (which were added to the credentials of your project folder in Jenkins when the project was set up by the DevOps Portal. While the first is added for Harbor, the second is added for Nexus OSS. That means that the library will automatically choose the best default for you. Please note that Harbor will be prefered, if both tools are used in the project.)
201 )))|(((
202 Id of the Jenkins Credentials which have to be used to authenticate to the //pullDockerRegistry//.
203 )))
204 |=(% rowspan="7" %)(((
205 Docker container test
206 )))|(((
207 skipSmokeTest
208 )))|(((
209 false
210 )))|(((
211 Set to true to skip the container smoke test after the build.
212 )))
213 |(((
214 containerArgs
215 )))|(((
216 '' (empty string)
217 )))|(((
218 Arguments to pass to the container for the smoke test.
219 )))
220 |(((
221 containerPort
222 )))|(((
223 80
224 )))|(((
225 Exposed port of the container that is used to check for an answer during the smoke test.
226 )))
227 |(((
228 containerProtocol
229 )))|(((
230 'http'
231 )))|(((
232 Protocol to use when trying to connect to the port of the container. Possible values: 'http', 'tcp', 'udp'. 'http' will be tested with curl, 'tcp' and 'udp' will be tested with ncat.
233 )))
234 |(((
235 containerContextPath
236 )))|(((
237 '' (empty string)
238 )))|(((
239 contextPath to query for the smoke test if the container protocol is 'http'
240 )))
241 |(((
242 containerMaxLoglineTime
243 )))|(((
244 15
245 )))|(((
246 After starting the container the smoke tests monitors stdout of the container for loglines. After containerMaxLoglineTime which is specified in seconds have passed without a new logline found, the container is expected to be up and running and the test query is performed. Increase if your container doesn't write loglines that often and seems to be not ready when the test query is performed.
247 )))
248 |(((
249 containerMaxStartupTime
250 )))|(((
251 180 (3 minutes)
252 )))|(((
253 Total time in seconds after which the container is expected to be up and running even if it's still writing loglines to stdout. After this time has passed the container will be queried for an answer.
254 )))
255 |=(% rowspan="2" %)(((
256 Docker push
257 )))|(((
258 pushDockerRegistry
259 )))|(((
260 '[[https:~~/~~/registry-CUSTOMER.devops.t-systems.net>>url:https://registry-CUSTOMER.devops.t-systems.net||shape="rect"]]' (docker registry of your DOaaS instance)
261 )))|(((
262 Docker registry to which the built image will be pushed after a successful smoke test.
263 )))
264 |(((
265 pushDockerRegistryCredentialsId
266 )))|(((
267 'doaas-PROJECTKEY+jenkins-push-harbor' or 'PROJECTKEY-jenkins' (which were added to the credentials of your project folder in Jenkins when the project was set up by the DevOps Portal. While the first is added for Harbor, the second is added for Nexus OSS. That means that the library will automatically choose the best default for you. Please note that Harbor will be prefered, if both tools are used in the project.)
268 )))|(((
269 Id of the Jenkins Credentials which have to be used to authenticate to the //pullDockerRegistry//.
270 )))
271 |=(% rowspan="6" %)(((
272 Helm chart
273 )))|(((
274 helmChartPath
275 )))|(((
276 './chart'
277 )))|(((
278 Specifies the path to the YAML files for the helm command.
279 )))
280 |(((
281 appName
282 )))|(((
283 artifactId from pom.xml for maven projects
284
285 name of the build job for all other project types
286 )))|(((
287 Name of the application. Used to build the name of the Helm chart.
288 )))
289 |(((
290 appVersion
291 )))|(((
292 version from pom.xml for maven projects
293
294 '1.0.0' for all other project types
295 )))|(((
296 Version of the application. Used to set the app version of the Helm chart.
297 )))
298 |(((
299 chartVersion
300 )))|(((
301 appVersion with '00' appended
302 )))|(((
303 Version of the Helm chart. Used to set the chart version of the Helm chart.
304 )))
305 |(((
306 helmRegistry
307 )))|(((
308 Nexus registry of your DOaaS instance
309 )))|(((
310 Name of registry to which the packaged Helm chart is uploaded.
311 )))
312 |(((
313 helmRegistryCredentialsId
314 )))|(((
315 'doaas-PROJECTKEY+jenkins-push-harbor' or 'PROJECTKEY-jenkins' (which were added to the credentials of your project folder in Jenkins when the project was set up by the DevOps Portal. While the first is added for Harbor, the second is added for Nexus OSS. That means that the library will automatically choose the best default for you. Please note that Harbor will be prefered, if both tools are used in the project.)
316 )))|(((
317 Id of the Jenkins Credentials which have to be used to authenticate to the Helm registry for acccessing Helm charts.
318 )))
319 |=(% rowspan="4" %)(((
320 Container image signature
321 )))|(((
322 signImages
323 )))|(((
324 false
325 )))|(((
326 When set to true, it activates signing of generated Docker images.
327 )))
328 |(((
329 signingNotaryServer
330 )))|(((
331 '[[https:~~/~~/notary.external.otc.telekomcloud.com>>url:https://notary.external.otc.telekomcloud.com||shape="rect"]]' (Magenta Trusted Registry)
332 )))|(((
333 Specifies the Notary service to be used for signing.
334 )))
335 |(((
336 signingPassphraseCredentialsId
337 )))|(((
338 'docker-trust-PROJECTKEY-passphrase'
339 )))|(((
340 Id of the Jenkins Credentials for passphrase of signers keyfile.
341 )))
342 |(((
343 signingKeyfileCredentialsId
344 )))|(((
345 'docker-trust-PROJECTKEY-keyfile'
346 )))|(((
347 Id of the Jenkins Credentials for signers private keyfile.
348 )))
349 |=(% colspan="1" rowspan="12" %)(((
350 Static Source Code Analysis
351 )))|(((
352 checkstyleConfig
353 )))|(((
354 best-practice releaxed configuration
355 )))|(((
356 Name of a config file to use for checkstyle. If not set a best-practice[[ relaxed configuration >>url:https://prd.sdc.t-systems.net/bitbucket/projects/DEVOPSAAS/repos/sdcloud-caas-jenkins-libs/browse/resources/com/tsystems/sdc/jenkinslib/checkstyle.xml||shape="rect"]]is used which is different from the original  Sun[[ Java Style>>url:https://checkstyle.org/sun_style.html||shape="rect"]].
357 To get the old behavior simply specify 'sun_checks.xml' to get the original Sun Checks. As an alternative 'google_checks.xml' can be used for the Google Checks which is another ruleset predefined in the checkstyle scanner tool.
358 For a custom configuration specify a filesystem path, a URL, or a classpath resource. This parameter expects that the contents of the location conforms to the xml format (Checkstyle Checker module) configuration of rulesets.
359 )))
360 |(((
361 skipBlames
362 )))|(((
363 false
364 )))|(((
365 If set to true all recordIssues steps will skip creating the SCM blames. This speeds up the processing of the results of static source code analyis. As a consequence you will not be able to see who introduced a problem into the code.
366 )))
367 |(((
368 yamlConfig
369 )))|(((
370 best-practice releaxed configuration
371 )))|(((
372 Name of a config file to use for yamllint. If not set a best-practice[[ relaxed configuration >>url:https://prd.sdc.t-systems.net/bitbucket/projects/DEVOPSAAS/repos/sdcloud-caas-jenkins-libs/browse/resources/com/tsystems/sdc/jenkinslib/yamllint.yml||shape="rect"]]is used which is different from the original yamllint[[ config>>url:https://yamllint.readthedocs.io/en/stable/configuration.html#default-configuration||shape="rect"]].
373 )))
374 |skipDependencyCheck|false|Set to true to skip the dependency-check.
375 |(((
376 dependencyCheckTool
377 )))|(((
378 'dependency-check'
379 )))|(((
380 Defines which named dependency-check tool should be used.
381 )))
382 |(((
383 dependencyCheckArgs
384 )))|(((
385 '~-~-disableAssembly ~-~-nvdValidForHours 720'
386 )))|(((
387 Addtional arguments which are be passed to dependency-check. See [[Dependency>>url:https://jeremylong.github.io/DependencyCheck/dependency-check-cli/arguments.html||shape="rect"]][[ Check CLI Arguments>>url:https://jeremylong.github.io/DependencyCheck/dependency-check-cli/arguments.html||shape="rect"]] for more information.
388 )))
389 |(((
390 dependencyCheckNvdApiKeyCredentialsId
391 )))|(((
392 'dependency-check-nvdapikey'
393 )))|(((
394 If you have your own NVD API Key, set it as a credential of type text in Jenkins. Then specify the credential id using this argument. It will be automatically passed to dependency-check. There will be no error if no credential is found. Just the NVD download will be slower. Please note, on DevOps-as-a-Service a shared NVD API Key is automatically supplied for the default credential id.
395 )))
396 |(((
397 sonarQube
398 )))|(((
399 true for the defaultBranch
400
401 false for all other branches and pull-requests
402 )))|(((
403 Boolean which determines if SonarQube should be used if an installation is found.
404
405 The default is the best choice for the free community edition of SonarQube, which supports just one branch.
406
407 If you have the Developer or Enterprise edition consider setting sonarQube to true to cover all branches. For more information see [[SonarQube>>SonarQube.WebHome]].
408 )))
409 |(((
410 sonarScanMavenOpts
411 )))|(((
412 ''
413 )))|(((
414 For huge source codes it can happen that the sonar scanner aborts with java.lang.OutOfMemoryError. In this case the memory that is availalbe for the sonar scanner needs to be increased. Try something like '-Xmx512m' to set a larger heap space for the JVM that is executing the maven-based sonar scan. '-Xmx3g' should be enough for most source codes, but make sure that your Jenkins agent has enough memory to cope with that.
415 )))
416 |sonarScanOpts|''|For huge source codes it can happen that the sonar scanner aborts with java.lang.OutOfMemoryError. In this case the memory that is availalbe for the sonar scanner needs to be increased. Try something like '-Xmx512m' to set a larger heap space for the JVM that is executing the
417 sonar scan. '-Xmx3g' should be enough for most source codes, but make sure that your Jenkins agent has enough memory to cope with that.
418 |(((
419 sonarSources
420 )))|(((
421 'src/main' for maven projects
422
423 null for non-maven projects
424 )))|(((
425 (% class="content-wrapper" %)
426 (((
427 Comma-separated paths to directories containing main source files to scan with SonarQube. Defaults to 'src/main' and therefore will automatically discover folders like src/main/java, src/main/webapp or src/main/resources. This makes sure to not only scan Java sources, but also for example JSP, CSS and HTML files.
428
429 Sonar scanning will fail if the configured directory itself is not found at all. This can happen if you have defined an alternative <sourceDirectory> in your pom.xml. If this is the case, you can disable sonar scanning by adding
430
431 {{code language="xml"}}
432 <properties>
433 <sonar.skip>true</sonar.skip>
434 </properties>
435 {{/code}}
436
437 to the pom.xml. If this is not what you want, set sonarSources to {{code language="none"}}null{{/code}}. This will make sure that sonar.sources are not explicitly passed to the sonar scanner. This will allow you to specify sonar.sources in the pom.xml like in the following example:
438
439 {{code language="xml"}}
440 <properties>
441 <sonar.source>src/main/java,src/main/webapp</sonar.sources>
442 </properties>
443 {{/code}}
444 )))
445 )))
446 |sonarQualityGate| |Sets the desired quality gate to use for the scan result in SonarQube.
447 If not specified, the quality gate is not changed.
448 As a default, SonarQube will use the quality gate "Sonar way" for new scan results.
449 |=(% rowspan="7" %)(((
450 Deployment
451 )))|(((
452 deployHelmChart
453 )))|(((
454 false
455 )))|(((
456 Boolean that activates or deactivates the automatic deployment of the Helm-Chart.
457 )))
458 |(((
459 kubeconfigCredentialsId
460 )))|(((
461 'kubeconfig-deployer-PROJECTKEY'
462 )))|(((
463 Id of the Jenkins credential which contains the kubeconfig file that should be used for deployment. The file is required to authorize access to the Kubernetes cluster.
464 )))
465 |(((
466 kubernetesNamespace
467 )))|(((
468 'default'
469 )))|(((
470 Name of the namespace in Kubernetes, to which the Chart will be deployed.
471 )))
472 |(((
473 helmValuesOverrideFile
474 )))|(((
475 'chart/values.yaml'
476 )))|(((
477 Name of the YAML file which contains specific values that should be overridden for the automatic deployment. The location needs to be specified relative to the Jenkinsfile.
478 )))
479 |(((
480 helmSetValues
481 )))|(((
482 ''
483 )))|(((
484 Set values using the format "key1=val1,key2=val2,..." for the automatic deployment.
485
486 While helmValuesOverrideFile is usally something static you can use this to specify different values depending on e.g. conditions in your Jenkinsfile.
487
488 Will be ignored if set to null, empty string or just white space.
489 )))
490 |(((
491 helmReleaseName
492 )))|(((
493 artifactId from pom.xml for maven projects
494
495 name of the build job for all other project types
496 )))|(((
497 Name of the Helm Release which will be installed or upgraded in your cluster.
498 )))
499 |(((
500 helmLockResource
501 )))|(((
502 null
503 )))|(((
504 Using [[lock>>url:https://www.jenkins.io/doc/pipeline/steps/lockable-resources/#lock-lock-shared-resource||shape="rect"]] the specified resource will be locked for the execution of any
505
506 (% class="code" %)
507 (((
508 helm upgrade
509 )))
510
511 or
512
513 (% class="code" %)
514 (((
515 helm uninstall
516 )))
517
518 command. This makes sense if deployHelmChart is used to upgrade an deployment in an environment which is currently used, e.g. for automated testing. In this case wrap your test in a lock statement with the same resource name. Defaults to null, which will not lock any resource.
519 )))