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
相关推荐
Cikiss5 小时前
Servlet详解
servlet
快乐非自愿5 小时前
一文解秘Rust如何与Java互操作
java·开发语言·rust
小万编程5 小时前
基于SpringBoot+Vue毕业设计选题管理系统(高质量源码,提供文档,免费部署到本地)
java·vue.js·spring boot·计算机毕业设计·java毕业设计·web毕业设计
m0_748235075 小时前
使用rustDesk搭建私有远程桌面
java
快乐是5 小时前
发票打印更方便
java
文浩(楠搏万)5 小时前
Java内存管理:不可达对象分析与内存泄漏优化技巧 Eclipse Memory Analyzer
java·开发语言·缓存·eclipse·内存泄漏·不可达对象·对象分析
圆蛤镇程序猿5 小时前
【什么是MVCC?】
java·数据库·oracle
m0_748256785 小时前
【SQL】掌握SQL查询技巧:数据分组与排序
java·jvm·sql
Damon撇嘴笑5 小时前
Cause: java.sql.SQLException: sql injection violation, comment not allow异常问题处理
java·数据库·sql
孟秋与你5 小时前
【redisson】redisson分布式锁原理分析
java·分布式