Scripting

如何從命令行檢查 Jenkins 建構的建構狀態?

  • March 2, 2022

如何在不切換到瀏覽器的情況下檢查 Jenkins 建構狀態?

如果需要,我可以使用 JSON API 創建一個腳本,但我想知道是否已經內置了類似的東西。

我找不到內置工具,所以我做了一個:

#!/usr/bin/python
#
# author: ajs
# license: bsd
# copyright: re2


import json 
import sys
import urllib
import urllib2

jenkinsUrl = "https://jenkins.example.com/job/"


if len( sys.argv ) > 1 :
   jobName = sys.argv[1]
   jobNameURL = urllib.quote(jobName)
else :
   sys.exit(1)

try:
   jenkinsStream   = urllib2.urlopen( jenkinsUrl + jobNameURL + "/lastBuild/api/json" )
except urllib2.HTTPError, e:
   print "URL Error: " + str(e.code) 
   print "      (job name [" + jobName + "] probably wrong)"
   sys.exit(2)

try:
   buildStatusJson = json.load( jenkinsStream )
except:
   print "Failed to parse json"
   sys.exit(3)

if buildStatusJson.has_key( "result" ):      
   print "[" + jobName + "] build status: " + buildStatusJson["result"]
   if buildStatusJson["result"] != "SUCCESS" :
       exit(4)
else:
   sys.exit(5)

sys.exit(0)

引用自:https://serverfault.com/questions/309848