Jenkins 管道执行作业并获取状态

Jenkins pipeline execute job and get status

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"
                }
            }    
        }
    }

} }

我有一个执行构建作业的管道,如何在 jenkins 管道中获取作业结果?

应该是 getResult(),状态应该是 FAILURE 而不是 Failed

所以你的整个代码应该是这样的

  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"
                    }
                }    
            }
        }
    }
}

获取结果的第二种方式在哪里:

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'
      }
    }
  }
}

您可以详细了解 'build' 步骤 here