访问先前的Jenkins版本中哪个阶段失败 [英] Access which stage failed in previous Jenkins build

查看:120
本文介绍了访问先前的Jenkins版本中哪个阶段失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个Jenkinsfile脚本,该脚本获取在当前Github提交中是更新文档还是更新代码,并相应地开始所有阶段.如果只更新文档,则不会再次运行代码测试阶段.

I have written a Jenkinsfile script which gets whether documents are updated or code is updated in the current Github commit and starts all the stages accordingly. If only documents are updated I don't run the code testing stage again.

因此,现在如果以前的构建失败,并且现在在当前的Git中,仅文档被更新,那么它将不会运行代码测试阶段.因此,我希望有一种方法/方法来知道在上一次Jenkins构建期间哪个阶段失败了,如果需要,请运行当前的Jenkins构建.

So now if the previous build failed and now in the current Git commit only documents are updated then it will not run the code testing stage. So I want a method/way to know which stage failed during the last Jenkins build and if needed run the current Jenkins build.

例如,如果在先前的构建中代码测试阶段失败,那么我将需要为此构建运行代码测试阶段,否则,我可以仅运行文档压缩阶段.

For example if the code testing stage failed in the previous build, I'll need to run the code testing stage for this build, otherwise I can just run the documents zipping stage.

推荐答案

作为从詹金斯构建中获取失败阶段的一种解决方法,可以使用这种功能.我找不到更简单的方法来做到这一点.但是此代码需要在没有Groovy沙箱的情况下运行,或者您需要将许多Jenkins方法签名列入白名单(不推荐使用).还必须安装blueocean插件.

As a workaround to get failed stages from Jenkins build such function can be used. I could not find a simpler way to do it. But this code requires to run without Groovy sandbox or you need to whitelist a lot of Jenkins method signatures (which is not recommeded). Also blueocean plugin has to be installed.

import io.jenkins.blueocean.rest.impl.pipeline.PipelineNodeGraphVisitor                                                                                                                                            
import io.jenkins.blueocean.rest.impl.pipeline.FlowNodeWrapper                                                                                                                                                     
import org.jenkinsci.plugins.workflow.flow.FlowExecution                                                                                                                                                           
import org.jenkinsci.plugins.workflow.graph.FlowNode                                                                                                                                                               
import org.jenkinsci.plugins.workflow.job.WorkflowRun                                                                                                                                                              

@NonCPS                                                                                                                                                                                                            
List getFailedStages(WorkflowRun run) {                                                                                                                                                                            
    List failedStages = []                                                                                                                                                                                         
    FlowExecution exec = run.getExecution()                                                                                                                                                                        
    PipelineNodeGraphVisitor visitor = new PipelineNodeGraphVisitor(run)                                                                                                                                           
    def flowNodes = visitor.getPipelineNodes()                                                                                                                                                                     

    for (node in flowNodes) {                                                                                                                                                                                      
        if (node.getType() != FlowNodeWrapper.NodeType.STAGE ) { continue; }                                                                                                                                       
        String nodeName = node.getDisplayName()                                                                                                                                                                    
        def nodeResult = node.getStatus().getResult()                                                                                                                                                              
        println String.format('{"displayName": "%s", "result": "%s"}',                                                                                                                                             
                              nodeName, nodeResult)                                                                                                                                                                
        def resultSuccess = io.jenkins.blueocean.rest.model.BlueRun$BlueRunResult.SUCCESS                                                                                                                          
        if (nodeResult != resultSuccess) {                                                                                                                                                                         
            failedStages.add(nodeName)                                                                                                                                                                             
        }                                                                                                                                                                                                          
    }                                                                                                                                                                                                              
    return failedStages                                                                                                                                                                                            
}                                                                                                                                                                                                                  

// Ex. Get last build of "test_job"                                                                                                                                                                                
WorkflowRun run = Jenkins.instance.getItemByFullName("test_job")._getRuns()[0]                                                                                                                                     
failedStages = getFailedStages(run) 

这篇关于访问先前的Jenkins版本中哪个阶段失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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