jck28 - 小柒 - jenkins api接口

一, Jenkins Api 简介

  • Jenkins 对外暴露的动作交付入口
  • 为外部程序提供入口,可以控制Jenkins
  • 支持协议 - Http
  • Api 接口支持用户名、密码认证
  • Jenkins Api支持的典型功能:运行Job、查看任务状态、返回任务编号…等

二, Jenkins Api 环境准备

2.1 环境准备

2.2 使用curl调用 获取所有Jobs接口

curl -u USERNAME:PASSWORD "JENKINSURL:PORT/api/json?pretty=true"

curl -u USERNAME:PASSWORD -g "JENKINSURL:PORT/api/json?pretty=true&tree=jobs[name,url]"

# 可以加上&tree=jobs[name,url]进行显示内容的展示

2.3 使用curl调用 获取所有构建队列

curl -u USERNAME:PASSWORD JENKINSURL:PORT/queue/api/json?pretty=true

2.4 使用curl调用 获取Job信息

# 获取任务所有信息
curl -u USERNAME:PASSWORD JENKINSURL:PORT/job/JOBNAME/api/json?pretty=true
# 获取最后一次构建信息
curl -u USERNAME:PASSWORD JENKINSURL:PORT/job/JOBNAME/lastBuild/api/json?pretty=true
# 获取最后稳定构建信息
curl -u USERNAME:PASSWORD JENKINSURL:PORT/job/JOBNAME/lastStableBuild/api/json?pretty=true
# 获取最后成功的构建
curl -u USERNAME:PASSWORD JENKINSURL:PORT/job/JOBNAME/lastSuccessfulBuild/api/json?pretty=true
# 获取最后完成的构建
curl -u USERNAME:PASSWORD JENKINSURL:PORT/job/JOBNAME/lastCompletedBuild/api/json?pretty=true
# 最近失败的构建
curl -u USERNAME:PASSWORD JENKINSURL:PORT/job/JOBNAME/lastFailedBuild/api/json?pretty=true

2.5 使用curl调用操作Job

获取crumb

  • 操作Job需要验证 crumb
    • 获取crumb使用
    • 使用token发起接口(不推荐)
# 方式1:获取crumb
username_password="USERNAME:PASSWORD"
jenkins_url="JENKINSURL:PORT"
cookie_file="$(mktemp)"
jenkins_crumb=$(curl -s -f -u "${username_password}" --cookie-jar "${cookie_file}" -s "${jenkins_url}/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,%22:%22,//crumb)")
# 方式2:使用token

操作job

# 获取job的config
curl -u USERNAME:PASSWORD -O JENKINSURL:PORT/job/JOBNAME/config.xml
# 创建job
curl -u USERNAME:PASSWORD "JENKINSURL:PORT/createItem?name=JOBNAME" --cookie "${cookie_file}" -X POST -H "${jenkins_crumb}" -H 'Content-Type:text/xml' --data-binary "@./config.xml"
# 复制job
curl -u USERNAME:PASSWORD "JENKINSURL:PORT/createItem?name=NEWJOBNAME&mode=copy&from=JOBNAME" --cookie "${cookie_file}" -X POST -H "${jenkins_crumb}"
# 修改job
curl -u USERNAME:PASSWORD "JENKINSURL:PORT/job/JOBNAME/config.xml" --cookie "${cookie_file}" -X POST -H "${jenkins_crumb}" -H 'Content-Type:text/xml' --data-binary "@./config.xml"
# 无参构建
curl -u USERNAME:PASSWORD "JENKINSURL:PORT/job/JOBNAME/build" --cookie "${cookie_file}" -X POST -H "${jenkins_crumb}" 
# 有参构建
curl -u USERNAME:PASSWORD "JENKINSURL:PORT/job/JOBNAME/buildWithParameters" --cookie "${cookie_file}" -X POST -H "${jenkins_crumb}" --data-urlencode json='{"key1":"value1","key2":"value2"}'
# 删除job
curl -u USERNAME:PASSWORD "JENKINSURL:PORT/job/JOBNAME/doDelete" --cookie "${cookie_file}" -X POST -H "${jenkins_crumb}"

操作指定构建的Job

# 查看最新构建任务编号
curl -u USERNAME:PASSWORD "JENKINSURL:PORT/job/JOBNAME/lastBuild/buildNumber"
# 查看某次构建控制台日志
curl -u USERNAME:PASSWORD "JENKINSURL:PORT/job/JOBNAME/ID/logText/progressiveText"
# 删除某次构建
curl -u USERNAME:PASSWORD "JENKINSURL:PORT/job/JOBNAME/ID/doDelete" -X POST --cookie "${cookie_file}" -H "${jenkins_crumb}"

三, Java Jenkins Api 常用接口操作

参考文档:Java调用Jenkins API获取任务构建归档文件_使用java实现构建jenkins的job,并拿到构建结果-CSDN博客

3.1 创建新项目,并导入依赖

    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.offbytwo.jenkins/jenkins-client -->
        <dependency>
            <groupId>com.offbytwo.jenkins</groupId>
            <artifactId>jenkins-client</artifactId>
            <version>0.3.8</version>
        </dependency>
    </dependencies>

3.2 连接jenkins

package com.ceshiren;

import com.offbytwo.jenkins.JenkinsServer;
import java.net.URI;
import java.net.URISyntaxException;

public class JenkinsConnect {
    public String jenkinsUrl = "http://192.168.43.68:5003";
    public String userName = "admin";
    public String password = "123456";
    private static JenkinsServer jenkinsServer = null ;

    public JenkinsServer getConnection(){
        if(jenkinsServer == null){
            try {
                jenkinsServer = new JenkinsServer(new URI(jenkinsUrl),userName,password);
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
        }
        return jenkinsServer;
    }
}

3.3 通过代码实现job操作

import com.ceshiren.JenkinsConnect;
import com.offbytwo.jenkins.model.Job;
import com.offbytwo.jenkins.model.JobWithDetails;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.util.Map;

public class JenkinsJobTest {
    JenkinsConnect jenkinsConnect = new JenkinsConnect();

//    获取所有的job列表
    @Test
    public void getAllJobs(){
        try {
            Map<String, Job> jobs = jenkinsConnect.getConnection().getJobs();
//            System.out.println(jobs);
            for(Job job : jobs.values()){
                System.out.println(job.getName());
                System.out.println();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//    获取单个job任务信息
    @Test
    public void getJob(){
        try {
            JobWithDetails createJob = jenkinsConnect.getConnection().getJob("create_job");
            System.out.println(createJob.getName());
            System.out.println(createJob.getUrl());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//    获取job的xml配置文件信息
    @Test
    public void getJobXml(){
        String jobXml = null;
        try {
            jobXml = jenkinsConnect.getConnection().getJobXml("create_job");
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(jobXml);
    }

//    新建job?????
    @Test
    public void createJob(){
        try {
            jenkinsConnect.getConnection().createJob("Java_create_job","src/main/resources/jobConfig.xml");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}