jenkinsjenkins-pipelinejenkins-groovyjenkins-cli

Is there a any way to get current stage (running, successfu,failure,aborted) of jenkins pipeline from another pipeline


I have two pipelines. Pipeline A (Application build) and pipeline B (App check). Pipeline A triggers the pipeline B and both runs simultaniously. In pipeline B before a specific stage (run check) I need to verify if the pipeline A is successful. If not wait and check for some time till pipeline A gets finished. So pipeline B can proceed with the check if "A" is successful or exit with a failure. What I need to know is, is there a any way to check the build status of pipeline A from pipeline B using pipeline "A"s build number. I passes the build number of Pipeline A to Pipeline B.

I looked if there's any env variable for status check but I couln'd find any. I passes the build number of Pipeline A to Pipeline B.


Solution

  • You can create Pipeline B like below. Here you can use waitUntil for waiting.

    pipeline {
        agent any
        stages {
            stage('Build') {
                steps {
                    script {
                            echo "Waiting"
                            def jobName = "JobA"
                            def buildNum = "92"
                            waitUntil { !isPending(jobName, buildNum) }
    
                            if(getStatus(jobName, buildNum).equals('SUCCESS')) {
                                echo "Job A is Successful"
                            } else {
                                echo "Job A Failed"
                            }
                        }
                       
                   }
                }
    
            }
    }
    
    def isPending(def JobName, def buildNumber) {
        def buildA = Jenkins.instance.getItemByFullName(JobName).getBuild(buildNumber)
        return buildA.isInProgress()
    }
    
    def getStatus(def JobName, def buildNumber) {
        def status = Jenkins.instance.getItemByFullName(JobName).getBuild(buildNumber).getResult().toString()
        return status
    }