Java测试开发平台搭建(八) Jenkins

一、基本配置

1. 添加依赖

XML 复制代码
<dependency>
            <groupId>com.offbytwo.jenkins</groupId>
            <artifactId>jenkins-client</artifactId>
            <version>0.3.8</version>
        </dependency>

2. 常见类-JenkinsHttpClient

bash 复制代码
封装了调用JenkinsAPI的底层方法。
JenkinsHttpClient(URI uri, String username, String password)
get(String path)
getFile(URI path)
post(String path, boolean crumbFlag)
post(String path, D data, Class cls)
post_xml(String path, String xml_data, boolean crumbFlag)

3. 常用类-JenkinsServer

bash 复制代码
封装了调用JenkinsAPI的语义级别的方法。
JenkinsServer(JenkinsHttpConnection client)
getJob(String jobName)
createJob(String jobName, String jobXml, Boolean crumbFlag)
updateJob(String jobName, String jobXml, boolean crumbFlag)
getJobXml(String jobName)
deleteJob(FolderJob folder, String jobName, boolean crumbFlag)

4. 常用类-Job

bash 复制代码
Jenkins中job对应的实体类,有很多实用的语义级别的方法。
Job(String name, String url)
build(Job job)
build(Job job, Map<String, String> params)
getFileFromWorkspace(String fileName)
setClient(JenkinsHttpConnection client)

5. 获取job配置信息的方法

获取的xml做为模板使用,后面会讲到

  • 创建新job
  • 进入job配置
  • 将job/job名称/configure改为job/job名称/config.xml并回车

二、接口调用

1. controller

java 复制代码
    @Operation(summary = "jenkins更新接口")
    @PostMapping("updateJenkins")
    public ResultDto updateJenkins(@RequestBody UpdateJenkinsDto updateJenkinsDto) throws IOException, URISyntaxException {

        if (StringUtils.isEmpty(updateJenkinsDto.getJobName())){// 这里应该是通过用户获取id,暂时先这样写
            return ResultDto.fail("jobName不能为空");
        }
        log.info("updateJenkins" + JSONObject.toJSONString(updateJenkinsDto));
        JenkinsUtil.build(updateJenkinsDto.getJobName(),updateJenkinsDto.getUserId(),updateJenkinsDto.getRemark(),updateJenkinsDto.getTestCommand());
        return ResultDto.success("成功");
    }

2. Dto

java 复制代码
package com.itestmini.testplatformbackend.dto.user;

import com.itestmini.testplatformbackend.entity.BaseEntityNew;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

@Data
@Schema(description= "Jenkins更新类")
public class UpdateJenkinsDto extends BaseEntityNew {

    /**
     * 用户id
     */
    @Schema(description= "userId", required = true, example = "123")
    private String userId;

    /**
     * jobName
     */
    @Schema(description= "jobName", required = true, example = "test-1")
    private String jobName;

    /**
     * remark
     */
    @Schema(description= "remark", required = true, example = "测试备注")
    private String remark;

    /**
     * 邮箱
     */
    @Schema(description= "testCommand", required = true, example = "pwd")
    private String testCommand;



}

3. Jenkins util

java 复制代码
package com.itestmini.testplatformbackend.util;


import com.offbytwo.jenkins.JenkinsServer;
import com.offbytwo.jenkins.client.JenkinsHttpClient;
import com.offbytwo.jenkins.model.Job;
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;

/**
 * @author moso
 * @Date 2025/1/4 21:40
 */

public class JenkinsUtil {

    public static void build(String jobName, String userId, String remark, String testCommand) throws IOException, URISyntaxException {
        ClassPathResource classPathResource = new ClassPathResource("jenkinsDir/i_test_mini_jenkins_config.xml");
        InputStream inputStream = classPathResource.getInputStream();
        String jobConfigXml = FileUtil.getText(inputStream);
        String baseURI = "http://127.0.0.1:8080/";
        String userName = "admin";
        String password = "your password";

        JenkinsHttpClient jenkinsHttpClient = new JenkinsHttpClient(new URI(baseURI), userName, password);
        String jenkinsVersion = jenkinsHttpClient.getJenkinsVersion();
        System.out.println("jenkinsVersion == " + jenkinsVersion);
        JenkinsServer jenkinsServer = new JenkinsServer(jenkinsHttpClient);
        jenkinsServer.updateJob(jobName, jobConfigXml,true);
        Map<String, Job> jobMap = jenkinsServer.getJobs();
        Job job = jobMap.get(jobName);
        Map<String, String> map = new HashMap<>();
        map.put("userId", userId);
        map.put("remark", remark);
        map.put("testCommand", testCommand);
        job.build(map,true);
        System.out.println();

    }
}

4. 接口调用

执行结果

java 复制代码
 
https://gitee.com/moso520/test-platform-backend.git
 
34035bb
相关推荐
mask哥9 分钟前
详解flink SQL基础(四)
java·大数据·数据库·sql·微服务·flink
灰原喜欢柯南17 分钟前
Spring Boot 自动配置全流程深度解析
java·spring boot·后端
Code_Artist22 分钟前
[Java并发编程]4.阻塞队列
java·数据结构·后端
遇见火星1 小时前
如何在 Jenkins 中安装 Master 和 Slave 节点以优化 CI/CD 流程
servlet·ci/cd·jenkins
心月狐的流火号1 小时前
Java NIO Selector 源码分析
java
MrSYJ1 小时前
AuthenticationEntryPoint认证入口
java·spring cloud·架构
lssjzmn1 小时前
Java并发容器ArrayBlockingQueue与LinkedBlockingQueue对比PK
java·消息队列
用户98408905087242 小时前
Java基础之深拷贝浅拷贝-Integer
java
渣哥2 小时前
99%的人忽略了!Java Integer缓存池原来暗藏玄机
java
小蒜学长2 小时前
vue家教预约平台设计与实现(代码+数据库+LW)
java·数据库·vue.js·spring boot·后端