前端小伙伴总是问 中台今天发布没。。
就写了个 定时任务 每天 定时提醒他们
新手入行不久 。。。
you bug 希望大佬 指点
下面就是拿到 Jenkins 的各种信息了
我是写个定时任务 每天定点去 执行 /或者写个接口 配置到 Jenkins
就可以推送到 各个系统了
[Java] 纯文本查看 复制代码 public class JenkinsUtils {
private final static String SUFFIX="/api/json";
private final static String JOB="/job/";
private static String BASE_URL="";
private static String JENKINS_TOKEN = "";
private static String JENKINS_USERNAME = "";
public JenkinsUtils(String url,String token,String username){
BASE_URL = url;
JENKINS_TOKEN = token;
JENKINS_USERNAME = username;
}
public String 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);
}
} |