jenkins,client, API,用户凭证
一、背景
我们目前正在对Jenkins进行二次开发,开源社区提供了针对jenkins的API,如:https://github.com/jenkinsci/java-client-api
但是,API支持的功能并不是很完善,如没有发现支持用户凭证的相关接口, 因此,只能通过其他解决方案来实现。
二、解决方案
2.1 curl命令行
2.1.1 方式一
curl -X POST 'http://user:token@jenkins_server:8080/credentials/store/system/domain/_/createCredentials' \
--data-urlencode 'json={
"": "0",
"credentials": {
"scope": "GLOBAL",
"id": "identification",
"username": "manu",
"password": "bar",
"description": "linda",
"$class": "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl"
}
}'
2.1.2 方式二
如果方式一,报403错误,如下所示:
HTTP ERROR 403
Problem accessing /credentials/store/system/domain/_/createCredentials. Reason:
No valid crumb was included in the request
Powered by Jetty://
可以使用下面的方式
CRUMB=$(curl -s 'http://user:token@jenkins_server:8080/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)')
curl -H $CRUMB -X POST 'http://user:token@jenkins_server:8080/credentials/store/system/domain/_/createCredentials' \
--data-urlencode 'json={
"": "0",
"credentials": {
"scope": "GLOBAL",
"id": "identification",
"username": "manu",
"password": "bar",
"description": "linda",
"$class": "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl"
}
}'
2.