6

How do we get the status of the preceding stage(s) in a Jenkins pipeline. For example: My example pipeline is as under:

pipeline {
    agent
    {
        node {
                label 'master'
                customWorkspace "${env.JobPath}"
              }
    }

    stages 
    {
        stage('Start') {
            steps {
                sh 'ls'
            }
        }

        stage ('set_env') {
            steps {
                // script to set environment.
            }
        }

        stage ('Build') {
            steps {
                // script to build.
            }
        }

        stage ('Send_mail') {
            steps {
                // script to send mail.
            }
        }

        stage('End') {
            steps {
                sh 'ls'
            }
        }
    }
}

How can i get the status of a particular stage in a pipeline. for ex: i want to take certain decisions based on whether the "Build" stage was a success or a failure.

Is there any environment variable which tracks the status of every stage, or there's a Jenkins REST API which can help me achieve this.

Thanks!

Yash
  • 2,944
  • 7
  • 25
  • 43
  • 1
    Via `currentBuild.result` you can set the status for the whole job. But if you only want to take decisions within your pipeline: how about using a normal groovy variable as a flag? If you set a variable within a global context you can use it across stages within the same build. Or maybe I'm not really getting the question right? – fishi0x01 Sep 08 '17 at 09:55
  • Thanks fishi for your suggestion. Kindly request you to elaborate on your logic with a sample code also. Thanks – Yash Sep 08 '17 at 10:13
  • I am looking for the same thing as I want to do some kind of report at the end of the build and I need to access all stages and to get the result for each of them. So far I was not able to find any code that does this. – sorin Sep 16 '17 at 18:58
  • did you find a solution? i am looking for a similar need https://stackoverflow.com/questions/74785704/jenkins-run-stage-repeatedly-and-parallely-until-another-stage-has-finished – anandhu Dec 13 '22 at 13:56

1 Answers1

1

Jenkins declarative pipelines are opinionated syntax on top of the former jenkins pipeline approach. This means you can also regard a jenkins declarative pipeline as a groovy script with extra syntactic sugar. So you can freely use variables. As mentioned in my comment above, you can set the whole jobs status via currentBuild.result, but if you want to make decisions within the same job between stages: simply use groovy variables to indicate the state of the previous stage. Taking your example:

pipeline {

    def didSucceed = true

    agent
    {
        node {
                label 'master'
                customWorkspace "${env.JobPath}"
              }
    }

    stages 
    {
        stage('Start') {
            steps {
                sh 'ls'
            }
        }

        stage ('set_env') {
            steps {
                // script to set environment.
                didSucceed = true // Here you indicate whether stuff actually worked..
            }
        }

        stage ('Build') {
            steps {
                if (didSucceed) {
                    // script to build.
                } else {
                    // Procedure in case of previous error..
                }
            }
        }

        stage ('Send_mail') {
            steps {
                // script to send mail.
            }
        }

        stage('End') {
            steps {
                sh 'ls'
            }
        }
    }
}

You could also stay in a more declarative way using the when statement in combination with a boolean variable.

fishi0x01
  • 3,579
  • 21
  • 25
  • 1
    Pretty sure this isn't quite right. You need to at the very least put your evaluations of the variable in a `script{}` block. I'm also pretty sure that you need to declare your variable *outside* the pipeline. – Mani Nov 24 '20 at 13:35