jenkins 版本 1.6.10
不多說,如果你還沒搞定這個接口,請運行下面的代碼,幫你創建一個 jenkins 任務
@Test
public void post_xml() throws IOException {
String server = "127.0.0.1:8080" ;
String jenkinsHost = "http://"+server+"/" ;
String projectName = "TEST44" ;
String configurationFile = "D:\\workspace\\Qone\\target\\classes\\jobTemplate\\demo.xml";
String username = "xxxxxxxx";
String apiToken = "xxxxxxxxxxxxxxxxxxxxxxxx" ;
HttpClient client = new HttpClient();
client.getState().setCredentials(
new AuthScope(server,443,"realm"),
new UsernamePasswordCredentials(username,apiToken)
);
client.getParams().setAuthenticationPreemptive(true);
PostMethod post = new PostMethod(jenkinsHost+"/createItem?name="+projectName);
post.setDoAuthentication(true);
File fileInput = new File(configurationFile);
RequestEntity requestEntity = new FileRequestEntity(fileInput,"text/xml; charset=UTF-8");
post.setRequestEntity(requestEntity);
int code = client.executeMethod(post);
System.out.print(post.getResponseBodyAsString());
}
https://github.com/RisingOak/jenkins-client 有人已經在此處封裝了jenkins 的操作API ,可以下載直接使用,但其創建任務的方法我沒有走通,我這邊問題出在 crumb 的獲取,此地址在我的 jenkins 服務上訪問不了 ,於是另尋出路
if (crumbFlag == true) {
Crumb crumb = get("/crumbIssuer", Crumb.class);
if (crumb != null) {
request.addHeader(new BasicHeader(crumb.getCrumbRequestField(), crumb.getCrumb()));
}
}
查看jenkins 用戶信息,點擊設置,看到 API TOKEN 一項信息
於是找到這篇文章 https://wiki.jenkins-ci.org/display/JENKINS/Authenticating+scripted+clients
文章中給出了 groovy script 生成任務的方法,於是根據 個人groovy 的邏輯寫出上述 JAVA 實現 。
運行結果