2015-12-29 34 views
3

我已经创建了一个参数化的Jenkins作业,我将变量从Java传递到该作业。如何通过Java将文件传递给Jenkins

这里是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 Parameter将A File传递给工作。

我看到的一件事是詹金斯的File ParameterFile LocationFile Description。所以不确定如何将其设置为来自Java的参数。

这可能吗?

+0

将文件设置为字符串然后在Jenkins端重新创建文件会更简单吗?这只是一个文本文件。 –

+0

类似的,但在Groovy。你可以从中提取java。 http://stackoverflow.com/questions/33840277/how-to-make-http-call-with-file-in-groovy-to-upload-a-file-and-build-arguments – Jayan

回答

3

这是一个可运行的类。使用apache-httpclient(4.5.1)和相关的jar。关键是使用/ build/URL与MultiPart表单提交。描述远程API 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(); 
     } 
    } 
} 
相关问题