LoginSignup
0
1

More than 5 years have passed since last update.

Jenkinsのジョブをpythonから起動して成功失敗を監視する

Last updated at Posted at 2018-11-21

memo

ここにいい記事があった

これをpythonで書き直す

事前に pip install python-jenkinsしておく。環境はpython3.5。Jenkins 2.60.3。


import jenkins
from time import sleep
import sys

JENKINS_HOST = "http://xxx.xxx.xxx.xxx"
JENKINS_USER = "xxxxxxxxx"
JENKINS_PASS = "xxxxxxxxx"
JENKINS_JOB = "ext_kicktest"
PARAM = {"partitionday":"2018-11-21"}
WAIT_SECONDS = 3

server = jenkins.Jenkins(JENKINS_HOST, username=JENKINS_USER, password = JENKINS_PASS)

print("Start to build {0} with {1}".format(JENKINS_JOB, PARAM))

user = server.get_whoami()
version = server.get_version()
print('Hello %s from Jenkins %s' % (user['fullName'], version))

last_build_number = server.get_job_info(JENKINS_JOB)['lastBuild']['number']
print("Last build_id is {}".format(last_build_number))

# trigger job
server.build_job(JENKINS_JOB, PARAM)

#wait for new build to Start
build_id = server.get_job_info(JENKINS_JOB)['lastBuild']['number']

while (build_id == last_build_number):
    print ("New build is not running yet, wait for {} seconds".format(WAIT_SECONDS))
    sleep(WAIT_SECONDS)
    build_id = server.get_job_info(JENKINS_JOB)['lastBuild']['number']

result = server.get_build_info(JENKINS_JOB, build_id)["result"]
WAIT_SECONDS = 30

while (result is None):
    print("Build is not finished yet, wait for {} seconds".format(WAIT_SECONDS))
    sleep(WAIT_SECONDS)
    result = server.get_build_info(JENKINS_JOB, build_id)["result"]

if (result != "SUCCESS"):
    print( "Not succeeded result is {}".format(result))
    sys.exit(1)
else :
    print("Succeeded")
Start to build ext_kicktest with {'partitionday': '2018-11-21'}
Hello xxxxxx from Jenkins 2.60.3
Last build_id is 4
New build is not running yet, wait for 3 seconds
New build is not running yet, wait for 3 seconds
Build is not finished yet, wait for 30 seconds
Build is not finished yet, wait for 30 seconds
Succeeded
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1