Jenkins 管道中的条件 step/stage
Conditional step/stage in Jenkins pipeline
如何 运行 仅在构建特定分支时构建 step/stage?
例如,运行 仅当分支被调用时才部署步骤 deployment
,其他一切保持不变。
只需使用if
和env.BRANCH_NAME
,示例:
if (env.BRANCH_NAME == "deployment") {
... do some build ...
} else {
... do something else ...
}
在声明性管道语法中做同样的事情,下面是几个例子:
stage('master-branch-stuff') {
when {
branch 'master'
}
steps {
echo 'run this stage - ony if the branch = master branch'
}
}
stage('feature-branch-stuff') {
when {
branch 'feature/*'
}
steps {
echo 'run this stage - only if the branch name started with feature/'
}
}
stage('expression-branch') {
when {
expression {
return env.BRANCH_NAME != 'master';
}
}
steps {
echo 'run this stage - when branch is not equal to master'
}
}
stage('env-specific-stuff') {
when {
environment name: 'NAME', value: 'this'
}
steps {
echo 'run this stage - only if the env name and value matches'
}
}
即将推出更有效的方法 -
https://issues.jenkins-ci.org/browse/JENKINS-41187
还看看——
https://jenkins.io/doc/book/pipeline/syntax/#when
可以设置指令 beforeAgent true
来避免将代理启动到 运行 条件,如果条件不需要 git 状态来决定是否 运行:
when { beforeAgent true; expression { return isStageConfigured(config) } }
Release post and docs
更新
新的 WHEN 子句
参考:https://jenkins.io/blog/2018/04/09/whats-in-declarative
equals - Compares two values - strings, variables, numbers, booleans -
and returns true if they’re equal. I’m honestly not sure how we missed
adding this earlier! You can do "not equals" comparisons using the not
{ equals ... } combination too.
changeRequest - In its simplest form, this will return true if this
Pipeline is building a change request, such as a GitHub pull request.
You can also do more detailed checks against the change request,
allowing you to ask "is this a change request against the master
branch?" and much more.
buildingTag - A simple condition that just checks if the Pipeline is
running against a tag in SCM, rather than a branch or a specific
commit reference.
tag - A more detailed equivalent of buildingTag, allowing you to check
against the tag name itself.
根据其他答案,我添加了并行阶段场景:
pipeline {
agent any
stages {
stage('some parallel stage') {
parallel {
stage('parallel stage 1') {
when {
expression { ENV == "something" }
}
steps {
echo 'something'
}
}
stage('parallel stage 2') {
steps {
echo 'something'
}
}
}
}
}
}
我添加这个答案是为了明确提及在 step 中使用条件而不是 stage,两者都在 声明式管道.
声明性管道阶段的条件
正如@Chandan Nayak 和其他人已经展示的那样,这可以基于 when
来完成,如
stage('myConditionalStage') {
when {
branch 'myBranch'
}
steps {
echo 'triggered by myBranch'
}
}
所以阶段 myConditionalStage
只有 运行 如果被推到 myBranch
.
声明性管道阶段步骤中的条件
如果您在阶段的步骤部分需要条件,您可以使用 Groovy 语法(在本例中为 if/else
),该语法用于 脚本化管道。如果是 声明式管道 您必须将其放入 script
块中,如下所示:
stage('myStage') {
steps {
echo 'within myStage'
script {
if (env.BRANCH_NAME == "myBranch") {
echo 'triggered by myBranch'
} else {
echo 'triggered by something else'
}
}
}
}
对于脚本化管道,您可以在没有script
块的情况下使用它,如@Krzysztof Krasoń
所示
如何 运行 仅在构建特定分支时构建 step/stage?
例如,运行 仅当分支被调用时才部署步骤 deployment
,其他一切保持不变。
只需使用if
和env.BRANCH_NAME
,示例:
if (env.BRANCH_NAME == "deployment") {
... do some build ...
} else {
... do something else ...
}
在声明性管道语法中做同样的事情,下面是几个例子:
stage('master-branch-stuff') {
when {
branch 'master'
}
steps {
echo 'run this stage - ony if the branch = master branch'
}
}
stage('feature-branch-stuff') {
when {
branch 'feature/*'
}
steps {
echo 'run this stage - only if the branch name started with feature/'
}
}
stage('expression-branch') {
when {
expression {
return env.BRANCH_NAME != 'master';
}
}
steps {
echo 'run this stage - when branch is not equal to master'
}
}
stage('env-specific-stuff') {
when {
environment name: 'NAME', value: 'this'
}
steps {
echo 'run this stage - only if the env name and value matches'
}
}
即将推出更有效的方法 -
https://issues.jenkins-ci.org/browse/JENKINS-41187
还看看——
https://jenkins.io/doc/book/pipeline/syntax/#when
可以设置指令 beforeAgent true
来避免将代理启动到 运行 条件,如果条件不需要 git 状态来决定是否 运行:
when { beforeAgent true; expression { return isStageConfigured(config) } }
Release post and docs
更新
新的 WHEN 子句
参考:https://jenkins.io/blog/2018/04/09/whats-in-declarative
equals - Compares two values - strings, variables, numbers, booleans - and returns true if they’re equal. I’m honestly not sure how we missed adding this earlier! You can do "not equals" comparisons using the not { equals ... } combination too.
changeRequest - In its simplest form, this will return true if this Pipeline is building a change request, such as a GitHub pull request. You can also do more detailed checks against the change request, allowing you to ask "is this a change request against the master branch?" and much more.
buildingTag - A simple condition that just checks if the Pipeline is running against a tag in SCM, rather than a branch or a specific commit reference.
tag - A more detailed equivalent of buildingTag, allowing you to check against the tag name itself.
根据其他答案,我添加了并行阶段场景:
pipeline {
agent any
stages {
stage('some parallel stage') {
parallel {
stage('parallel stage 1') {
when {
expression { ENV == "something" }
}
steps {
echo 'something'
}
}
stage('parallel stage 2') {
steps {
echo 'something'
}
}
}
}
}
}
我添加这个答案是为了明确提及在 step 中使用条件而不是 stage,两者都在 声明式管道.
声明性管道阶段的条件
正如@Chandan Nayak 和其他人已经展示的那样,这可以基于 when
来完成,如
stage('myConditionalStage') {
when {
branch 'myBranch'
}
steps {
echo 'triggered by myBranch'
}
}
所以阶段 myConditionalStage
只有 运行 如果被推到 myBranch
.
声明性管道阶段步骤中的条件
如果您在阶段的步骤部分需要条件,您可以使用 Groovy 语法(在本例中为 if/else
),该语法用于 脚本化管道。如果是 声明式管道 您必须将其放入 script
块中,如下所示:
stage('myStage') {
steps {
echo 'within myStage'
script {
if (env.BRANCH_NAME == "myBranch") {
echo 'triggered by myBranch'
} else {
echo 'triggered by something else'
}
}
}
}
对于脚本化管道,您可以在没有script
块的情况下使用它,如@Krzysztof Krasoń