Ian*_*nce 14 jenkins jenkins-pipeline
在我们的Jenkins Pipeline工作中,我们有几个阶段,我想要的是,如果任何阶段失败,那么让构建停止而不是继续进入更多阶段.
以下是其中一个阶段的示例:
stage('Building') {
def result = sh returnStatus: true, script: './build.sh'
if (result != 0) {
echo '[FAILURE] Failed to build'
currentBuild.result = 'FAILURE'
}
}
Run Code Online (Sandbox Code Playgroud)
脚本将失败,构建结果将更新,但作业将继续进行到下一阶段.如果发生这种情况,我该如何中止或停止工作?
sob*_*obi 15
由于 Jenkins v2 这也应该有效
error('Failed to build')
Run Code Online (Sandbox Code Playgroud)
工作将结束:
ERROR: Failed to build
Finished: ERROR
Run Code Online (Sandbox Code Playgroud)
Rik*_*Rik 14
基本上这就是sh步骤的作用.如果您没有在变量中捕获结果,则可以运行:
sh "./build"
Run Code Online (Sandbox Code Playgroud)
如果脚本重新运行非零退出代码,则将退出.
如果您需要先执行操作,并且需要捕获结果,则可以使用shell步骤退出作业
stage('Building') {
def result = sh returnStatus: true, script: './build.sh'
if (result != 0) {
echo '[FAILURE] Failed to build'
currentBuild.result = 'FAILURE'
// do more stuff here
// this will terminate the job if result is non-zero
// You don't even have to set the result to FAILURE by hand
sh "exit ${result}"
}
}
Run Code Online (Sandbox Code Playgroud)
但是下面的内容会给你相同的,但看起来更加明智
stage('Building') {
try {
sh './build.sh'
} finally {
echo '[FAILURE] Failed to build'
}
}
Run Code Online (Sandbox Code Playgroud)
也可以在代码中调用return.但是如果你在里面,stage它只会退出那个阶段.所以
stage('Building') {
def result = sh returnStatus: true, script: './build.sh'
if (result != 0) {
echo '[FAILURE] Failed to build'
currentBuild.result = 'FAILURE'
return
}
echo "This will not be displayed"
}
echo "The build will continue executing from here"
Run Code Online (Sandbox Code Playgroud)
不会退出这份工作,但是
stage('Building') {
def result = sh returnStatus: true, script: './build.sh'
}
if (result != 0) {
echo '[FAILURE] Failed to build'
currentBuild.result = 'FAILURE'
return
}
Run Code Online (Sandbox Code Playgroud)
将.
实现此行为的另一种方法是抛出异常.事实上,这正是詹金斯本身所做的.这样,您还可以将构建状态设置为ABORTED或FAILURE.此示例中止构建:
stage('Building') {
currentBuild.rawBuild.result = Result.ABORTED
throw new hudson.AbortException('Guess what!')
echo 'Further code will not be executed'
}
Run Code Online (Sandbox Code Playgroud)
输出:
[Pipeline] stage
[Pipeline] { (Building)
[Pipeline] }
[Pipeline] // stage
[Pipeline] End of Pipeline
ERROR: Guess what!
Finished: ABORTED
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
35111 次 |
| 最近记录: |