|
| 1 | +pipeline { |
| 2 | + agent any |
| 3 | + stages { |
| 4 | + stage('Example Build') { |
| 5 | + steps { |
| 6 | + echo 'Hello World' |
| 7 | + } |
| 8 | + } |
| 9 | + stage('Example Deploy') { |
| 10 | + when { |
| 11 | + branch 'production' |
| 12 | + } |
| 13 | + steps { |
| 14 | + echo 'Deploying' |
| 15 | + } |
| 16 | + } |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +Multiple Condition, Declarative Pipeline |
| 21 | +######################## |
| 22 | + |
| 23 | +pipeline { |
| 24 | + agent any |
| 25 | + stages { |
| 26 | + stage('Example Build') { |
| 27 | + steps { |
| 28 | + echo 'Hello World' |
| 29 | + } |
| 30 | + } |
| 31 | + stage('Example Deploy') { |
| 32 | + when { |
| 33 | + branch 'production' |
| 34 | + environment name: 'DEPLOY_TO', value: 'production' |
| 35 | + } |
| 36 | + steps { |
| 37 | + echo 'Deploying' |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +Nested condition (same behavior as previous example) |
| 44 | +########################### |
| 45 | + |
| 46 | +pipeline { |
| 47 | + agent any |
| 48 | + stages { |
| 49 | + stage('Example Build') { |
| 50 | + steps { |
| 51 | + echo 'Hello World' |
| 52 | + } |
| 53 | + } |
| 54 | + stage('Example Deploy') { |
| 55 | + when { |
| 56 | + allOf { |
| 57 | + branch 'production' |
| 58 | + environment name: 'DEPLOY_TO', value: 'production' |
| 59 | + } |
| 60 | + } |
| 61 | + steps { |
| 62 | + echo 'Deploying' |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | + |
| 69 | +Multiple condition and nested condition |
| 70 | +#################################### |
| 71 | + |
| 72 | +pipeline { |
| 73 | + agent any |
| 74 | + stages { |
| 75 | + stage('Example Build') { |
| 76 | + steps { |
| 77 | + echo 'Hello World' |
| 78 | + } |
| 79 | + } |
| 80 | + stage('Example Deploy') { |
| 81 | + when { |
| 82 | + branch 'production' |
| 83 | + anyOf { |
| 84 | + environment name: 'DEPLOY_TO', value: 'production' |
| 85 | + environment name: 'DEPLOY_TO', value: 'staging' |
| 86 | + } |
| 87 | + } |
| 88 | + steps { |
| 89 | + echo 'Deploying' |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | + |
| 96 | +triggeredBy |
| 97 | +######################## |
| 98 | + |
| 99 | +pipeline { |
| 100 | + agent none |
| 101 | + stages { |
| 102 | + stage('Example Build') { |
| 103 | + steps { |
| 104 | + echo 'Hello World' |
| 105 | + } |
| 106 | + } |
| 107 | + stage('Example Deploy') { |
| 108 | + when { |
| 109 | + triggeredBy "TimerTrigger" |
| 110 | + } |
| 111 | + steps { |
| 112 | + echo 'Deploying' |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | + |
| 119 | +beforeOptions: |
| 120 | +######################## |
| 121 | +pipeline { |
| 122 | + agent none |
| 123 | + stages { |
| 124 | + stage('Example Build') { |
| 125 | + steps { |
| 126 | + echo 'Hello World' |
| 127 | + } |
| 128 | + } |
| 129 | + stage('Example Deploy') { |
| 130 | + when { |
| 131 | + beforeOptions true |
| 132 | + branch 'testing' |
| 133 | + } |
| 134 | + options { |
| 135 | + lock label: 'testing-deploy-envs', quantity: 1, variable: 'deployEnv' |
| 136 | + } |
| 137 | + steps { |
| 138 | + echo "Deploying to ${deployEnv}" |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +beforeInput |
| 145 | +##################### |
| 146 | +pipeline { |
| 147 | + agent none |
| 148 | + stages { |
| 149 | + stage('Example Build') { |
| 150 | + steps { |
| 151 | + echo 'Hello World' |
| 152 | + } |
| 153 | + } |
| 154 | + stage('Example Deploy') { |
| 155 | + when { |
| 156 | + beforeInput true |
| 157 | + branch 'production' |
| 158 | + } |
| 159 | + input { |
| 160 | + message "Deploy to production?" |
| 161 | + id "simple-input" |
| 162 | + } |
| 163 | + steps { |
| 164 | + echo 'Deploying' |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | + |
| 171 | +Sequential Stages |
| 172 | +######################## |
| 173 | +Stages in Declarative Pipeline may have a stages section containing a list of nested stages to be run in sequential order. Note that a stage must have one and only one of steps, stages, parallel, or matrix. It is not possible to nest a parallel or matrix block within a stage directive if that stage directive is nested within a parallel or matrix block itself. However, a stage directive within a parallel or matrix block can use all other functionality of a stage, including agent, tools, when, etc. |
| 174 | + |
| 175 | + Sequential Stages, Declarative Pipeline |
| 176 | +pipeline { |
| 177 | + agent none |
| 178 | + stages { |
| 179 | + stage('Non-Sequential Stage') { |
| 180 | + agent { |
| 181 | + label 'for-non-sequential' |
| 182 | + } |
| 183 | + steps { |
| 184 | + echo "On Non-Sequential Stage" |
| 185 | + } |
| 186 | + } |
| 187 | + stage('Sequential') { |
| 188 | + agent { |
| 189 | + label 'for-sequential' |
| 190 | + } |
| 191 | + environment { |
| 192 | + FOR_SEQUENTIAL = "some-value" |
| 193 | + } |
| 194 | + stages { |
| 195 | + stage('In Sequential 1') { |
| 196 | + steps { |
| 197 | + echo "In Sequential 1" |
| 198 | + } |
| 199 | + } |
| 200 | + stage('In Sequential 2') { |
| 201 | + steps { |
| 202 | + echo "In Sequential 2" |
| 203 | + } |
| 204 | + } |
| 205 | + stage('Parallel In Sequential') { |
| 206 | + parallel { |
| 207 | + stage('In Parallel 1') { |
| 208 | + steps { |
| 209 | + echo "In Parallel 1" |
| 210 | + } |
| 211 | + } |
| 212 | + stage('In Parallel 2') { |
| 213 | + steps { |
| 214 | + echo "In Parallel 2" |
| 215 | + } |
| 216 | + } |
| 217 | + } |
| 218 | + } |
| 219 | + } |
| 220 | + } |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | + |
| 225 | +Parallel |
| 226 | +Stages in Declarative Pipeline may have a parallel section containing a list of nested stages to be run in parallel. Note that a stage must have one and only one of steps, stages, parallel, or matrix. It is not possible to nest a parallel or matrix block within a stage directive if that stage directive is nested within a parallel or matrix block itself. However, a stage directive within a parallel or matrix block can use all other functionality of a stage, including agent, tools, when, etc. |
| 227 | + |
| 228 | +In addition, you can force your parallel stages to all be aborted when any one of them fails, by adding failFast true to the stage containing the parallel. Another option for adding failfast is adding an option to the pipeline definition: parallelsAlwaysFailFast() |
| 229 | + |
| 230 | + |
| 231 | +Parallel Stages, Declarative Pipeline |
| 232 | +################## |
| 233 | +pipeline { |
| 234 | + agent any |
| 235 | + stages { |
| 236 | + stage('Non-Parallel Stage') { |
| 237 | + steps { |
| 238 | + echo 'This stage will be executed first.' |
| 239 | + } |
| 240 | + } |
| 241 | + stage('Parallel Stage') { |
| 242 | + when { |
| 243 | + branch 'master' |
| 244 | + } |
| 245 | + failFast true |
| 246 | + parallel { |
| 247 | + stage('Branch A') { |
| 248 | + agent { |
| 249 | + label "for-branch-a" |
| 250 | + } |
| 251 | + steps { |
| 252 | + echo "On Branch A" |
| 253 | + } |
| 254 | + } |
| 255 | + stage('Branch B') { |
| 256 | + agent { |
| 257 | + label "for-branch-b" |
| 258 | + } |
| 259 | + steps { |
| 260 | + echo "On Branch B" |
| 261 | + } |
| 262 | + } |
| 263 | + stage('Branch C') { |
| 264 | + agent { |
| 265 | + label "for-branch-c" |
| 266 | + } |
| 267 | + stages { |
| 268 | + stage('Nested 1') { |
| 269 | + steps { |
| 270 | + echo "In stage Nested 1 within Branch C" |
| 271 | + } |
| 272 | + } |
| 273 | + stage('Nested 2') { |
| 274 | + steps { |
| 275 | + echo "In stage Nested 2 within Branch C" |
| 276 | + } |
| 277 | + } |
| 278 | + } |
| 279 | + } |
| 280 | + } |
| 281 | + } |
| 282 | + } |
| 283 | +} |
| 284 | +parallelsAlwaysFailFast |
| 285 | +###################### |
| 286 | +pipeline { |
| 287 | + agent any |
| 288 | + options { |
| 289 | + parallelsAlwaysFailFast() |
| 290 | + } |
| 291 | + stages { |
| 292 | + stage('Non-Parallel Stage') { |
| 293 | + steps { |
| 294 | + echo 'This stage will be executed first.' |
| 295 | + } |
| 296 | + } |
| 297 | + stage('Parallel Stage') { |
| 298 | + when { |
| 299 | + branch 'master' |
| 300 | + } |
| 301 | + parallel { |
| 302 | + stage('Branch A') { |
| 303 | + agent { |
| 304 | + label "for-branch-a" |
| 305 | + } |
| 306 | + steps { |
| 307 | + echo "On Branch A" |
| 308 | + } |
| 309 | + } |
| 310 | + stage('Branch B') { |
| 311 | + agent { |
| 312 | + label "for-branch-b" |
| 313 | + } |
| 314 | + steps { |
| 315 | + echo "On Branch B" |
| 316 | + } |
| 317 | + } |
| 318 | + stage('Branch C') { |
| 319 | + agent { |
| 320 | + label "for-branch-c" |
| 321 | + } |
| 322 | + stages { |
| 323 | + stage('Nested 1') { |
| 324 | + steps { |
| 325 | + echo "In stage Nested 1 within Branch C" |
| 326 | + } |
| 327 | + } |
| 328 | + stage('Nested 2') { |
| 329 | + steps { |
| 330 | + echo "In stage Nested 2 within Branch C" |
| 331 | + } |
| 332 | + } |
| 333 | + } |
| 334 | + } |
| 335 | + } |
| 336 | + } |
| 337 | + } |
| 338 | +} |
| 339 | +Matrix |
| 340 | +Stages in Declarative Pipeline may have a matrix section defining a multi-dimensional matrix of name-value combinations to be run in parallel. We’ll refer these combinations as "cells" in a matrix. Each cell in a matrix can include one or more stages to be run sequentially using the configuration for that cell. Note that a stage must have one and only one of steps, stages, parallel, or matrix. It is not possible to nest a parallel or matrix block within a stage directive if that stage directive is nested within a parallel or matrix block itself. However, a stage directive within a parallel or matrix block can use all other functionality of a stage, including agent, tools, when, etc. |
| 341 | + |
| 342 | +In addition, you can force your matrix cells to all be aborted when any one of them fails, by adding failFast true to the stage containing the matrix. Another option for adding failfast is adding an option to the pipeline definition: parallelsAlwaysFailFast() |
| 343 | + |
| 344 | +The matrix section must include an axes section and a stages section. The axes section defines the values for each axis in the matrix. The stages section defines a list of stages to run sequentially in each cell. A matrix may have an excludes section to remove invalid cells from the matrix. Many of the directives available on stage, including agent, tools, when, etc., can also be added to matrix to control the behavior of each cell. |
| 345 | + |
| 346 | +axes |
| 347 | +The axes section specifies one or more axis directives. Each axis consists of a name and a list of values. All the values from each axis are combined with the others to produce the cells. |
| 348 | + |
| 349 | +One-axis with 3 cells |
| 350 | +############################ |
| 351 | +matrix { |
| 352 | + axes { |
| 353 | + axis { |
| 354 | + name 'PLATFORM' |
| 355 | + values 'linux', 'mac', 'windows' |
| 356 | + } |
| 357 | + } |
| 358 | + // ... |
| 359 | +} |
| 360 | +Two-axis with 12 cells (three by four) |
| 361 | +##################### |
| 362 | +matrix { |
| 363 | + axes { |
| 364 | + axis { |
| 365 | + name 'PLATFORM' |
| 366 | + values 'linux', 'mac', 'windows' |
| 367 | + } |
| 368 | + axis { |
| 369 | + name 'BROWSER' |
| 370 | + values 'chrome', 'edge', 'firefox', 'safari' |
| 371 | + } |
| 372 | + } |
| 373 | + // ... |
| 374 | +} |
0 commit comments