本文共 2051 字,大约阅读时间需要 6 分钟。
pip3 install python-jenkins
注意:是安装python-jenkins模块,不是jenkins模块,我一开始使用pip3 install jenkins安装jenkins模块,当进入python3尝试导入jenkins模块时出错
>>> import jenkinsTraceback (most recent call last): File "", line 1, in File "/usr/local/lib/python3.6/site-packages/jenkins.py", line 9, in lookup3 = cdll.LoadLibrary(os.path.join(get_python_lib(), "lookup3.so")) File "/usr/local/lib/python3.6/ctypes/__init__.py", line 426, in LoadLibrary return self._dlltype(name) File "/usr/local/lib/python3.6/ctypes/__init__.py", line 348, in __init__ self._handle = _dlopen(self._name, mode)OSError: /usr/local/lib/python3.6/site-packages/lookup3.so: cannot open shared object file: Nor directory>>>
使用明文密码登录jenkins并获取版本信息,
>>> import jenkins>>> server = jenkins.Jenkins('http://localhost:8081', username='sudley', password='111111')>>> server = jenkins.Jenkins('http://192.168.37.133:8081', username='sudley', password='111111')>>> user = server.get_whoami()>>> version = server.get_version()>>> print('Hello %s from Jenkins %s' % (user['fullName'], version))Hello sudley from Jenkins 2.208>>>
获取token:在web页面使用某用户登录jenkins页面,点击右上方用户->设置->添加新Token
注意:Token只有创建的时候可以拷贝 获取版本信息>>> username="sudley">>> sudley_token = "11220150210b953d16e198c4ef6b7df387">>> jenkins_url = 'http://%s:%s@192.168.37.133:8081' % (username,sudley_token)>>> print(jenkins_url)http://sudley:11220150210b953d16e198c4ef6b7df387@192.168.37.133:8081>>> server = jenkins.Jenkins(jenkins_url)>>> user = server.get_whoami()>>> version = server.get_version()>>> print('Hello %s from Jenkins %s' % (user['fullName'], version))Hello sudley from Jenkins 2.208>>>
这个官方文档的实例其实没看明白,文档里面使用pip install kerberos安装kerberos模块后就能直接登录使用,
import jenkinsserver = jenkins.Jenkins('http://localhost:8081')print (server.jobs_count())
但是实际验证过程中执行print (server.jobs_count())时会报用户验证失败的错误
jenkins.JenkinsException: Error in request. Possibly authentication failed [403]: Forbidden 希望哪位网友知道具体方法可以指教一下,感激不尽~ 参考链接:转载地址:http://wuiti.baihongyu.com/