1、获取最后一次构建的版本号
String BUILD_NUMBER_URL = "http://localhost:8080/job/应用名称/lastBuild/buildNumber";
//认证
HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth(jenkinsUsername, jenkinsPassword);
HttpEntity<MultiValueMap<String, String>> getNumberEntity = new HttpEntity<>(headers);
//获取最后一次构建的版本号
ResponseEntity<String> responseEntity = restTemplate.exchange(BUILD_NUMBER_URL, HttpMethod.POST, getNumberEntity, String.class);
if (HttpStatus.OK.equals(responseEntity.getStatusCode())) {
String result = responseEntity.getBody();
}
2、构建
String BUILD_URL = "http://localhost:8080/job/应用名称/buildWithParameters";
//认证
HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth(jenkinsUsername, jenkinsPassword);
//流水线参数
MultiValueMap<String, String> params = new LinkedMultiValueMap<>(16);
params.add("tag", "1.0");
params.add("ip", "127.0.0.1");
params.add("username", "root");
params.add("id", "1");
HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(params, headers);
//构建
restTemplate.exchange(BUILD_URL, HttpMethod.POST, entity, String.class);
3、获取流水线日志
String LOG_URL = "http://localhost:8080/job/应用名称/1/logText/progressiveText";
//认证
HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth(jenkinsUsername, jenkinsPassword);
HttpEntity<MultiValueMap<String, String>> logEntity = new HttpEntity<>(headers);
//获取流水线日志
ResponseEntity<String> responseEntity = restTemplate.exchange(LOG_URL, HttpMethod.POST, logEntity, String.class);
String result = responseEntity.getBody();