jenkins python-API打包问题
jenkins Error 403 异常
安装新版本jenkins,想使用代码调用api打包,但是确发生如下403异常:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 403 No valid crumb was included in the request</title>
</head>
<body><h2>HTTP ERROR 403 No valid crumb was included in the request</h2>
<table>
<tr><th>URI:</th><td>/job/%E6%B5%8B%E8%AF%95/build</td></tr>
<tr><th>STATUS:</th><td>403</td></tr>
<tr><th>MESSAGE:</th><td>No valid crumb was included in the request</td></tr>
<tr><th>SERVLET:</th><td>Stapler</td></tr>
</table>
<hr><a href="https://eclipse.org/jetty">Powered by Jetty:// 9.4.38.v20210224</a><hr/>
</body>
</html>
这个问题查了好久,有版本说是跨站请求保护,但是新版本的jenkins(2.204.6之后)就没有这个按钮了,默认就是启用的;在一个文章中查到在启动jenkins的时候可以添加参数禁用csrf的功能;文章连接→禁用csrf
也就是在jenkins启动的时候,加入如下参数:
-Dhudson.security.csrf.GlobalCrumbIssuerConfiguration.DISABLE_CSRF_PROTECTION=true
我是用war启动的,但是即便加入了这个参数还是不行;没有找到究竟怎么解决,在官网看到了对应python封装包,好吧最终成功解决了这个问题。
python-jenkins
python-jenkins的官网连接
在使用ipython测试的时候,成功传入了参数,build成功。
安装包可以直接使用pip安装即可: pip install python-jenkins
一个简单的实例:
In [1]: import jenkins
In [2]: server = jenkins.Jenkins('http://host:7799', username='admin',password='123456')
In [36]: p1 # 参数的形式{key:value,},可以根据实际应用传参
Out[36]:
{'branch': 'develop',
'map_uuid_name': 'xxxxx',
'modules': 'xxx',
'release': '35',
'version': '3.0.0'}
In [37]: resp = server.build_job(u"测试", parameters=p1)
In [38]: resp
Out[38]: 11