Check Jenkins job status after triggering a build remotely

前端 未结 4 834
夕颜
夕颜 2020-12-10 02:39

I have a script to trigger a job on Jenkins remotely using a token. Here is my script:

JENKINS_URL=\'http://jenkins.myserver.com/jenkins\'
JOB_NAME=\'job/uti         


        
4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-10 02:57

    You can use Jenkins API for this. A sample Python script:

    import json
    import requests
    import time
    
    
    
    job_name = "testjob" .  #Give your job name here
    
    
    def jenkins_job_status(job_name):
    
            try:
                    url  = "https://your_jenkins_endpoint/job/%s/lastBuild/api/json" %job_name   #Replace 'your_jenkins_endpoint' with your Jenkins URL
                    while True:
                            data = requests.get(url).json()
                            if data['building']:
                                    time.sleep(60)
                            else:
                                    if data['result'] == "SUCCESS":
    
                                            print "Job is success"
                                            return True
                                    else:
                                            print "Job status failed"
                                            return False
    
    
            except Exception as e:
                    print str(e)
                    return False
    
    
    
    
    if __name__ == "__main__":
    
            if jenkins_job_status(job_name):
    
                    print "Put your autmation here for 'job is success' condition"
    
            else:
                    print "Put your autmation here for 'job is failed' condition" 
    

    Refer http://www.easyaslinux.com/tutorials/devops/how-to-check-build-status-of-jenkins-job-using-python-script/ for detailed explanation

提交回复
热议问题