詹金斯将建筑标记为失败

时间:2018-08-22 02:06:45

标签: jenkins groovy jenkins-pipeline

我正在尝试执行一项詹金斯(Jenkins)工作,该工作会触发其他工作,如果其他工作失败,则会失败。

我有以下Jenkinsfile

#!groovy
String hipChatRoom = "${HC_ROOM}" //'Ecom WSS'
def projectPath = "${projectPath}"
def numberOfNodes = "${numberOfNodes}".toInteger()
def parallelStageMap = [:]
def failure = false


def generateStage(int i) {
    return {
        node("Cypress") {
            try {
                build(job: 'Cypress-Kickoff', wait: true, parameters: [[$class: 'StringParameterValue', name: 'BRANCH_NAME', value: BRANCH_NAME], [$class: 'StringParameterValue', name: 'recordKey', value: recordKey], [$class: 'StringParameterValue', name: 'projectPath', value: projectPath], [$class: 'StringParameterValue', name: 'CI_BUILD_ID', value: BUILD_ID], [$class: 'StringParameterValue', name: 'ID', value: i.toString()]])
            } catch (Exception e) {
                println e
                failure = true
            }
        }
    }
}

stage("Cypress Run") {
    node("Cypress") {
        currentBuild.setDisplayName("${projectPath}")
        deleteDir()
        git url: 'http://github.com/REPO/repo.git', branch: BRANCH_NAME
        for (int i = 0; i < numberOfNodes; i++) {
            parallelStageMap["stage" + i] = generateStage(i)    
        }
        parallel(
            parallelStageMap
        )
    }
}

if (failure) { 
    currentBuild.result = "FAILURE"
}

如果我进入并终止其中一台Cypress,构建仍会通过

[stage0] Starting building: Cypress-Kickoff #438
[stage2] Starting building: Cypress-Kickoff #439
[stage1] Starting building: Cypress-Kickoff #440
[stage3] Starting building: Cypress-Kickoff #441
[stage4] Starting building: Cypress-Kickoff #437
[Pipeline] [stage1] echo
[stage1] hudson.AbortException: Cypress-Kickoff #440 completed with status ABORTED (propagate: false to ignore)
[Pipeline] [stage1] }
[Pipeline] [stage1] // node
[Pipeline] [stage1] }
[Pipeline] [stage0] }
[Pipeline] [stage0] // node
[Pipeline] [stage0] }
[Pipeline] [stage4] }
[Pipeline] [stage4] // node
[Pipeline] [stage4] }
[Pipeline] [stage3] }
[Pipeline] [stage3] // node
[Pipeline] [stage3] }
[Pipeline] [stage2] }
[Pipeline] [stage2] // node
[Pipeline] [stage2] }
[Pipeline] // parallel
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] End of Pipeline
Finished: SUCCESS

如果我使用“ catchError”,它可以工作,但是我不能使用它,因为我需要在出现故障时向Slack的Hipchat发布消息。

如果任何并行作业失败,如何使作业失败?

1 个答案:

答案 0 :(得分:0)

我不确定我是否理解得很好,但是在catchError之后使用currentBuild.result怎么办?我经常使用 try / catch / finally 进行管理。

Official Documentation

此外,您可以通过行currentBuild.result='FAILURE'来强制作业失败

node {
    catchError {
        // Do your job
    }
    if(currentBuild.result=='FAILURE')
        // Notify
}