詹金斯的工作能否成功中止?
Can a Jenkins job be aborted with success result?
在声明性管道中,使用多分支作业和 Jenkinsfile,是否可以使用 success
退出代码而不是 failure
中止 作业退出代码?
在下面的阶段,我主要检查启动作业的提交是否包含 "ci skip",如果包含我想中止作业。
使用 error
中止作业但也将其标记为中止(红行)。我希望这项工作用绿色行标记。
stage ("Checkout SCM") {
steps {
script {
checkout scm
result = sh (script: "git log -1 | grep '.*\[ci skip\].*'", returnStatus: true)
if (result == 0) {
error ("'ci skip' spotted in git commit. Aborting.")
}
}
}
}
编辑:
我现在尝试简单地跳过所有阶段,而不是上面的步骤,以防 git 提交包含 "ci skip"。我的理解是,如果expression
中的结果是false
,就应该跳过这个阶段...
pipeline {
environment {
shouldBuild = "true"
}
...
...
stage ("Checkout SCM") {
steps {
script {
checkout scm
result = sh (script: "git log -1 | grep '.*\[ci skip\].*'", returnStatus: true)
if (result == 0) {
echo ("'ci skip' spotted in git commit. Aborting.")
shouldBuild = "false"
}
}
}
}
stage ("Unit tests") {
when {
expression {
return shouldBuild
}
}
steps {
...
}
}
...
...
}
好的,所以让这个工作的方法不是使用 environment
指令,而是使用 parameters
指令。
也就是在pipeline
的最前面加上parameters
:
parameters {
booleanParam(defaultValue: true, description: 'Execute pipeline?', name: 'shouldBuild')
}
检查 git 提交时,如果它包含 "ci skip" 我更改 shouldBuild
的值:
env.shouldBuild = "false"
然后在 expression
中:
expression {
return env.shouldBuild != "false"
}
就是这样。如果 git 提交包含 "ci skip",则跳过阶段并且作业以 SUCCESS
.
结束
如果您启动错误,则无法将状态更改为 SUCCESS
,因为 a build result can only get worse.
无论如何,我通过使用 shared library 将构建结果标记为 NOT_BUILT
来实现 ci-skip 功能。我认为这是一个更好的解决方案,因为您不需要在每个阶段添加 when
表达式。
此外,管道的代码看起来更清晰:
pipeline {
stages {
stage('prepare') { steps { ciSkip action: 'check' } }
// other stages here ...
}
post { always { ciSkip action: 'postProcess' } }
}
库的代码见this answer。
您还可以使用 when
指令在一个阶段进行 skip-ci
检查。例如:
stages {
stage("Check Preconditions") {
when {
expression {
result = sh (script: "git log -1 | grep '.*\[ci skip\].*'", returnStatus: true) # Check if commit message contains skip ci label
result == 0 # Evaluate the result
}
}
steps {
script {
echo 'Got skip=ci, aborting build' # Just an info message
currentBuild.result = 'ABORTED' # Mark the current build as aborted
error('Skip-CI') # Here you actually stop the build
}
}
}
stage("Test") {
...
}
想法是,如果满足 skip-ci
条件,将执行 when
块下的代码,构建将中止。
这里有几点说明:
- pipeline 仍然会被标记为红色,但是在 Build History 中你
将看到灰色(中止)构建
- 如果您使用的是 Groovy 沙箱,您需要通过 In-process script approval 批准签名
staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods getAt java.lang.Object java.lang.String
才能获得 currentBuild.result = 'ABORTED'
工作
P.S 如果您需要将构建标记为已中止,此方法很有效。基本上,Jenkins 会在 [ci skip]
条件下中止构建,所以这个状态看起来更合适。我想,这也适用于 currentBuild.result = 'SUCCESS'
。否则,您可以尝试使用 catchError
指令
执行此操作的声明方法如下:
stage ("Unit tests") {
when {
not {
changelog '.*\[ci skip\].*'
}
}
steps {
...
}
}
在声明性管道中,使用多分支作业和 Jenkinsfile,是否可以使用 success
退出代码而不是 failure
中止 作业退出代码?
在下面的阶段,我主要检查启动作业的提交是否包含 "ci skip",如果包含我想中止作业。
使用 error
中止作业但也将其标记为中止(红行)。我希望这项工作用绿色行标记。
stage ("Checkout SCM") {
steps {
script {
checkout scm
result = sh (script: "git log -1 | grep '.*\[ci skip\].*'", returnStatus: true)
if (result == 0) {
error ("'ci skip' spotted in git commit. Aborting.")
}
}
}
}
编辑:
我现在尝试简单地跳过所有阶段,而不是上面的步骤,以防 git 提交包含 "ci skip"。我的理解是,如果expression
中的结果是false
,就应该跳过这个阶段...
pipeline {
environment {
shouldBuild = "true"
}
...
...
stage ("Checkout SCM") {
steps {
script {
checkout scm
result = sh (script: "git log -1 | grep '.*\[ci skip\].*'", returnStatus: true)
if (result == 0) {
echo ("'ci skip' spotted in git commit. Aborting.")
shouldBuild = "false"
}
}
}
}
stage ("Unit tests") {
when {
expression {
return shouldBuild
}
}
steps {
...
}
}
...
...
}
好的,所以让这个工作的方法不是使用 environment
指令,而是使用 parameters
指令。
也就是在pipeline
的最前面加上parameters
:
parameters {
booleanParam(defaultValue: true, description: 'Execute pipeline?', name: 'shouldBuild')
}
检查 git 提交时,如果它包含 "ci skip" 我更改 shouldBuild
的值:
env.shouldBuild = "false"
然后在 expression
中:
expression {
return env.shouldBuild != "false"
}
就是这样。如果 git 提交包含 "ci skip",则跳过阶段并且作业以 SUCCESS
.
如果您启动错误,则无法将状态更改为 SUCCESS
,因为 a build result can only get worse.
无论如何,我通过使用 shared library 将构建结果标记为 NOT_BUILT
来实现 ci-skip 功能。我认为这是一个更好的解决方案,因为您不需要在每个阶段添加 when
表达式。
此外,管道的代码看起来更清晰:
pipeline {
stages {
stage('prepare') { steps { ciSkip action: 'check' } }
// other stages here ...
}
post { always { ciSkip action: 'postProcess' } }
}
库的代码见this answer。
您还可以使用 when
指令在一个阶段进行 skip-ci
检查。例如:
stages {
stage("Check Preconditions") {
when {
expression {
result = sh (script: "git log -1 | grep '.*\[ci skip\].*'", returnStatus: true) # Check if commit message contains skip ci label
result == 0 # Evaluate the result
}
}
steps {
script {
echo 'Got skip=ci, aborting build' # Just an info message
currentBuild.result = 'ABORTED' # Mark the current build as aborted
error('Skip-CI') # Here you actually stop the build
}
}
}
stage("Test") {
...
}
想法是,如果满足 skip-ci
条件,将执行 when
块下的代码,构建将中止。
这里有几点说明:
- pipeline 仍然会被标记为红色,但是在 Build History 中你 将看到灰色(中止)构建
- 如果您使用的是 Groovy 沙箱,您需要通过 In-process script approval 批准签名
staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods getAt java.lang.Object java.lang.String
才能获得currentBuild.result = 'ABORTED'
工作
P.S 如果您需要将构建标记为已中止,此方法很有效。基本上,Jenkins 会在 [ci skip]
条件下中止构建,所以这个状态看起来更合适。我想,这也适用于 currentBuild.result = 'SUCCESS'
。否则,您可以尝试使用 catchError
指令
执行此操作的声明方法如下:
stage ("Unit tests") {
when {
not {
changelog '.*\[ci skip\].*'
}
}
steps {
...
}
}