Python sample code of hudson remote api, my first Python hello world

refer http://wiki.hudson-ci.org/display/HUDSON/Remote+access+API
 for more about hudson remote api

#!/usr/bin/env python

# this script is only demo purpose which is designed to get properties of job, queue, like
# nextBuildNumber. But note the logistics might not be correct

import urllib2

#call api of job 'git_plugin_test'
url="http://localhost:9001/job/git_plugin_test/api/python?depth=0"
response=urllib2.urlopen(url)
build_dict=eval(response.read())

#call api of job 'queue' of hudson (global but not specific for one job)
url="http://localhost:9001/queue/api/python?depth=0"
response=urllib2.urlopen(url)
queue_dict=eval(response.read())

print ''*40,'build dict',''*40
#print properties of job
for eachKey in build_dict:
    print eachKey,build_dict[eachKey]
    print

print ''*40,'queue dict',''*40
#look through items in queue and can be extended to forecast the job build
#number in for one item in queue
for index in range(1,len(queue_dict['items'])):
    print ''*40,'queue hash',''*40
    qi_action=queue_dict['items'][index]['actions']
    list_para=qi_action[0]['parameters']
    for index1 in range(0,len(list_para)):
        print list_para[index1]
        if list_para[index1]['name'] == 'SLEEP_TIME' and list_para[index1]['value'] == '62':
            print "OK"

#only valid when no more than one build found in queue
if build_dict['inQueue']:
    build_number=int (build_dict['nextBuildNumber']) + 1
else :
    build_number=int (build_dict['nextBuildNumber'])
print "Hudson Build URL:" ,build_dict['url']+str(build_number)
print "Current build tree:" +build_dict['builds'][0]['url']

Jenkins和Python的结合可以实现自动化的构建、测试和部署等任务。Python可以通过Jenkins的API接口来与Jenkins进行交互,Jenkins也提供了Python插件,可以通过Python脚本进行构建和部署操作。 具体来说,可以使用Python的jenkinsapi库来访问Jenkins的API接口,该库提供了丰富的方法和属性,可以获取、创建、修改、删除Jenkins的Job、构建、视图等对象。下面是使用Python的jenkinsapi库创建Jenkins的Job的示例代码: ``` from jenkinsapi.jenkins import Jenkins # Jenkins连接配置 jenkins_url = 'http://jenkins.example.com' jenkins_username = 'username' jenkins_password = 'password' # 创建Jenkins连接 jenkins_server = Jenkins(jenkins_url, jenkins_username, jenkins_password) # Job配置信息 job_name = 'my_job' job_config = ''' <project> <actions/> <description>My Jenkins Job</description> <keepDependencies>false</keepDependencies> <properties/> <scm class="hudson.scm.NullSCM"/> <canRoam>true</canRoam> <disabled>false</disabled> <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding> <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding> <triggers> <hudson.triggers.SCMTrigger> <spec>*/15 * * * *</spec> <ignorePostCommitHooks>false</ignorePostCommitHooks> </hudson.triggers.SCMTrigger> </triggers> <concurrentBuild>false</concurrentBuild> <builders> <hudson.tasks.Shell> <command>echo "Hello, Jenkins!"</command> </hudson.tasks.Shell> </builders> <publishers/> <buildWrappers/> </project> ''' # 创建Jenkins的Job jenkins_server.create_job(job_name, job_config) ``` 该示例代码中,首先配置Jenkins的连接信息,然后使用Jenkins类创建Jenkins连接对象jenkins_server。接着,定义Job的名称job_name和配置信息job_config,其中配置信息为Job的XML配置文件。最后,调用jenkins_server的create_job方法创建Jenkins的Job。 除了创建Job,还可以使用jenkinsapi库执行Jenkins的构建、获取构建结果、获取构建日志等操作。同时,Jenkins也提供了Python插件,可以使用Python脚本进行构建和部署操作。例如,可以在Jenkins的构建步骤中添加Execute Python Script步骤,执行Python脚本完成特定的构建和部署操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值