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();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值