一.需求:
系统cicd发布时首页需要展示jenkins发布的版本和优化内容
二.思路:
1.jenkins创建用户和秘钥
2.找到对应构建任务信息的api
3.RestTemplate发起http请求
三.实现:
1.创建用户和token
2.查找jenkins API
创建 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发起请求
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");