【问题标题】:How to override the default value of a Jenkins parameterized build?如何覆盖 Jenkins 参数化构建的默认值?
【发布时间】:2015-05-21 08:08:32
【问题描述】:

我有一个调用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 和默认值 barString Parameter,稍后将在 Build 中重用它strong> 调用ANT 的部分:

Properties: p_foo=$p_foo

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

现在我需要使用 cURLJersey 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)。

这种方法有什么问题?当cURL 解决方案正常时,REST 解决方案可能会起作用。

上述命令行调用有效,但以下 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;
    }
}

【问题讨论】:

    标签: json curl jenkins jobs parameterized


    【解决方案1】:

    使用build端点代替buildWithParameters并去掉最后一个冒号

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

    【讨论】:

      【解决方案2】:

      上述命令行调用有效,但以下 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;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-20
        • 1970-01-01
        相关资源
        最近更新 更多