一、基本配置
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