python的Jenkins接口調(diào)用方式
本來(lái)非常喜歡偷懶
最好就是不干活那種
所以最近在研究把Jenkins模塊集成起來(lái)
做成傻瓜界面這樣就給他們用
本人Python搓望大神不要噴,多多指導(dǎo)
jenkins的Python模塊模塊安裝
pip:pip install python-jenkins
easy_install:easy_install python-jenkins
使用:
class jenkins_tools(): def __init__(self): cf = get_conf() self.username = cf.get(’jenkins’, ’username’) self.password = cf.get(’jenkins’, ’password’) self.php_jenkins = ’’’#本?拋約旱?enkins的conf文件 <project>#這里可以去抄jenkins的項(xiàng)目文件夾里面的配置文件 <actions/>#記得不要加xml頭,源碼哪里幫我們加了,自己加就是作死 <description></description>#項(xiàng)目需求不一樣,配置文件也不一樣,你們不要抄我的 <keepDependencies>false</keepDependencies> <properties> <hudson.model.ParametersDefinitionProperty> <parameterDefinitions><hudson.model.StringParameterDefinition> <name>Branch</name> <description></description> <defaultValue>%s</defaultValue></hudson.model.StringParameterDefinition> </parameterDefinitions> </hudson.model.ParametersDefinitionProperty> </properties> <scm /> <canRoam>true</canRoam> <disabled>false</disabled> <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding> <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding> <triggers/> <concurrentBuild>false</concurrentBuild> <builders> <hudson.tasks.Shell> <command>xxxxxxx</command> </hudson.tasks.Shell> </builders> <publishers/> <buildWrappers/> </project> ’’’ self.java_newjenkins = ’’’#本?諾牧磽庖桓?enkins的conf文件 <project> <actions/> <description></description> <keepDependencies>false</keepDependencies> <properties><hudson.model.ParametersDefinitionProperty> <parameterDefinitions> <hudson.model.StringParameterDefinition> <name>Branch</name> <description></description> <defaultValue>%s</defaultValue> </hudson.model.StringParameterDefinition> </parameterDefinitions></hudson.model.ParametersDefinitionProperty> </properties> <scm /> <canRoam>true</canRoam> <disabled>false</disabled> <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding> <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding> <triggers/> <concurrentBuild>false</concurrentBuild> <builders><hudson.tasks.Shell> <command>xxxx</command></hudson.tasks.Shell> </builders> <publishers/> <buildWrappers/> </project>’’’ def __conn_jenkins_server(self, url): try: #獲得一個(gè)jenkins的操作實(shí)例 server = jenkins.Jenkins(url, username=self.username, password=self.password) return server except Exception: logging.warning(’login jenkins failed!’) return None def create_project(self, host_ip, project_name, git_path, git_branch, url, environment): server = self.__conn_jenkins_server(url) if server: server.create_job(project_name, self.php_jenkins)#參數(shù)1寫(xiě)的是項(xiàng)目名稱(chēng),參數(shù)2是xml文檔 return True else: return None def project_built(self, url, project_name, git_branch):#這個(gè)函數(shù)作用是構(gòu)建項(xiàng)目 server = self.__conn_jenkins_server(url) server.build_job(project_name, {’Branch’: git_branch}) def check_project_exist(self, project_name, url):#這個(gè)函數(shù)是檢查項(xiàng)目是否已經(jīng)存在雖然寫(xiě)得很挫忘不要見(jiàn)怪 server = self.__conn_jenkins_server(url) name = server.get_job_name(project_name) if name is None: return False return True
詳細(xì)可以看官方文檔:http://python-jenkins.readthedocs.io/en/latest/api.html
補(bǔ)充知識(shí):python調(diào)用jenkinsapi
在通過(guò)python 調(diào)用jenkinsapi的時(shí)候,需要對(duì)一些作業(yè)進(jìn)行定時(shí)對(duì)構(gòu)建
報(bào)錯(cuò):
<title>Error 403 No valid crumb was included in the request</title>n</head>n<body><h2>HTTP ERROR 403</h2>
原因是在jenkins的安全配置里勾選里下面這個(gè)選項(xiàng),在預(yù)防跨站點(diǎn)請(qǐng)求,將其勾掉即可。
以上這篇python的Jenkins接口調(diào)用方式就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Laravel操作session和cookie的教程詳解2. html小技巧之td,div標(biāo)簽里內(nèi)容不換行3. XML入門(mén)的常見(jiàn)問(wèn)題(一)4. css進(jìn)階學(xué)習(xí) 選擇符5. 將properties文件的配置設(shè)置為整個(gè)Web應(yīng)用的全局變量實(shí)現(xiàn)方法6. PHP字符串前后字符或空格刪除方法介紹7. jsp實(shí)現(xiàn)登錄界面8. 解析原生JS getComputedStyle9. 淺談SpringMVC jsp前臺(tái)獲取參數(shù)的方式 EL表達(dá)式10. Echarts通過(guò)dataset數(shù)據(jù)集實(shí)現(xiàn)創(chuàng)建單軸散點(diǎn)圖
