Skip to main content
 首页 » 编程设计

python之无法使用 jenkinsapi 在 Jenkins 中创建 View

2025年05月04日10luoye11

我正在尝试使用 Python 和 jenkinsapi 在 Jenkins 中创建一堆嵌套 View 。

代码如下:

jenkins_url = "http://127.0.0.1:8080/" 
api = Jenkins(jenkins_url, 'jenkins_admin', 'jenkins_password') 
logger.info('view_name is %s' %view_name) 
new_view = api.views.create(view_name, Views.NESTED_VIEW) 

如果我禁用授权并允许“任何人都可以做任何事情”但用户/密码身份验证失败,一切都会顺利进行。我们正在使用 Active Directory,并且用户具有管理权限。

失败是:

INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): 127.0.0.1 
DEBUG:requests.packages.urllib3.connectionpool:"GET /api/python **HTTP/1.1" 200 3290** 
INFO:root: view_name is 9.8 
INFO:jenkinsapi.views:Creating "hudson.plugins.nested_view.NestedView" view "9.8" 
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): 127.0.0.1 
DEBUG:requests.packages.urllib3.connectionpool:"POST /createView **HTTP/1.1" 302 0** 
DEBUG:requests.packages.urllib3.connectionpool:"GET /view/9.8/configure **HTTP/1.1" 401 1445** 
Traceback (most recent call last): 
  File "JenkinsNewVersionJobsItems.py", line 46, in <module> 
    top_view = NewVersionJobsItemsUtils.createView(short_version, api, NESTED) 
  File "/tmp/NewVersionJobsItemsUtils.py", line 35, in createView 
    new_view = parent_view.views.create(view_name, Views.NESTED_VIEW) 
  File "build/bdist.linux-x86_64/egg/jenkinsapi/views.py", line 103, in create 
  File "build/bdist.linux-x86_64/egg/jenkinsapi/utils/requester.py", line 117, in post_and_confirm_status 
jenkinsapi.custom_exceptions.JenkinsAPIException: Operation failed. url=http://127.0.0.1/view/9.8/configure, data={'json': '{"name": "9.8", "mode": "hudson.plugins.nested_view.NestedView"}', 'name': '9.8', 'Submit': 'OK', 'mode': 'hudson.plugins.nested_view.NestedView'}, headers={'Content-Type': 'application/x-www-form-urlencoded'}, status=401, text=<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/> 
<title>**Error 401 Invalid password/token for user: jenkins**</title> 
</head> 
<body><h2>HTTP ERROR 401</h2> 

我看到身份验证已通过, View 已创建,我被重定向 (HTTP 302) 到其配置页面,但因密码错误而失败。

执行后我可以看到新创建的 View ,再次运行脚本找到 View ,创建下一个 View 并再次失败并出现相同的错误。问题是我需要一打这样的 View 。

谁能帮我指出问题所在? 提前致谢。

请您参考如下方法:

如果您尝试创建嵌套 View ,但没有安装嵌套 View 插件,就会出现这种情况。也许这是你的问题?