[英]How to pass file to Jenkins with Java
我創建了一個參數化的Jenkins作業,我將變量傳遞給我的Java。
這是Java:
final HttpClient client = new HttpClient();
final PostMethod buildMethod = new PostMethod(Constants.SVN2GIT_QA_URL);
buildMethod.setParameter(Constants.GIT_URL_PARAM, gitUrl);
buildMethod.setParameter(Constants.PASSWORD_PARAM, password);
buildMethod.setParameter(Constants.SVN2GIT_COMMAND, svn2gitCommand);
buildMethod.setParameter(Constants.SVN2GIT_EMAIL, email);
buildMethod.setParameter(Constants.REPO_NAME, repoName);
client.executeMethod(buildMethod);
所以這很簡單,因為我只是將String
傳遞給了這份工作。 但是,我現在想使用Jenkins中的File Parameter
將A File
傳遞給作業。
我看到的一件事是Jenkins中的File Parameter
有一個File Location
和一個File Description
。 所以不確定如何將它設置為Java中的參數。
這可能嗎?
這是一個可運行的類。 使用apache-httpclient(4.5.1)和相關的jar。 關鍵是使用/ build / URL和MultiPart Form提交。 此處描述了遠程API
package my;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.FormBodyPartBuilder;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.HttpClientBuilder;
import java.io.File;
import java.io.IOException;
class JenkinsClientExample {
void helloJenkins() throws IOException {
String server = "localhost";
String jenkinsHost = "http://" + server + ":8080";
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
HttpClient httpClient = httpClientBuilder.build();
String payLoad="{ \"parameter\": [{\"name\":\"FILE1_PARAM\",\"file\":\"file0\"}, {\"name\":\"FILE2_PARAM\",\"file\":\"file1\"},{\"name\":\"STRING_PARAM\", \"value\":\"2014\"}, " +
"{\"name\":\"BOOLEAN_PARAM\", \"value\":\"TRUE\"} ] }";
File file = new File("c:/dummy.txt");
File file2 = new File("c:/another.txt");
FormBodyPartBuilder formBodyPartBuilder3 = FormBodyPartBuilder.create("file0", new FileBody(file, ContentType.TEXT_PLAIN));
FormBodyPartBuilder formBodyPartBuilder4 = FormBodyPartBuilder.create("file1", new FileBody(file2, ContentType.TEXT_PLAIN));
FormBodyPartBuilder formBodyPartBuilder1 = FormBodyPartBuilder.create("json", new StringBody(payLoad, ContentType.TEXT_PLAIN));
HttpEntity entity = MultipartEntityBuilder
.create()
.addPart(formBodyPartBuilder3.build())
.addPart(formBodyPartBuilder4.build())
.addPart(formBodyPartBuilder1.build())
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
.build();
//must be the build URL not buildWithParameters
HttpPost httpPost = new HttpPost(jenkinsHost + "/job/fake.UpdateCQ_VersionFixed/build");
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
HttpEntity result = response.getEntity();
System.out.println(result);
System.out.println(response.toString());
}
public static void main(String[] args) {
try {
new JenkinsClientExample().helloJenkins();
} catch (IOException e) {
e.printStackTrace();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.