`
kiikoo
  • 浏览: 7754 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

hudson remote API

阅读更多
1、hudson API create job
public static void main(String[] args) throws Exception {
        HttpClient client = new HttpClient();
        String hudson = "http://10.20.157.179:8080/hudson/";
        File configFile = new File("config.xml");
        put(client, hudson, "test", configFile);
    }

    private static void put(HttpClient client, String hudsonBaseURL, String jobName, File configFile)
                                                                                                     throws IOException,
                                                                                                     HttpException {
        PostMethod postMethod = new PostMethod(hudsonBaseURL + "/createItem?name=" + jobName);
        client.getParams().setAuthenticationPreemptive(true);
        Credentials defaultcreds = new UsernamePasswordCredentials("admin", "sfhudson");
        client.getState().setCredentials(new AuthScope("10.20.157.179", 8080), defaultcreds);
        postMethod.setRequestHeader("Content-type", "application/xml; charset=ISO-8859-1");
        postMethod.setRequestBody(new FileInputStream(configFile));
        postMethod.setDoAuthentication(true);
        try {
            int status = client.executeMethod(postMethod);
            System.out.println(status + "\n" + postMethod.getResponseBodyAsString());
        } finally {
            postMethod.releaseConnection();
        }
    }


2、update hudson job
public static void main(String[] args) throws Exception {
        HttpClient client = new HttpClient();
        String hudson = "http://10.20.157.179:8080/hudson/";
        File configFile = new File("config.xml");
        put(client, hudson, "test", configFile);
    }

    private static void put(HttpClient client, String hudsonBaseURL, String jobName, File configFile)
                                                                                                     throws IOException,
                                                                                                     HttpException {
        PostMethod postMethod = new PostMethod(hudsonBaseURL + "/job/" + jobName + "/config.xml");
        client.getParams().setAuthenticationPreemptive(true);
        Credentials defaultcreds = new UsernamePasswordCredentials("admin", "sfhudson");
        client.getState().setCredentials(new AuthScope("10.20.157.179", 8080), defaultcreds);
        postMethod.setRequestHeader("Content-type", "application/xml; charset=ISO-8859-1");
        postMethod.setRequestBody(new FileInputStream(configFile));
        postMethod.setDoAuthentication(true);
        try {
            int status = client.executeMethod(postMethod);
            System.out.println(status + "\n" + postMethod.getResponseBodyAsString());
        } finally {
            postMethod.releaseConnection();
        }
    }

3、copy hudson job
public static void main(String[] args) throws Exception {
        HttpClient client = new HttpClient();
        String hudson = "http://10.20.157.179:8080/hudson/";
        File configFile = new File("config.xml");
        put(client, hudson, "test", configFile);
    }

    private static void put(HttpClient client, String hudsonBaseURL, String jobName, File configFile)
                                                                                                     throws IOException,
                                                                                                     HttpException {
        PostMethod postMethod = new PostMethod(hudsonBaseURL + "/createItem");
        NameValuePair n1 = new NameValuePair("name", "copyNew");
        NameValuePair n2 = new NameValuePair("mode", "copy");
        NameValuePair n3 = new NameValuePair("from", jobName);
        postMethod.setQueryString(new NameValuePair[] { n1, n2, n3 });
        client.getParams().setAuthenticationPreemptive(true);
        Credentials defaultcreds = new UsernamePasswordCredentials("admin", "sfhudson");
        client.getState().setCredentials(new AuthScope("10.20.157.179", 8080), defaultcreds);
        postMethod.setRequestHeader("Content-type", "application/xml; charset=ISO-8859-1");
        postMethod.setRequestBody(new FileInputStream(configFile));
        postMethod.setDoAuthentication(true);
        try {
            int status = client.executeMethod(postMethod);
            System.out.println(status + "\n" + postMethod.getResponseBodyAsString());
        } finally {
            postMethod.releaseConnection();
        }
    }

4、delete job
public static void main(String[] args) throws Exception {
        HttpClient client = new HttpClient();
        String hudson = "http://10.20.157.179:8080/hudson/";
        put(client, hudson, "copyNew");
    }

    private static void put(HttpClient client, String hudsonBaseURL, String jobName) throws IOException, HttpException {
        PostMethod postMethod = new PostMethod(hudsonBaseURL + "/job/" + jobName + "/doDelete");
        client.getParams().setAuthenticationPreemptive(true);
        Credentials defaultcreds = new UsernamePasswordCredentials("admin", "sfhudson");
        client.getState().setCredentials(new AuthScope("10.20.157.179", 8080), defaultcreds);
        postMethod.setDoAuthentication(true);
        try {
            int status = client.executeMethod(postMethod);
            System.out.println(status + "\n" + postMethod.getResponseBodyAsString());
        } finally {
            postMethod.releaseConnection();
        }
    }

5、enable disable job
public static void main(String[] args) throws Exception {
        HttpClient client = new HttpClient();
        String hudson = "http://10.20.157.179:8080/hudson/";
        put(client, hudson, "test");
    }

    private static void put(HttpClient client, String hudsonBaseURL, String jobName) throws IOException, HttpException {
        PostMethod postMethod = new PostMethod(hudsonBaseURL + "/job/" + jobName + "/disable");
        client.getParams().setAuthenticationPreemptive(true);
        Credentials defaultcreds = new UsernamePasswordCredentials("admin", "sfhudson");
        client.getState().setCredentials(new AuthScope("10.20.157.179", 8080), defaultcreds);
        postMethod.setDoAuthentication(true);
        try {
            int status = client.executeMethod(postMethod);
            System.out.println(status + "\n" + postMethod.getResponseBodyAsString());
        } finally {
            postMethod.releaseConnection();
        }
    }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics