0
pipeline {
agent { label 'master' }
stages {
    stage('test') {
        steps {
            script {
                def job_exec_details = build job: 'build_job'
                
                if (job_exec_details.status == 'Failed') {
                   echo "JOB FAILED"
                }
            }    
        }
    }

} }

I have a pipeline that executing build job, how can I get Job result in jenkins pipeline ?

Dashrath Mundkar
  • 7,956
  • 2
  • 28
  • 42
Roman Zinger
  • 102
  • 2
  • 9
  • See [This Question](https://stackoverflow.com/questions/51103359/jenkins-pipeline-return-value-of-build-step) for further information – Noam Helmer Jun 26 '21 at 18:25
  • @NoamHelmer that question and this question too different question. – Dashrath Mundkar Jun 26 '21 at 18:26
  • @DashrathMundkar Why? it is a detailed explanation of how to extract all the needed information from the object returned by the build step, which is exactly what he needs. – Noam Helmer Jun 26 '21 at 18:32

2 Answers2

2

It should be getResult() and status should be FAILURE not Failed.

so your whole code should be like this

  pipeline {
    agent { label 'master' }
    stages {
        stage('test') {
            steps {
                script {
                    def job_exec_details = build job: 'build_job', propagate: false, wait: true // Here wait: true means current running job will wait for build_job to finish.
                    
                    if (job_exec_details.getResult() == 'FAILURE') {
                       echo "JOB FAILED"
                    }
                }    
            }
        }
    }
}
Dashrath Mundkar
  • 7,956
  • 2
  • 28
  • 42
0

Where is a second way of getting results:

pipeline {
  agent { label 'master' }
    stages {
      stage('test') {
        steps {
          build(job: 'build_job', propagate: true, wait: true)
        }
      }
    }
    post { 
      success { 
        echo 'Job result is success'
      }
      failure { 
        echo 'Job result is failure'
      }
    }
  }
}

You can read more about 'build' step here

Dmitriy Tarasevich
  • 1,082
  • 5
  • 6