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

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

问题描述

我正在尝试通过在前一个阶段周围进行try/catch来复制Jenkins管道中一个条件阶段的等效内容,然后在该阶段之前设置一个成功变量,该变量用于触发条件阶段.

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).

我正在使用的代码如下:

The code I am using is as follows:

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{

我也尝试了很多变化.

I've also tried lots of variations.

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

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

谢谢.

推荐答案

这可能会解决您的问题,具体取决于您要做什么.阶段仅在前面的阶段成功时才运行,因此,如果实际上有两个阶段(如您的示例中所示),并且如果您希望第二阶段仅在第一个阶段成功时才运行,则要确保在测试失败时第一个阶段适当地失败.捕捉将防止(理想的)故障.最终将保留故障,并且仍然可以用来获取测试结果.

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,但是

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天全站免登陆