当前位置: 首页 > article >正文

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

一、基本配置

1. 添加依赖

<dependency>
            <groupId>com.offbytwo.jenkins</groupId>
            <artifactId>jenkins-client</artifactId>
            <version>0.3.8</version>
        </dependency>

2. 常见类-JenkinsHttpClient

封装了调用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

封装了调用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

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

    @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

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

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. 接口调用

执行结果

 
https://gitee.com/moso520/test-platform-backend.git
 
34035bb


http://www.kler.cn/a/470549.html

相关文章:

  • 概率基本概念 --- 离散型随机变量实例
  • Elasticsearch:优化的标量量化 - 更好的二进制量化
  • [文献精汇]使用PyCaret预测 Apple 股价
  • CSS 学习之正确看待 CSS 世界里的 margin 合并
  • 基于STM32的自动水满报警系统设计
  • 在Spring Boot项目中使用Zookeeper和Curator实现高效、可靠的分布式锁
  • 【Linux】RPMSG通讯协议介绍
  • w140体育馆使用预约平台的设计与实现
  • CV-MLLM经典论文解读|OneLLM: One Framework to Align All Modalities with Language
  • netty系列(四)websocket client和server
  • 用CRD定义未来:解锁机器学习平台的无限可能
  • ollama+FastAPI部署后端大模型调用接口
  • 修改 页面 滚动条样式
  • 【React】漫游式引导
  • java开发springoot
  • 【苏德矿高等数学】第1讲:有界函数、无界函数、复合函数
  • DeepSpeed是什么,怎样使用
  • 个性化电影推荐系统|Java|SSM|JSP|
  • 【形式篇】年终总结怎么写:PPT如何将内容更好地表现出来
  • 5.C语言流程控制语句详解:if、switch、while、for、break、continue等
  • Vue Router v3.x 迁移到 v4.x(两者的区别)【路由篇】
  • 【模电】功率放大电路总结
  • 【C++数据结构——查找】二分查找(头歌实践教学平台习题)【合集】
  • 【渗透测试术语总结】
  • Zero to JupyterHub with Kubernetes 下篇 - Jupyterhub on k8s
  • 人工智能的发展领域之GPU加速计算的应用概述、架构介绍与教学过程