• SpringBoot|封装接口实现自动创建job并且自动构建功能


    一、前言

      在我们做测试平台的时候,需要把自动创建job以及构建这种功能封装成一个接口给外部调用,下面是简单的实现方案

    二、具体步骤

      1、熟悉代码调用步骤

        传送门

      2、改造JenkinsUtil工具类

    package com.example.demo.util;
    
    import com.offbytwo.jenkins.JenkinsServer;
    import com.offbytwo.jenkins.client.JenkinsHttpClient;
    import com.offbytwo.jenkins.model.Job;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.core.io.ClassPathResource;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URI;
    import java.net.URISyntaxException;
    import java.util.HashMap;
    import java.util.Map;
    
    public class JenkinsUtil {
    
    
        private static final Logger logger = LoggerFactory.getLogger(JenkinsUtil.class);
    
    
        public static void build( String jobName,String userId)throws URISyntaxException, IOException {
    
            ClassPathResource classPathResource = new ClassPathResource("jenkinsConfigDir/weifang_test_jenkins_show.xml");
            InputStream inputStream = classPathResource.getInputStream();
            String jobConfigXml = FileUtil.getText(inputStream);
    
            String baseUrl = "http://localhost:8080/jenkins/";
            String userName = "durant";
            String password = "123456";
    
            JenkinsHttpClient jenkinsHttpClient = new JenkinsHttpClient(new URI(baseUrl),userName,password);
            String JenkinsVersion = jenkinsHttpClient.getJenkinsVersion();
            logger.info(JenkinsVersion);
            JenkinsServer jenkinsServer = new JenkinsServer(jenkinsHttpClient);
            jenkinsServer.createJob(jobName,jobConfigXml,true);
    
            Map<String, Job> jobMap= jenkinsServer.getJobs();
            Job job = jobMap.get(jobName);
            Map<String,String> map = new HashMap<>();
            map.put("userId",userId);
    
            job.build(map,true);
    
    
    
        }
    }

      3、定义创建并且构建job的DTO类

    package com.example.demo.dto;
    
    
    import io.swagger.annotations.ApiModel;
    import io.swagger.annotations.ApiModelProperty;
    import lombok.Data;
    
    @ApiModel(value = "jenkins构建类",description = "请求类")
    
    @Data
    public class BuildDto {
    
        @ApiModelProperty(value = "用户id",example = "durant",required = true)
        private String userId;
    
        @ApiModelProperty(value = "job名称",example = "durant",required = true)
        private String jobName;
        
    }

      4、在Controller层

        @PostMapping("build")
        @ApiOperation("调用jenkins构建job")
        public ResultDto build(@RequestBody BuildDto buildDto) throws IOException, URISyntaxException {
    
            log.info("调用jenkins构建job 入参" + JSONObject.toJSONString(buildDto));
            JenkinsUtil.build(buildDto.getJobName(),buildDto.getUserId());
            return ResultDto.success("成功");
        }

      5、postman调用

      6、服务日志

       7、实际效果

     三、总结

      在测试平台开发的道路上还任重道远,需要学习的知识还很多

    知道、想到、做到、得到
  • 相关阅读:
    转载cocos3.17cmake打包
    cocos-lua3.17 Lua tableView工具类
    cocso引擎整体流程
    letCode-1
    letCode-2
    图像语义分割的前世今生
    简要介绍弱监督学*
    win10下乌龟git安装和使用
    STM32F4+Wi-Fi+EDP 向 OneNet 上传数据
    GoogLeNet学习
  • 原文地址:https://www.cnblogs.com/Durant0420/p/15144384.html
Copyright © 2020-2023  润新知