java驱动jenkins构建job
java驱动jenkins构建job配置完成jenkins工程后,在jenkins可视化页面可以直接构建job,当然也可以通过java驱动jenkins接口,构建job。
·
java驱动jenkins构建job
配置完成jenkins工程后,在jenkins可视化页面可以直接构建job,当然也可以通过java驱动jenkins接口,构建job。
package jenkinsApi;
import java.io.IOException;
import java.net.URI;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class Job {
//jenkins登录账号
public static String username = "XXXX";
//jenkins登录密码
public static String password = "XXXXXXXXX";
//jenkins登录url
public static String jenkinsUrl = "http://ip:port/jenkins";
public static String buildToken = "buildToken";
public static void main(String [] args) throws ClientProtocolException, IOException{
Job job = new Job();
//job.buildJob("YourJobName",true,null);
//job.buildJob("YourJobName",true,"param1=value1¶m2=value2¶m3=value3");
job.buildJob("YourJobName", false , "xxxx");
}
/*
* 调用jenkins接口,构建job
* 1、jobName:需要构建的jenkins工程的名字
* 2、buildJobParams:构建jenkins工程时需要的参数
* 3、isNeedParams:jenkins工程的类型,如果jenkins工程带参数,isNeedParams=ture(isNeedParams=true时,buildJobParams生效,参数格式:key1=value1&key2=value2...,不填写参数时jenkins使用默认参数)
* 如果jenkins工程不带参数,isNeedParams=false(isNeedParams=false时,buildJobParams不生效,可以随便填写)
*/
public void buildJob(String jobName ,Boolean isNeedParams ,String buildJobParams) throws ClientProtocolException, IOException{
//jenkins构建job的url
String jenkinsBuildUrl = jenkinsUrl + "/job/" + jobName + "/build";
if(isNeedParams == true){
jenkinsBuildUrl = jenkinsUrl + "/job/" + jobName + "/buildWithParameters" + "?" + buildJobParams;
}
URI uri = URI.create(jenkinsBuildUrl);
HttpHost host = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(uri.getHost(), uri.getPort()), new UsernamePasswordCredentials(username, password));
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(host, basicAuth);
CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
HttpPost httpPost = new HttpPost(uri);
// Add AuthCache to the execution context
HttpClientContext localContext = HttpClientContext.create();
localContext.setAuthCache(authCache);
HttpResponse response = httpClient.execute(host, httpPost, localContext);
EntityUtils.toString(response.getEntity());
}
}
注:本代码只涉及构建job,参考了大牛的部分代码
更多推荐
已为社区贡献2条内容
所有评论(0)