如何使用Java将文件传递给Jenkins [英] How to pass file to Jenkins with Java
问题描述
我已经创建了一个参数化的Jenkins作业,我正在将Java中的变量传递给该作业.
I have created a parameterized Jenkins job which I am passing variables to from my 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
s传递给工作.但是,我现在想使用Jenkins中的 File
传递给作业.
So this is pretty straight forward because I am only passing String
s to the job. However, I would now like to pass A File
to the job using the File Parameter
in Jenkins.
我看到的一件事是Jenkins中的 File Parameter
有一个 File Location
和一个 File Description
.因此,甚至不知道如何通过Java将其设置为参数.
One thing I see is that the File Parameter
in Jenkins has a File Location
and a File Description
. So not sure even how to set it to the parameter from Java.
这可能吗?
推荐答案
这是一个可运行的类.使用apache-httpclient(4.5.1)和相关的jar.关键是将/build/URL与MultiPart Form提交一起使用.在此处
Here is a runnable class. Uses apache-httpclient (4.5.1) and related jars. Key is to use /build/ URL with MultiPart Form submission. Remote API is described here
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();
}
}
}
这篇关于如何使用Java将文件传递给Jenkins的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!