访问先前的Jenkins版本中哪个阶段失败

时间:2018-07-24 05:01:00

标签: jenkins jenkins-pipeline

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

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

enter image description here

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

2 个答案:

答案 0 :(得分:0)

我认为这可能适合。如果需要更改设置阶段状态的buildVariables \ timeout,请使用先前版本的input \ try中的catch。代码示例:

// yourJob
// with try/catch block

def stageOneStatus;
def stageTwoStatus;
def stageThreeStatus;

pipeline {
    agent any
    stages {
        stage("STAGE 1") {
            // For initial run every stage
            when { expression { params.stageOne == "FAILURE" } }
            steps {
                script {
                    try {
                        // make thing
                    } catch (Exception e) {
                        stageOneStatus = "FAILURE";
                    }
                }
            }
        }

        stage("STAGE 2") {
            when { expression { params.stageTwo == "FAILURE" } }
            steps {
                script {
                    try {
                        // make thing
                    } catch (Exception e) {
                        stageTwoStatus = "FAILURE";
                    }
                }
            }
        }

        stage("STAGE 3") {
            when { expression { params.stageThree == "FAILURE" } }
            steps {
                script {
                    try {
                        // make thing
                    } catch (Exception e) {
                        stageThreeStatus = "FAILURE";
                    }
                }
            }
        }
    }
}

// Checking JOB

def pJob;

pipeline {
    agent any
    stages {
        // Run job with inheriting variable from build
        stage("Inheriting job") {
            steps {
                script {
                    pJob = build(job: "yourJob", parameters: [
                            [$class: 'StringParameterValue', name: 'stageOne', value: 'FAILURE'],
                            [$class: 'StringParameterValue', name: 'stageTwo', value: 'FAILURE'],
                            [$class: 'StringParameterValue', name: 'stageThree', value: 'FAILURE']
                            ], propagate: false)
                    if (pJob.result == 'FAILURE') {
                    error("${pJob.projectName} FAILED")
                    }
                }
            }
        }
        // Wait for fix, and re run job 
        stage ('Wait for fix') {
            timeout(time: 24, unit: 'HOURS') {
            input "Ready to rerun?"
            }
        }
        // Re run job after changes in code
        stage("Re-run Job") {
            steps {
                script {
                    build(
                        job: "yourJob",
                        parameters: [
                            [$class: 'StringParameterValue',name: 'stageOne',value: pJob.buildVariables.stageOneStatus ],
                            [$class: 'StringParameterValue',name: 'stageTwo',value: pJob.buildVariables.stageTwoStatus ],
                            [$class: 'StringParameterValue',name: 'stageThree',value: pJob.buildVariables.stageThreeStatus ]

                        ]
                    )
                }
            }
        }
    }
}

答案 1 :(得分:0)

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

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)