Alpha
Alpha

Reputation: 14036

How to get failed stage name during parallel run of stages?

I've a pipeline where multiple stages run in parallel but if any of the stages fails, I want to get its name to show failed stage. With following code, even if it fails in first stage ie Checking Redmine access, it always show last stage as failed i.e. Checking Reviewers list. This is because it runs in parallel and latest assigned value is picked up.

pipeline {
    agent {
        label {
            label "<machine-ip>"
            customWorkspace "workspace/RedmineAndReviewboardProject/SVNCheckoutTest"
        }
    }
    stages {
        stage('Verify inputs') {

            parallel {
                stage('Checking Redmine access') {
                    steps {
                        script {
                            FAILED_STAGE = env.STAGE_NAME
                            echo "Redmine"
                            sh'''hello'''
                        }
                    }
                }
                stage('Checking SVN access') {
                    steps {
                        script {
                            FAILED_STAGE = env.STAGE_NAME
                            echo "SVN"
                        }
                    }

                }
                stage('Checking Reviewers list') {
                    steps {
                        script {
                            FAILED_STAGE = env.STAGE_NAME
                            echo "Reviewer"
                        }
                    }
                }

            }

        }
    }
    post {
        failure {
            script {
                echo "Failed stage is " + FAILED_STAGE
            }
        }
    }
}

Is there any way I can get exactly failed stage of parallel running stages? or it will be also ok with me if parent stage name is returned as failed stage.

Upvotes: 3

Views: 1176

Answers (1)

Patrice M.
Patrice M.

Reputation: 4319

I believe you can use a post { failure { block for each stage : see https://www.jenkins.io/doc/book/pipeline/syntax/#post

pipeline {
    agent {
        label {
            label "<machine-ip>"
            customWorkspace "workspace/RedmineAndReviewboardProject/SVNCheckoutTest"
        }
    }
    stages {
        stage('Verify inputs') {

            parallel {
                stage('Checking Redmine access') {
                    steps {
                        script {
                            echo "Redmine"
                            sh'''hello'''
                        }
                    }
                    post {
                        failure {
                            script {
                                echo "Failed stage is ${STAGE_NAME}"
                            }
                        }
                    }
                }
                stage('Checking SVN access') {
                    steps {
                        script {
                            FAILED_STAGE = env.STAGE_NAME
                            echo "SVN"
                        }
                    }
                    post {
                        failure {
                            script {
                                echo "Failed stage is ${STAGE_NAME}"
                            }
                        }
                    }
                }
                stage('Checking Reviewers list') {
                    steps {
                        script {
                            FAILED_STAGE = env.STAGE_NAME
                            echo "Reviewer"
                        }
                    }
                    post {
                        failure {
                            script {
                                echo "Failed stage is ${STAGE_NAME}"
                            }
                        }
                    }
                }
            }
        }
    }
}

Upvotes: 1

Related Questions