code-maven.com icon indicating copy to clipboard operation
code-maven.com copied to clipboard

Report failed stage name not working with parallel stages

Open elavaud opened this issue 6 years ago • 1 comments

Hi there,

Reporting failed stage in the post section is quite a handy trick. Thanks for that! However I struggle to make it work in a more complex scenario using parallel stages. Let's consider this:

pipeline {
    agent none // Using 2 different slaves
    stages {
        stage('For parallel stuff') {
            failFast true
            parallel {
                stage('Slave A with sequential stages') {
                    agent  { node { label "os-linux" } }
                    stages {
                        stage ('Stuff 1 on slave A') {
                            steps {
                                script {
                                    last_started = env.STAGE_NAME
                                    // stuff
                                }
                            }
                        }
                        stage ('Stuff 2 on slave A') { ... }
                        stage ('Stuff 3 on slave A') { ... }
                    }
                }
                stage('Slave B with sequential stages') {
                    agent  { node { label "os-windows" } }
                    stages {
                        stage ('Stuff 1 on slave B') {
                            steps {
                                script {
                                    last_started = env.STAGE_NAME
                                    // stuff
                                }
                            }
                        }
                        stage ('Stuff 2 on slave B') { ... }
                        stage ('Stuff 3 on slave B') { ... }
                    }
                }
            }
        }
    }
    post { // Using STAGE_NAME variable }
}

If, for example, "Stuff 2 on slave B" started after "Stuff 1 on slave A", although that the later one fails, the first one is reported.

Then people throw rocks at you beause you reported the wrong failed stage ... :(

Some ideas on how to handle that? How come Jenkins pipeline UI or Blue Ocean manage to get the right one that failed?

Cheers,

Edouard

elavaud avatar Apr 16 '20 07:04 elavaud

Clearly not as handy / beautiful, but I solved my issue assigning the variable "last_started" in a post block for the stage:

pipeline {
    agent none // Using 2 different slaves
    stages {
        stage('For parallel stuff') {
            failFast true
            parallel {
                stage('Slave A with sequential stages') {
                    agent  { node { label "os-linux" } }
                    stages {
                        stage ('Stuff 1 on slave A') {
                            steps {
                                // stuff
                            }
                            post {
                                failure {
                                    last_started = env.STAGE_NAME
                                }
                                unstable { ... } // Adding this one on stages that could be unstable
                            }
                        }
                        ...
                    }
                }
                ....
            }
        }
    }
    post { // Using STAGE_NAME variable }
}

elavaud avatar Apr 23 '20 07:04 elavaud