Java 对接Jenkins API操作(不需要勾选允许站点跨域访问)

最先的是jenkins的跨域问题 如果是一般学习直接关闭  在公司Java对接jenkins的话 还是不关闭

Java 对接Jenkins API操作(不需要勾选允许站点跨域访问)_第1张图片

 Java 对接Jenkins API操作(不需要勾选允许站点跨域访问)_第2张图片

 需要的maven 依赖


			com.offbytwo.jenkins
			jenkins-client
			0.3.8
		

JenkinsUtil 不用勾选跨域也可以访问

public class JenkinsUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(JenkinsUtils.class);

    private static String job_class="hudson.model.FreeStyleProject";
    private static String folder_class= "com.cloudbees.hudson.plugins.folder.Folder";
    public static JenkinsServer getJenkinsServer(String userName, String password, String jenkinsUrl) {
        JenkinsHttpClient jenkinsHttpClient=null;
        JenkinsServer jenkinsServer=null;
        try{
            jenkinsHttpClient = new JenkinsHttpClient(new URI(jenkinsUrl), userName, password);
            jenkinsServer = new JenkinsServer(jenkinsHttpClient);
        }catch (URISyntaxException ex){
            LOGGER.info("JenkinsUtils :{}",ex.getMessage());
            throw new ResourceCenterException(ResponseCodeEnum.SYSTEM_ERROR_MESSAGE);
        }
        return jenkinsServer;
    }

    ;


    public static Integer buildWithParams(HashMap params,String jobName, JenkinsServer jenkinsServer) {
   
        JobWithDetails job=null;
        int nextBuildNumber =0;
     try{
         job = jenkinsServer.getJob(jobName); /*根据名称获取job*/
         if(null==job){
             LOGGER.info("jenkins 不存在该Job :{}",jobName);
             throw  new ResourceCenterException(ResponseCodeEnum.JOB_NOT_EXIST);
         }
         /*获取下一次构建的构建编号,可以用于在触发构建前,先记录构建编号。在后续获取指定编号的构建结果*/
         //这里需要 一个插件 Build With Parameters Plugin
         nextBuildNumber=job.getNextBuildNumber();
         job.build(params,true);
     }catch (IOException ex){
         LOGGER.info("JenkinsUtils :{}",ex.getMessage());
         throw new ResourceCenterException(ResponseCodeEnum.SYSTEM_ERROR_MESSAGE);
     }
        //获取结果  如果为null表示正在构建中 SUCCESS  表示构建成功
        return nextBuildNumber;
    }

    ;

    /**
     * 获取job 某一个构建的日志
     * @param number
     * @param jobName
     * @param jenkinsServer
     * @return
     * @throws Exception
     */
    public static String getBuildLogPrint(Integer number, String jobName, JenkinsServer jenkinsServer) {

        BuildWithDetails buildWithDetails=getBuildDetails(number,jobName,jenkinsServer);
        String log =null;
       try{
           log=buildWithDetails.getConsoleOutputText();
       }catch (IOException ex){
           LOGGER.info("JenkinsUtils :{}",ex.getMessage());
           throw new ResourceCenterException(ResponseCodeEnum.SYSTEM_ERROR_MESSAGE);
       }
        return log;

    }

    /**
     * 获取job 某一个构建的结果 :成功,失败,取消等状态
     * @param number
     * @param jobName
     * @param jenkinsServer
     * @return
     * @throws Exception
     */
    public static String getBuildLogResult(Integer number, String jobName, JenkinsServer jenkinsServer) throws Exception {

        BuildWithDetails buildWithDetails=getBuildDetails(number,jobName,jenkinsServer);
        BuildResult result = buildWithDetails.getResult();
        return result.name();

    }

    private static BuildWithDetails getBuildDetails(Integer number, String jobName, JenkinsServer jenkinsServer){
        JobWithDetails job = null;
        Build build=null;
         BuildWithDetails buildWithDetails=null;
        try{
            job=jenkinsServer.getJob(jobName); /*根据jobName名称获取job*/
            build = job.getBuildByNumber(number);
            buildWithDetails = build.details();
         }catch (Exception e){
            LOGGER.info("JenkinsUtils :{}",e.getMessage());
            throw new ResourceCenterException(ResponseCodeEnum.SYSTEM_ERROR_MESSAGE);
        }
        return buildWithDetails;
    }


    //暂时解析一层文件夹
    public  static String  getJobFullName(JenkinsServer jenkinsServer,String jobName){
        String jobFullName="";
        try {
            Map jobs1 = jenkinsServer.getJobs();
            if(null!=jobs1.get(jobName)){
                return  jobName;
            }
            Set params = jobs1.keySet();
            for (String tempKey: params) {
                jobFullName=tempKey;
                JobWithDetails job = jenkinsServer.getJob(tempKey);
                if(!folder_class.equals(job.get_class())){
                    continue;
                }
                Optional folderJob = jenkinsServer.getFolderJob(job);
                FolderJob folderJob1 = folderJob.get();
                Map jobs = folderJob1.getJobs();
                Job targetJob = jobs.get(jobName);
                if(null!=targetJob){
                    jobFullName=jobFullName+"/"+jobName;
                    return jobFullName;
                }
            }


        }catch (IOException ex){
            LOGGER.info("JenkinsUtils :{}",ex.getMessage());
            throw new ResourceCenterException(ResponseCodeEnum.SYSTEM_ERROR_MESSAGE);
        }
        return jobFullName;

    }
}

为什么Java对接jenkins 不需要勾选跨域访问也可以:

job.build(params,true);

 这个方法是带参数构建job true表示需要跨域访问  所以在代码底层为true时 会去 

http://JENKINS_URL/crumbIssuer/api/xml

 获取crumb 然后放进header里面 就可以了

Java 对接Jenkins API操作(不需要勾选允许站点跨域访问)_第3张图片

 

你可能感兴趣的:(Jenkins,#,Java基础)