1. 程式人生 > >一個java創建,刪除,構建Jenkins等功能的JenkinsUtil工具類

一個java創建,刪除,構建Jenkins等功能的JenkinsUtil工具類

args eem sco .class vip any ica == serve

package com.vip.webpagetest.utils;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.InputStreamRequestEntity; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.RequestEntity; import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import static com.jayway.restassured.path.json.JsonPath.with; public class JenkinsUtil { private static final Logger logger = LoggerFactory.getLogger(JenkinsUtil.class
); static String jenkinsBaseUrl = ClassPathPropertiesUtils.getProperty("jenkins.server"); static String userName = ClassPathPropertiesUtils.getProperty("jenkins.userName"); static String apiToken = ClassPathPropertiesUtils.getProperty("jenkins.apiToken"); /** * 創建Jenkins Job * @param jobName * @throws Exception */ public static void creatJenkinsJob(String jobName) throws Exception{ if(isJenkinsJobExist(jobName)){ logger.info("已經存在job:"+jobName); }else{ HttpClient client = new HttpClient(); UsernamePasswordCredentials creds = new UsernamePasswordCredentials(userName,apiToken); client.getState().setCredentials(AuthScope.ANY,creds); client.getParams().setAuthenticationPreemptive(true); PostMethod post = new PostMethod(jenkinsBaseUrl+"/createItem?name="+jobName); post.setDoAuthentication(true); Resource resource = new ClassPathResource("config.xml"); InputStream fileInput = resource.getInputStream(); InputStreamRequestEntity requestEntity = new InputStreamRequestEntity(fileInput, "text/xml; charset=UTF-8"); post.setRequestEntity(requestEntity); int code = client.executeMethod(post); logger.info("成功創建job:"+jobName); } } /** * 查詢是否存在名為jobName的job * @param jobName * @return * @throws Exception */ public static boolean isJenkinsJobExist(String jobName) throws Exception{ HttpClient client = new HttpClient(); UsernamePasswordCredentials creds = new UsernamePasswordCredentials(userName,apiToken); client.getState().setCredentials(AuthScope.ANY,creds); client.getParams().setAuthenticationPreemptive(true); GetMethod get = new GetMethod(jenkinsBaseUrl+"/api/json"); get.setDoAuthentication(true); client.executeMethod(get); String result = get.getResponseBodyAsString(); List<String> jobList = with(result).getList("jobs.name"); for(String job:jobList){ if(jobName.equals(job)){ return true; } } return false; } /** * 刪除Jenkins Job * @param jobName * @throws Exception */ public static void deleteJenkinsJob(String jobName) throws Exception{ if(!isJenkinsJobExist(jobName)){ logger.info("不存在job:"+jobName); }else{ HttpClient client = new HttpClient(); UsernamePasswordCredentials creds = new UsernamePasswordCredentials(userName,apiToken); client.getState().setCredentials(AuthScope.ANY,creds); client.getParams().setAuthenticationPreemptive(true); PostMethod post = new PostMethod(jenkinsBaseUrl+"/job/"+jobName+"/doDelete"); post.setDoAuthentication(true); client.executeMethod(post); } } /** * 構建觸發Jenkins Job * @param jobName * @throws Exception */ public static boolean buildJenkinsJob(String jobName) throws Exception{ if(!isJenkinsJobExist(jobName)){ logger.info("不存在job:"+jobName); return false; }else{ HttpClient client = new HttpClient(); UsernamePasswordCredentials creds = new UsernamePasswordCredentials(userName,apiToken); client.getState().setCredentials(AuthScope.ANY,creds); client.getParams().setAuthenticationPreemptive(true); PostMethod post = new PostMethod(jenkinsBaseUrl+"/job/"+jobName+"/build"); post.setDoAuthentication(true); int code = client.executeMethod(post); if(code == 201){ logger.info("已開始構建job:"+jobName); return true; } } return false; } /** * 帶參數的構建 * @param jobName * @param paramsJson--{"parameter":{"name":"xxx","value":"xxx"}} * @return * @throws Exception */ public static boolean buildJenkinsJobWithParameters(String jobName,String paramsJson) throws Exception{ if(!isJenkinsJobExist(jobName)){ logger.info("不存在job:"+jobName); return false; }else{ HttpClient client = new HttpClient(); UsernamePasswordCredentials creds = new UsernamePasswordCredentials(userName,apiToken); client.getState().setCredentials(AuthScope.ANY,creds); client.getParams().setAuthenticationPreemptive(true); PostMethod post = new PostMethod(jenkinsBaseUrl+"/job/"+jobName+"/build"); RequestEntity entity = new StringRequestEntity(paramsJson,"application/json","UTF-8"); post.setRequestEntity(entity); post.setRequestHeader("Content-Type","application/json;charset=UTF-8"); client.executeMethod(post); return true; } } /** * 終止Jenkins Job構建 * @param jobName * @return * @throws Exception */ public static boolean stopJenkinsJob(String jobName) throws Exception{ if(!isJenkinsJobExist(jobName)){ logger.info("不存在job:"+jobName); return false; }else{ HttpClient client = new HttpClient(); UsernamePasswordCredentials creds = new UsernamePasswordCredentials(userName,apiToken); client.getState().setCredentials(AuthScope.ANY,creds); client.getParams().setAuthenticationPreemptive(true); PostMethod post = new PostMethod(jenkinsBaseUrl+"/job/"+jobName+"/api/json"); post.setDoAuthentication(true); client.executeMethod(post); String result = post.getResponseBodyAsString(); int buildNumber = with(result).get("lastBuild.number"); PostMethod stopJenkinsRequest = new PostMethod(jenkinsBaseUrl+"/job/"+jobName+"/"+buildNumber+"/stop"); client.executeMethod(stopJenkinsRequest); return true; } } public static void main(String[] args) throws Exception { creatJenkinsJob("yytest"); } }

一個java創建,刪除,構建Jenkins等功能的JenkinsUtil工具類