Jenkins是一个开源软件项目,是基于Java开发的一种[持续集成]工具,用于监控持续重复的工作,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能
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的操作的空的空缺,实现对Jenkins的Credentials通过API的方式进行简单的操作.
操作jenkins Credentials的一般步骤在界面上为 凭据>系统>全局凭据>添加凭据 输入对应的内容即可,如图所示
界面操作中需要填写如下内容
确认之后会生成一条新的凭证,可以在job页面使用
当新增一条凭据信息时可以通过浏览器的网络监控看到他的请求内容
以上截图为chrom调试
从上图可以看到 请求的path为
/credentials/store/system/domain/_/createCredentials
请求凭证的内容较多,如下图:
可以看到操作凭证的请求地址、请求参数列表,这里的参数列表的参数非常的多,但实际上在源码(https://github.com/jenkinsci/jenkins)中读取的只有红框中的json的这一段。用postman来模拟调用一下
请求结果是:结果生成页面的html内容,若请求错误会报错 http://localhost:8080/jenkins/credentials/store/system/domain/_/ 可以到上述url查看生成效果: 基于以上,我们封装简单的增删改查方法:
通过jenkins-client发送请求。
Java中通过JenkinsServer获取JenkinsHttpClient对象,使用postFromWithResult方法发送请求报文即可,但这里判断返回值的时候不能使用200判断结果,因为返回结果是一个重定向请求(302)。
HttpResponse httpResponse = staticJenkinsHttpClient.post_form_with_result("/credentials/store/system/domain/_/createCredentials", data, crumbFlag);
# !/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是否存在,匹配返回错误的字符串即可.在不同环境上可能需要稍加改善