如何覆盖Jenkins参数化构建的默认值?

时间:2015-05-21 08:08:32

标签: json curl jenkins jobs parameterized

我有一个参数化的工作" dummy"什么调用ANT脚本:

<?xml version="1.0" encoding="UTF-8"?>
<project name="dummy" basedir="." default="sayit">
    <target name="sayit">
        <echo message="p_foo: ${p_foo}" level="info" />
    </target>
</project>

当我从命令行执行ANT

ant -Dp_foo=12345678

显示

sayit:
    [echo] p_foo: 12345678

作业的配置定义名称 p_foo 字符串参数,默认值 bar 以后在构建部分,其中调用了ANT

Properties: p_foo=$p_foo

当我从仪表板运行作业时,它会提示我输入 p_foo 的值,然后回显该值。

现在我需要使用 cURL Jersey REST API 来获得相同的结果。

在命令行中我调用

curl -X POST http://localhost:8080/job/dummy/buildWithParameters?delay=0sec \
  --data-urlencode json="{'parameter':[{'name':'p_foo', 'value':'1111'}:]}"

作业已执行,但 p_foo 的值保持不变( bar )。

这种做法有什么不对?当REST解决方案没问题时,cURL解决方案可能会有效。

以上命令行调用有效,但以下Jersey客户端不会:

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;

import javax.ws.rs.core.UriBuilder;
import java.net.URI;
import java.util.logging.Level;
import java.util.logging.Logger;

    public class RestClient {

    public static final Logger LOGGER = Logger.getLogger(RestClient.class.getSimpleName());

    private static final String JOB_URI = "http://%s:%s/job/%s/build";
    private static final String JOB_JSON_PARAM = "{\"parameter\":[{\"name\":\"%s\",\"value\":\"%s\"}]}";

    private String hostname;
    private String port;
    private String username;
    private String password;

    public RestClient(String p_hostname, String p_port, String p_username, String p_password) {
        this.hostname = p_hostname;
        this.port = p_port;
        this.username = p_username;
        this.password = p_password;
    }

    public String getHostname() {
        return this.hostname;
    }

    public String getPort() {
        return this.port;
    }

    public String getUsername() {
    return this.username;
    }

    public String getPassword() {
        return this.password;
    }

    private URI getBaseURI(String p_url) {
        return UriBuilder.fromUri(p_url).build();
    }

    private WebResource getWebResource(String p_url) {
        ClientConfig config = new DefaultClientConfig();
        Client client = Client.create(config);
        client.addFilter(new HTTPBasicAuthFilter(this.username, this.password));
        return client.resource(getBaseURI(p_url));
    }

    public String startJenkinsJob(String p_jobname) {
        String uri = String.format(JOB_URI, this.getHostname(), this.getPort(), p_jobname);
        LOGGER.log(Level.INFO, uri);
        WebResource oWebResource = this.getWebResource(uri);
        String json = String.format(JOB_JSON_PARAM, "p_foo", "1111");
        LOGGER.log(Level.INFO, json);
        ClientResponse response = oWebResource.accept("application/json")
                .type("application/json").post(ClientResponse.class, json);
        /*if (response.getStatus() != 200) {
            throw new RuntimeException("Failed : HTTP error code : "
                    + response.getStatus());
        }*/
        return response.getEntity(String.class);
    }

        public static void main(String[] args) {
            RestClient rc = new RestClient("localhost", "8080", "user", "password");
        LOGGER.log(Level.INFO, rc.startJenkinsJob("dummy"));
        rc = null;
    }
}

2 个答案:

答案 0 :(得分:1)

使用build端点代替buildWithParameters并删除最后一个冒号

curl -X POST http://localhost:8080/job/dummy/build\?delay\=0sec \ 
  --data-urlencode json="{'parameter':[{'name':'foo', 'value':'1111'}]}"

答案 1 :(得分:0)

以上命令行调用有效,但以下Jersey客户端不会:

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;

import javax.ws.rs.core.UriBuilder;
import java.net.URI;
import java.util.logging.Level;
import java.util.logging.Logger;

    public class RestClient {

    public static final Logger LOGGER = Logger.getLogger(RestClient.class.getSimpleName());

    private static final String JOB_URI = "http://%s:%s/job/%s/build";
    private static final String JOB_JSON_PARAM = "{\"parameter\":[{\"name\":\"%s\",\"value\":\"%s\"}]}";

    private String hostname;
    private String port;
    private String username;
    private String password;

    public RestClient(String p_hostname, String p_port, String p_username, String p_password) {
        this.hostname = p_hostname;
        this.port = p_port;
        this.username = p_username;
        this.password = p_password;
    }

    public String getHostname() {
        return this.hostname;
    }

    public String getPort() {
        return this.port;
    }

    public String getUsername() {
    return this.username;
    }

    public String getPassword() {
        return this.password;
    }

    private URI getBaseURI(String p_url) {
        return UriBuilder.fromUri(p_url).build();
    }

    private WebResource getWebResource(String p_url) {
        ClientConfig config = new DefaultClientConfig();
        Client client = Client.create(config);
        client.addFilter(new HTTPBasicAuthFilter(this.username, this.password));
        return client.resource(getBaseURI(p_url));
    }

    public String startJenkinsJob(String p_jobname) {
        String uri = String.format(JOB_URI, this.getHostname(), this.getPort(), p_jobname);
        LOGGER.log(Level.INFO, uri);
        WebResource oWebResource = this.getWebResource(uri);
        String json = String.format(JOB_JSON_PARAM, "p_foo", "1111");
        LOGGER.log(Level.INFO, json);
        ClientResponse response = oWebResource.accept("application/json")
                .type("application/json").post(ClientResponse.class, json);
        /*if (response.getStatus() != 200) {
            throw new RuntimeException("Failed : HTTP error code : "
                    + response.getStatus());
        }*/
        return response.getEntity(String.class);
    }

        public static void main(String[] args) {
            RestClient rc = new RestClient("localhost", "8080", "user", "password");
        LOGGER.log(Level.INFO, rc.startJenkinsJob("dummy"));
        rc = null;
    }
}