我们可以在 Jenkinsfile 的 'stages' 中应用 'when' 条件吗?
Can we apply 'when' condition in 'stages' of a Jenkinsfile?
我有一个 Jenkinsfile
,它将由 GitLab(使用 Webhooks)触发。
如果不满足特定条件,我想跳过整个 Jenkinsfile
的执行。
一种方法是在每个阶段应用相同的条件
pipeline {
agent any
stages {
stage ('Stage 1') {
when {
expression {
//Expression
}
}
//do something
}
stage ('Stage 2') {
when {
expression {
//Expression
}
}
//do something
}
stage ('Stage 3') {
when {
expression {
//Expression
}
}
//do something
}
.
.
.
.
}
}
但这看起来很奇怪,因为我希望对所有阶段应用相同的条件。
我们可以对 stages
本身应用类似的条件吗?像这样?
pipeline {
agent any
stages {
when {
expression {
//Expression
}
}
stage ('Stage 1') {
//do something
}
stage ('Stage 2') {
//do something
}
stage ('Stage 3') {
//do something
}
.
.
.
.
}
}
“我们可以在 Jenkinsfile 的 'stages' 中应用 'when' 条件吗?”编号
每Pipeline Syntax, when只允许在一个阶段.
The when directive allows the Pipeline to determine whether the stage should be executed depending on the given condition. The when directive must contain at least one condition. If the when directive contains more than one condition, all the child conditions must return true for the stage to execute. This is the same as if the child conditions were nested in an allOf condition (see the examples below). If an anyOf condition is used, note that the condition skips remaining tests as soon as the first "true" condition is found.
More complex conditional structures can be built using the nesting conditions: not, allOf, or anyOf. Nesting conditions may be nested to any arbitrary depth.
你能简单地使用表达式和 fail/abort 管道的第一阶段吗?
我有一个 Jenkinsfile
,它将由 GitLab(使用 Webhooks)触发。
如果不满足特定条件,我想跳过整个 Jenkinsfile
的执行。
一种方法是在每个阶段应用相同的条件
pipeline {
agent any
stages {
stage ('Stage 1') {
when {
expression {
//Expression
}
}
//do something
}
stage ('Stage 2') {
when {
expression {
//Expression
}
}
//do something
}
stage ('Stage 3') {
when {
expression {
//Expression
}
}
//do something
}
.
.
.
.
}
}
但这看起来很奇怪,因为我希望对所有阶段应用相同的条件。
我们可以对 stages
本身应用类似的条件吗?像这样?
pipeline {
agent any
stages {
when {
expression {
//Expression
}
}
stage ('Stage 1') {
//do something
}
stage ('Stage 2') {
//do something
}
stage ('Stage 3') {
//do something
}
.
.
.
.
}
}
“我们可以在 Jenkinsfile 的 'stages' 中应用 'when' 条件吗?”编号
每Pipeline Syntax, when只允许在一个阶段.
The when directive allows the Pipeline to determine whether the stage should be executed depending on the given condition. The when directive must contain at least one condition. If the when directive contains more than one condition, all the child conditions must return true for the stage to execute. This is the same as if the child conditions were nested in an allOf condition (see the examples below). If an anyOf condition is used, note that the condition skips remaining tests as soon as the first "true" condition is found.
More complex conditional structures can be built using the nesting conditions: not, allOf, or anyOf. Nesting conditions may be nested to any arbitrary depth.
你能简单地使用表达式和 fail/abort 管道的第一阶段吗?