Joh*_*hee 39 jenkins jenkins-pipeline
我有一系列快速检查的阶段.即使有失败,我也想要完成它们.例如:
stage('one') {
node {
sh 'exit 0'
}
}
stage('two') {
node {
sh 'exit 1' // failure
}
}
stage('three') {
node {
sh 'exit 0'
}
}
Run Code Online (Sandbox Code Playgroud)
阶段two失败,因此默认情况下three不执行阶段.
通常这是一个工作parallel,但我想在舞台视图中显示它们.在下面的模拟中:
two失败,所以three不运行.two失败并显示为,但three仍然运行.真正的Jenkins可能会显示整个Build#6略带红色,这当然很好.Eri*_*k B 29
现在这是可能的。以下是声明性管道的示例,但也catchError适用于脚本化管道。
pipeline {
agent any
stages {
stage('1') {
steps {
sh 'exit 0'
}
}
stage('2') {
steps {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
sh "exit 1"
}
}
}
stage('3') {
steps {
sh 'exit 0'
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,所有阶段都将执行,管道将成功,但是阶段2将显示为失败:
您可能已经猜到了,如果您希望它不稳定或其他任何情况,您可以自由选择buildResult和stageResult。您甚至可以使构建失败并继续执行管道。
只要确保您的Jenkins是最新的,因为这是一个相当新的功能。
编辑:您需要“管道:基本步骤” 2.16(2019年5月14日)
小智 10
我也有同样的担忧。我能够解决此问题。
第二阶段将显示为红色,并标记为失败,而其余阶段将继续运行。您可以设置一个标志,并在阶段结束时检查标志并告知整个构建的状态。
node {
def build_ok = true
stage('one') {
sh 'exit 0'
}
try{
stage('two') {
sh 'exit 1' // failure
}
} catch(e) {
build_ok = false
echo e.toString()
}
stage('three') {
sh 'exit 0'
}
....
if(build_ok) {
currentBuild.result = "SUCCESS"
} else {
currentBuild.result = "FAILURE"
}
}
Run Code Online (Sandbox Code Playgroud)
声明式管道语法:
pipeline {
agent any
stages {
stage('one') {
steps {
sh 'exit 0'
}
}
stage('two') {
steps {
sh 'exit 1' // failure
}
}
}
post {
always {
sh 'exit 0'
}
}
}
Run Code Online (Sandbox Code Playgroud)
脚本化管道语法:
node {
def build_ok = true
stage('one') {
sh 'exit 0'
}
try{
stage('two') {
sh 'exit 1' // failure
}
} catch(e) {
build_ok = false
echo e.toString()
}
stage('three') {
sh 'exit 0'
}
if(build_ok) {
currentBuild.result = "SUCCESS"
} else {
currentBuild.result = "FAILURE"
}
}
Run Code Online (Sandbox Code Playgroud)
解决方案:为了始终继续詹金斯管道中失败的步骤:
选项 1.将函数包装在 try/catch 或 bash 脚本中<someOpertation> || true
试着抓:
script {
try {
sh 'do your stuff'
} catch (Exception e) {
sh 'Handle the exception!'
}
}
Run Code Online (Sandbox Code Playgroud)
bash 始终为真:
script {
sh 'cp ~/someFile.txt ~/dev || true'
}
Run Code Online (Sandbox Code Playgroud)
选项 2.并行运行 jenkins 管道并failFast false在步骤中设置配置属性。
pipeline {
agent any
stages {
stage('Parallel Stage') {
when {
branch 'master'
}
failFast false
parallel {
stage('Branch A') {
agent {
label "for-branch-a"
}
steps {
echo "On Branch A"
}
}
stage('Branch B') {
agent {
label "for-branch-b"
}
steps {
echo "On Branch B"
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这应该工作。但是,即使只有一个失败,所有框都是红色的,但是您会看到带有错误标记的框,因此您可以轻松地区分失败的作业。
def indexes = ['one', 'two', 'three']
node() {
for (index in indexes) {
catchError {
stage(index) {
println index
sh '''echo "123"'''
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
28781 次 |
| 最近记录: |