詹金斯管道 - 尝试捕获特定阶段和后续条件步骤 [英] Jenkins pipeline - try catch for particular stage and subsequent conditional step

查看:22
本文介绍了詹金斯管道 - 尝试捕获特定阶段和后续条件步骤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Jenkins 管道中使用 try/catch 围绕前一个阶段复制等效的条件阶段,然后设置一个成功变量,用于触发条件阶段.

I'm trying to replicate the equivalent of a conditional stage in Jenkins pipeline using a try / catch around a preceding stage, which then sets a success variable, which is used to trigger the conditional stage.

看来,try catch 块是要走的路,将成功变量设置为 SUCCESS 或 FAILED,稍后用作 when 语句的一部分(作为条件阶段的一部分).

It appears that a try catch block is the way to go, setting a success var to SUCCESS or FAILED, which is used as part of a when statement later (as part of the conditional stage).

我使用的代码如下:

pipeline {
    agent any
    stages {
        try{
            stage("Run unit tests"){
                steps{
                    sh  '''
                        # Run unit tests without capturing stdout or logs, generates cobetura reports
                        cd ./python
                        nosetests3 --with-xcoverage --nocapture --with-xunit --nologcapture --cover-package=application
                        cd ..
                    '''
                    currentBuild.result = 'SUCCESS'
                }
            }
        } catch(Exception e) {
            // Do something with the exception 
            currentBuild.result = 'SUCCESS'
        }

        stage ('Speak') {
            when {
                expression { currentBuild.result == 'SUCCESS' }
            }
            steps{
                echo "Hello, CONDITIONAL"
            }
        }
    }
}

我收到的最新语法错误如下:

The latest syntax error I am receiving is as follows:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup 
failed:
WorkflowScript: 4: Expected a stage @ line 4, column 9.
       try{

我也尝试了很多变化.

我在这里采取了错误的方法吗?这似乎是一个相当普遍的要求.

Am I taking the wrong approach here? This seems like a fairly common requirement.

谢谢.

推荐答案

这可能会根据您的目标解决您的问题.只有在前面的阶段成功时才会运行阶段,因此如果您实际上有两个阶段(如示例中所示),并且如果您希望第二个阶段仅在第一个阶段成功时运行,则需要确保在测试失败时第一个阶段适当地失败.捕捉将防止(理想的)失败.finally 会保存失败,并且仍然可以用来抓取你的测试结果.

This might solve your problem depending on what you are going for. Stages are only run when the preceding stages succeed, so if you actually have two stages like in your example, and if you want the second to only run when the first succeeds, you want to ensure that the first stage fails appropriately when tests fail. Catching will prevent the (desirable) failure. Finally will preserve the failure, and can also still be used to grab your test results.

所以这里,只有在测试通过时才会运行第二阶段,并且无论如何都会记录测试结果:

So here, the second stage will only run when the tests pass, and the test results will be recorded regardless:

pipeline {
  agent any
  stages {
    stage("Run unit tests"){
      steps {
        script {
          try {
            sh  '''
              # Run unit tests without capturing stdout or logs, generates cobetura reports
              cd ./python
              nosetests3 --with-xcoverage --nocapture --with-xunit --nologcapture --cover-package=application
              cd ..
              '''
          } finally {
            junit 'nosetests.xml'
          }
        }
      }
    }
    stage ('Speak') {
      steps{
        echo "Hello, CONDITIONAL"
      }
    }
  }
}

请注意,我实际上是在声明式管道中使用 try,但 就像 StephenKing 说的,你不能直接使用 try(你必须在脚本步骤中包装任意的 groovy 代码).

Note that I'm actually using try in a declarative pipeline, but like StephenKing says, you can't just use try directly (you have to wrap arbitrary groovy code in the script step).

这篇关于詹金斯管道 - 尝试捕获特定阶段和后续条件步骤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆