JenkinsUtil 获取Jenkins的信息
前端小伙伴总是问 中台今天发布没。。
就写了个 定时任务 每天 定时提醒他们
新手入行不久 。。。
you bug 希望大佬 指点
下面就是拿到 Jenkins 的各种信息了
我是写个定时任务 每天定点去 执行 /或者写个接口 配置到 Jenkins
就可以推送到 各个系统了
public class JenkinsUtils {
private finalstatic String SUFFIX="/api/json";
private finalstatic String JOB="/job/";
private static String BASE_URL="";
private static String JENKINS_TOKEN = "";
private static String JENKINS_USERNAME = "";
publicJenkinsUtils(String url,String token,String username){
BASE_URL = url;
JENKINS_TOKEN = token;
JENKINS_USERNAME = username;
}
publicString customHttpMsg(String url, HttpRequest httpRequest) throws Exception {
URI uri = new URI(url);
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(uri.getHost(), uri.getPort()), new UsernamePasswordCredentials(JENKINS_USERNAME, JENKINS_TOKEN));
AuthCache authCache = (AuthCache) new BasicAuthCache();
HttpHost host = new HttpHost(uri.getHost(), uri.getPort());
BasicScheme basicScheme = new BasicScheme();
authCache.put(host,basicScheme);
try (CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).build()) {
HttpClientContext httpClientContext = HttpClientContext.create();
httpClientContext.setAuthCache(authCache);
CloseableHttpResponse response = httpClient.execute(host, httpRequest, httpClientContext);
return EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
}
}
public JSONArray getJobs(){
String JENKINS_URL =BASE_URL+SUFFIX;
HttpPost httpPost = new HttpPost(JENKINS_URL);
String res = null;
try {
res = customHttpMsg(JENKINS_URL, httpPost);
} catch (Exception e) {
throw new RuntimeException(e);
}
JSONObject parse = (JSONObject) JSONObject.parse(res);
return parse.getJSONArray("jobs");
}
public JSONArray getNumbers(String job) {
String JENKINS_URL =BASE_URL+JOB+job+SUFFIX;
HttpPost httpPost = new HttpPost(JENKINS_URL);
String res = null;
try {
res = customHttpMsg(JENKINS_URL, httpPost);
} catch (Exception e) {
throw new RuntimeException(e);
}
JSONObject parse = (JSONObject) JSONObject.parse(res);
return parse.getJSONArray("builds");
}
public JSONObject getBuild(String job,String build){
String JENKINS_URL =BASE_URL+JOB+job+"/"+build+SUFFIX;
HttpPost httpPost = new HttpPost(JENKINS_URL);
String res = null;
try {
res = customHttpMsg(JENKINS_URL, httpPost);
} catch (Exception e) {
throw new RuntimeException(e);
}
return(JSONObject) JSONObject.parse(res);
}
} 后续 可以自己加上 发任务 各种接口 啥的 谢谢大神的知识分享,感谢 好东西,回头试试{:1_918:}
通过githooks 来实现的jenkins自动发布,发布失败自动预警 这个是jenkins的插件吗? 这接口是jenkins的接口吗? 代码托管平台不是有一个推送机制嘛 比如设置代码提交了 就自动给jenkins发消息 然后Jenkins 自动拉取代码 自动发布
页:
[1]