How do I know which stage of jenkins pipeline has failed How do I know which stage of jenkins pipeline has failed jenkins jenkins

How do I know which stage of jenkins pipeline has failed


There is a variable called env.STAGE_NAME which you can use. However, in your case you will probably need to store the stage name in a different variable, because when you get the env.STAGE_NAME in a post stage the result will be Declarative: Post Actions. Instead, you will need to store the stage name in a variable in all stages. So once a stage fails - Jenkins will not continue with the next stages and therefore you will have the "failed" stage name.

Here's an example:

def FAILED_STAGEpipeline {    agent { label "master" }    stages {        stage("Stage 1") {            steps {                script {                    FAILED_STAGE=env.STAGE_NAME                    echo "stage 1"                }            }        }        stage("Stage 2") {            steps {                script {                    FAILED_STAGE=env.STAGE_NAME                    echo "stage 2"                    error "failed for some reason."                }            }        }        stage("Stage 3") {            steps {                script {                    FAILED_STAGE=env.STAGE_NAME                    echo "stage 3"                }            }        }    }    post {        failure {            echo "Failed stage name: ${FAILED_STAGE}"        }    }}

There might be a better way to do it, but I haven't found it so far.

Regarding the logs - As of JENKINS-40526 you could possibly use the API and get the log file from there, but I am not sure you can get the parameters you need from within the pipeline. The other solution would be to use emailext and email the entire build log file:

emailext attachLog: true, body: '', compressLog: true, subject: 'Build failed.', to: 'somebody@somewhere.com'