Jenkins是一个开源软件项目,是基于Java开发的一种[持续集成]工具,用于监控持续重复的工作,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能,jenkins提供了许多便利的api如
Java api常用的有 :
https://github.com/jenkinsci/java-client-api
python api 常用的有以下两种:
https://pypi.org/project/python-jenkins/
https://pypi.org/project/jenkinsapi/
目前所存在API中对Jenkins的Credentials的操作基本都为空缺,这里以Python为例,实现jenkins Credentials的基本操作
操作jenkins Credentials的一般步骤在界面上为 凭据>系统>全局凭据>添加凭据 输入对应的内容即可,如图所示当新增一条凭据信息时可以通过浏览器的网络监控看到他的请求内容
可以看到操作凭证的请求地址、请求参数列表,这里的参数列表的参数非常的多,但实际上在源码中读取的只有红框中的json的这一段。用postman来模拟调用一下
填写内容如下
- 请求url使用post请求方式
- body内容为红框中的json
- 使用 postman的Basic Auth认证
请求结果是:结果生成页面的html内容,若请求错误会报错
http://localhost:8080/jenkins/credentials/store/system/domain/_/
可以到上述url查看生成效果:
基于以上,我们封装简单的增删改查方法:
java jenkins-client(0.3.8以上版本):
通过jenkins-client发送请求。
Java中通过JenkinsServer获取JenkinsHttpClient对象,使用postFromWithResult方法发送请求报文即可,但这里判断返回值的时候不能使用200判断结果,因为返回结果是一个重定向请求(302)。
HttpResponse httpResponse = staticJenkinsHttpClient.post_form_with_result("/credentials/store/system/domain/_/createCredentials", data, crumbFlag);
python-jenkins(基于python3.7) 封装代码如下
# !/usr/bin/python
# -*- coding: UTF-8 -*-
import requests
# 注:host请直接传入机器ip, 域名由于dns解析等可能会导致Jenkins解析失败
class jenkins_credentials:
# 传入值请使用双引号
def createEOSCredentials(self, host, userName, passworld, id, des, jenkinsUserName, jenkinsPassworld):
url = host + "/credentials/store/system/domain/_/createCredentials"
json = {"": "0", "credentials": {"scope": "GLOBAL", "username": userName, "password": passworld,
"$redact": "password", "id": id,
"description": des,
"stapler-class": "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl",
"$class": "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl"}}
data = {}
data["json"] = str(json)
result = requests.post(url, data=data, auth=(jenkinsUserName, jenkinsPassworld))
print(result.text)
# 传入值请使用双引号
def deleteEOSCredentials(self, host, id, jenkinsUserName, jenkinsPassworld):
url = host + "/credentials/store/system/domain/_/credential/" + id + "/doDelete"
requests.post(url, auth=(jenkinsUserName, jenkinsPassworld))
# 传入值请使用双引号
def updateEOSCredentials(self, host, userName, passworld, id, des, jenkinsUserName, jenkinsPassworld):
url = host + "/credentials/store/system/domain/_/credential/" + id + "/updateSubmit"
json = {"stapler-class": "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl",
"scope": "GLOBAL", "username": userName, "password": passworld, "$redact": "password",
"id": id, "description": des}
data = {}
data["json"] = str(json)
result = requests.post(url, data=data, auth=(jenkinsUserName, jenkinsPassworld))
print(result.text)
def searchEOSCredentials(self, host, id, jenkinsUserName, jenkinsPassworld):
url = host + "/credentials/store/system/domain/_/credential/" + id + "/"
result = requests.post(url, auth=(jenkinsUserName, jenkinsPassworld))
error_top = "The requested resource is not available."
if error_top in result.text:
return False
else:
return True
上述python代码中 增删改都可以直接复用,searchEOSCredentials方法请求当前id的credential是否存在,匹配返回错误的字符串即可.在不同环境上可能需要稍加改善
以上,如有问题请留言