Jenkinsfile 中的失败后块不起作用 [英] Post failure block in Jenkinsfile is not working
问题描述
我正在尝试使用并行步骤执行失败后操作,但它永远不会起作用.
I'm trying a post failure action with a parallel step but it never works.
这是我的 Jenkins 文件:
This is my Jenkinsfile:
pipeline {
agent any
stages {
stage("test") {
steps {
withMaven(
maven: 'maven3', // Maven installation declared in the Jenkins "Global Tool Configuration"
mavenSettingsConfig: 'maven_id', // Maven settings.xml file defined with the Jenkins Config File Provider Plugin
mavenLocalRepo: '.repository')
{
// Run the maven build
sh "mvn --batch-mode release:prepare -Dmaven.deploy.skip=true" --> it will always fail
}
}
}
stage("testing") {
steps {
parallel (
phase1: { sh 'echo phase1' },
phase2: { sh "echo phase2" }
)
}
}
}
post {
failure {
echo "FAIL"
}
}
}
但是这里的失败后操作有点没用......我看不到任何地方.
But the post failure action here is a bit useles... I don´t see it any place.
谢谢大家!问候
推荐答案
经过几个小时的搜索,我找到了问题所在.您缺少的(我也缺少)是 catchError 部分.
I've found the issue, after several hours of searching. What you are missing (and I was missing too) is the catchError section.
pipeline {
agent any
stages {
stage('Compile') {
steps {
catchError {
sh './gradlew compileJava --stacktrace'
}
}
post {
success {
echo 'Compile stage successful'
}
failure {
echo 'Compile stage failed'
}
}
}
/* ... other stages ... */
}
post {
success {
echo 'whole pipeline successful'
}
failure {
echo 'pipeline failed, at least one step failed'
}
}
您应该将每个可能失败的步骤包装到 catchError
函数中.这是做什么的:
You should wrap every step that can potentially fail into a catchError
function. What this does is:
- 如果发生错误...
- ...将
build.result
设置为FAILURE
... - ...然后继续构建
- If an error occurs...
- ... set
build.result
toFAILURE
... - ... and continue the build
最后一点很重要:您的 post{ }
块没有被调用,因为您的整个管道在它们有机会执行之前就中止.
The last point is important: your post{ }
blocks did not get called because your entire pipeline was aborted before they even had a chance to execute.
这篇关于Jenkinsfile 中的失败后块不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!