Wiki source code of Jenkinsfile

Last modified by Gábor Mándity on 2025/09/18 08:09

Show last authors
1 This page describes, how to do things in scripted Pipeline (Groovy) Jenkinsfiles. This [[link >>url:https://www.jenkins.io/doc/book/pipeline/jenkinsfile/||shape="rect"]]is a good starting point to learn more about Jenkinsfile.
2
3
4 {{toc/}}
5
6 = General =
7
8 The Jenkinsfile must be located in the root folder of a repository / branch, to be detected by Jenkins automatically. The Jenkinsfile defines, how the build job is performed by Jenkins.
9
10 = Properties =
11
12 At the top of the Jenkinsfile properties can be called with an array of global properties that will be set for the pipeline.
13
14 {{code language="groovy"}}
15 properties([
16 ... , ... , ...
17 ])
18 {{/code}}
19
20 Please note that another call to {{code language="none"}}properties(){{/code}} will overwrite the previous one!
21
22 == Build Discarder ==
23
24 At the top of the Jenkinsfile add a buildDiscarder to the properties to let Jenkins delete old artifacts or complete job executions:
25
26 {{code language="groovy" title="ℹ **Example**"}}
27 properties([
28 buildDiscarder(logRotator(numToKeepStr: '50', artifactNumToKeepStr: '5'))
29 ])
30 {{/code}}
31
32 Parameters for {{code language="none"}}logRotator{{/code}} (from [[the source code>>url:https://github.com/jenkinsci/jenkins/blob/master/core/src/main/java/hudson/tasks/LogRotator.java#L53||shape="rect"]]):
33
34 * {{code language="none"}}daysToKeepStr{{/code}}: history is only kept up to this days.
35 * {{code language="none"}}numToKeepStr{{/code}}: only this number of build logs are kept.
36 * {{code language="none"}}artifactDaysToKeepStr{{/code}}: artifacts are only kept up to this days.
37 * {{code language="none"}}artifactNumToKeepStr{{/code}}: only this number of builds have their artifacts kept.
38
39 == Pipeline Triggers ==
40
41 Instead of having pipelines just triggered on source code changes you can also use cron syntax to define dates and times that shoud automatically trigger the pipeline.
42
43 {{code language="groovy" title="**ℹ Example**"}}
44 properties([
45 pipelineTriggers([cron('TZ=Europe/Berlin \n 00 08 * * 1-5')])
46 ])
47 {{/code}}
48
49 The above example triggers the pipeline at 8:00 on Mo-Fr using the local German time. Without the TZ specification it will use the timezone of the Jenkins server which is UTC for DevOps-as-a-Service.
50
51 More about the cron spec is documented at [[https:~~/~~/jenkins.io/doc/book/pipeline/syntax/#cron-syntax>>url:https://jenkins.io/doc/book/pipeline/syntax/#cron-syntax||shape="rect"]].
52
53 == Durability Settings ==
54
55 At the top of the Jenkinsfile add a durabilityHint to the properties:
56
57 {{code language="groovy" title="**ℹ Example**"}}
58 properties([
59 durabilityHint('PERFORMANCE_OPTIMIZED')
60 ])
61 {{/code}}
62
63 The possible values and their meaning are documented at: [[https:~~/~~/jenkins.io/doc/book/pipeline/scaling-pipeline/>>url:https://jenkins.io/doc/book/pipeline/scaling-pipeline/||shape="rect"]]
64
65 If unsure, please don't change the default!
66
67 = Automated building =
68
69 The best and easiest approach to use the power of Jenkins pipelines is to use the automatically provided [[doc:Jenkins.Jenkins Shared Library.WebHome]] which will detect the type of your project and automatically build and test it. The pipeline also provides optional deployments to target environments. See [[doc:Jenkins.Jenkins Shared Library.WebHome]] for more information!
70
71 = Building with tool plug-ins =
72
73 Some build tools have a special plug-in support in Jenkins which means you can easily call them on any Jenkins node. Jenkins will make sure to install and update them for you. In your Jenkinsfile you just refer with symbolic names to the major version of the tool you want to use. The DevOps-as-a-Service team will make sure that the latest Long Term Support (LTS) version of the tool will be automatically available for you. Most of the time these updates are done together with the update of your Jenkins controller instance.
74
75 We strive to make the exact version of the different tools publicly available on the start page of your Jenkins. See the screenshot below as an example. Put please check the current versions available to you at [[https:~~/~~/CUSTOMER.devops.t-systems.net/jenkins/>>url:https://CUSTOMER.devops.t-systems.net/jenkins/||shape="rect"]] where CUSTOMER needs to be replaced by the name of your instance.
76
77 [[image:attach:image2022-11-10_10-27-6.png||data-xwiki-image-style-border="true" queryparams="effects=drop-shadow" height="250"]]
78
79 == Maven ==
80
81 Supported symbolic name is 'maven'. The exact version can be looked up at the start page of your Jenkins, e.g. [[https:~~/~~/CUSTOMER.devops.t-systems.net/jenkins/>>url:https://CUSTOMER.devops.t-systems.net/jenkins/||shape="rect"]]
82
83 {{code language="groovy" title="ℹ **Scripted Pipeline**"}}
84 withMaven(maven: 'maven')
85 {
86 sh 'mvn --version'
87 }
88 {{/code}}
89
90 For more information about the withMaven pipeline step see [[https:~~/~~/www.jenkins.io/doc/pipeline/steps/pipeline-maven/>>url:https://www.jenkins.io/doc/pipeline/steps/pipeline-maven/||shape="rect"]]. When you use the [[doc:Jenkins.Jenkins Shared Library.WebHome]] a maven build will be automatically done when a pom.xml file is found.
91
92 == JDK (Java Development Kit) ==
93
94 The supported symbolic names are listed at the start page of your Jenkins (e.g. [[https:~~/~~/CUSTOMER.devops.t-systems.net/jenkins/>>url:https://CUSTOMER.devops.t-systems.net/jenkins/||shape="rect"]]) and start with 'jdk' or 'corretto'.
95
96 The following examples use jdk17.
97
98 {{code language="groovy" title="**ℹ Scripted Pipeline**"}}
99 env.JAVA_HOME = "${tool 'jdk17'}"
100 env.PATH="${env.JAVA_HOME}/bin:${env.PATH}"
101 sh 'java -version'
102 {{/code}}
103
104 When you use the [[doc:Jenkins.Jenkins Shared Library.WebHome]] you simply do
105
106 {{code language="groovy" title="**ℹ Scripted Pipeline**"}}
107 @Library('sdcloud') _
108
109 sdcPipeline(jdk: 'jdk17')
110 {{/code}}
111
112 to use the JDK version of your choice. See [[doc:Jenkins.Jenkins Shared Library.WebHome||anchor="PipelineCustomization"]] for more information.
113
114 == nodejs ==
115
116 The supported symbolic names are listed at the start page of your Jenkins (e.g. [[https:~~/~~/CUSTOMER.devops.t-systems.net/jenkins/>>url:https://CUSTOMER.devops.t-systems.net/jenkins/||shape="rect"]]) and start with 'nodejs'. As an example 'nodes16' would be refering to the latest release of nodejs v16. The short name 'nodejs' is usually set to the recommended LTS version of nodejs.
117
118 The following examples use 'nodejs18'.
119
120 {{code language="groovy" title="**ℹ Scripted Pipeline**"}}
121 env.NODEJS_HOME = "${tool 'nodejs18'}"
122 env.PATH="${env.NODEJS_HOME}/bin:${env.PATH}"
123 sh 'node --version'
124 sh 'npm --version'
125 {{/code}}
126
127 When you use the [[doc:Jenkins.Jenkins Shared Library.WebHome]] you simply do
128
129 {{code language="groovy" title="**ℹ Scripted Pipeline**"}}
130 @Library('sdcloud') _
131
132 sdcPipeline(nodejs: 'nodejs18')
133 {{/code}}
134
135 to use the nodejs version of your choice. See [[doc:Jenkins.Jenkins Shared Library.WebHome||anchor="PipelineCustomization"]] for more information.
136
137 == Golang ==
138
139 The supported symbolic names are listed at the start page of your Jenkins (e.g. [[https:~~/~~/CUSTOMER.devops.t-systems.net/jenkins/>>url:https://CUSTOMER.devops.t-systems.net/jenkins/||shape="rect"]]) and start with 'go'. As an example 'go1.25' would be referring to the latest release of Golang v1.25. The short name 'go' is usually set to the recommended LTS version of Golang.
140
141 The following examples use 'go1.25'.
142
143 {{code language="groovy" title="**ℹ Scripted Pipeline**"}}
144 def root = tool type: 'go', name: go1.25
145 withEnv(["GOROOT=${root}", "PATH+GO=${root}/bin"]) {
146 sh 'go version'
147 }
148 {{/code}}
149
150 When you use the [[doc:Jenkins.Jenkins Shared Library.WebHome]] you simply do
151
152 {{code language="groovy" title="**ℹ Scripted Pipeline**"}}
153 @Library('sdcloud') _
154
155 sdcPipeline(go: 'go1.25')
156 {{/code}}
157
158 to use the go version of your choice. See [[doc:Jenkins.Jenkins Shared Library.WebHome||anchor="PipelineCustomization"]] for more information.