Chr*_*ton 3 java parameter-passing jenkins
我创建了一个参数化的 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);
Run Code Online (Sandbox Code Playgroud)
所以这很简单,因为我只是将Strings传递给工作。但是,我现在想File使用File Parameterin Jenkins将 A 传递给工作。
我看到的一件事是File ParameterJenkins 中有一个File Location和File Description。所以甚至不确定如何将它设置为来自 Java 的参数。
这可能吗?
这是一个可运行的类。使用 apache-httpclient (4.5.1) 和相关的 jars。关键是在 MultiPart 表单提交中使用 /build/ URL。此处描述了远程 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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1415 次 |
| 最近记录: |