当前位置: 首页 > 面试题库 >

如何使用Java将文件传递给Jenkins

巫马泰
2023-03-14
问题内容

我创建了一个参数化的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);

所以这很简单,因为我只是将Strings 传递给工作。但是,我现在想File使用File ParameterJenkins中的A将A传递给工作。

我看到的一件事是File ParameterJenkins中的拥有一个File LocationFile 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();
        }
    }
}


 类似资料:
  • 我在集群中有以下代码: csv_file=“/usr/usr1/test.csv” 但是程序不是在挑FI。有人能帮忙吗?

  • 我有一个通过PHP文件调用的bash脚本。首先,这些文件的内容如下: script.sh php文件 在上面的例子中,通过运行类似于的内容来查找。 但是,当运行PHP文件来获取相同的信息时,我得到了错误。 最终,我想通过一个PHP文件运行一个bash脚本,包括参数。 我花了大部分时间尝试使用这个答案中提出的解决方案,感觉我的PHP文件应该可以工作。 我知道我的脚本和编码肯定很混乱,显然没有工作——

  • 问题内容: 我必须在6个以上的环境中运行相同的其余api脚本集。因此,我已将所有测试数据和端点/资源路径存储在json文件中。然后,我尝试将此json文件读入我的karate- config.js文件,这是因为我要获取与从命令行(karate.env)传递的环境相对应的数据,该环境正在读入我的空手道-config.js文件 以下是我的json文件示例 下面是我的karate-config.js文件

  • 问题内容: 谁能建议我如何将参数传递给线程? 另外,它如何用于匿名类? 问题答案: 你需要将构造函数中的参数传递给Runnable对象: 然后调用它:

  • 问题内容: 我正在尝试编写一个bash脚本,该脚本允许用户使用通配符传递目录路径。 例如, 在此目录中执行时 将输出: 现在的样子,它输出: 的内容: 问题答案: 父外壳(一个调用)为您扩展了外壳。 在脚本中,您需要使用: 双引号确保正确处理文件名中的多个空格等。 可能令人困惑的附录 如果您确实确定要扩展该脚本,则必须确保将其传递给脚本(如其他答案中所述,用引号引起来),然后确保在正确的位置将其扩

  • 问题内容: 我是webpack的新手,几乎可以解决所有构建部分的问题,但是现在的问题是,我想将环境变量从.env文件传递到webpack config,以便可以通过插件将这些变量传递给我的构建文件。 目前,我能够将环境变量直接从webpack传递到我的构建中。请查看下面我在webpack中使用的代码。 我的构建脚本是 问题答案: 您可以为此目的使用包装 参考:https : //www.npmjs