继续詹金斯管道过去失败的阶段

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,但我想在舞台视图中显示它们.在下面的模拟中:

  • Build#4显示了正常情况.作业two失败,所以three不运行.
  • 我在Photoshop#6中展示了我想要看到的内容.作业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将显示为失败:

管道示例

您可能已经猜到了,如果您希望它不稳定或其他任何情况,您可以自由选择buildResultstageResult。您甚至可以使构建失败并继续执行管道。

只要确保您的Jenkins是最新的,因为这是一个相当新的功能。

编辑:您需要“管道:基本步骤” 2.16(2019年5月14日)

  • 我想我们可以以给出答案的日期为基础,但是为了清楚起见,您介意添加引入此问题的日期以及版本吗? (2认同)

小智 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)

  • 尽管此代码段可能是解决方案,但是[包括说明](https://meta.stackexchange.com/questions/114762/explaining-entirely-%E2%80%8C%E2%80%8Bcode-based-answers)确实有助于提高您的帖子质量。请记住,您将来会为读者回答这个问题,而这些人可能不知道您提出代码建议的原因。 (3认同)

Dir*_*irk 8

这取决于您使用的是声明式管道语法还是脚本式管道语法

声明式管道语法

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)


avi*_*amg 6

解决方案:为了始终继续詹金斯管道中失败的步骤:

选项 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)


Pav*_*nik 5

这应该工作。但是,即使只有一个失败,所有框都是红色的,但是您会看到带有错误标记的框,因此您可以轻松地区分失败的作业。

def indexes = ['one', 'two', 'three']

node() {
    for (index in indexes) {
        catchError {
            stage(index) {
                println index
                sh '''echo "123"'''
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)