一.需求:
系统cicd发布时首页需要展示jenkins发布的版本和优化内容
二.思路:
1.jenkins创建用户和秘钥
2.找到对应构建任务信息的api
3.RestTemplate发起http请求
三.实现:
1.创建用户和token
2.查找jenkins API文章来源:https://www.toymoban.com/news/detail-697267.html
创建 Job
POST http://localhost:8080/createItem/api/json?name=xxx
更新 Job
POST http://localhost:8080/job/{job_name}/config.xml/api/json
获取 Job
GET http://localhost:8080/job/{job_name}/api/json
获取 JobXml
GET http://localhost:8080/job/{job_name}/config.xml/api/json
创建 Build
POST http://localhost:8080/job/{job_name}/build/api/json
获取 QueueItem
GET http://localhost:8080/queue/item/17/api/json
获取 Build信息
GET http://localhost:8080/job/test/6/api/json
获取TXT日志
GET http://localhost:8080/job/test/{build_number}/logText/progressiveText/api/json
获取 Html 日志
GET http://localhost:8080/job/test/{build_number}/logText/progressiveHtml/api/json
3.java发起请求文章来源地址https://www.toymoban.com/news/detail-697267.html
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
requestHeaders.add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");
HttpEntity<HttpHeaders> entity = new HttpEntity<>(requestHeaders);
String url = "http://localhost:8080/job/finance-admin/lastSuccessfulBuild/api/json?pretty=true";
RestTemplate restTemplate = new RestTemplate();
restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor("deployer", "xxxxxxxxxxxxxxxxxxxxxxx"));
JSONObject result = restTemplate.exchange(url, HttpMethod.GET, entity, JSONObject.class)
.getBody();
List<LinkedHashMap<String, Object>> data = (List<LinkedHashMap<String, Object>>) result.get("actions");
List<LinkedHashMap<String, Object>> restList = (List<LinkedHashMap<String, Object>>) data.get(0).get("parameters");
到了这里,关于java获取jenkins发布版本信息的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!