pom.xml

    <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>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.25</version>
        </dependency>

    </dependencies>

JenkinsApi.java

import com.offbytwo.jenkins.JenkinsServer;

public class JenkinsApi {
    private JenkinsServer jenkinsServer;

    JenkinsApi() {
        jenkinsServer = JenkinsConnect.connection();
    }
}

JenkinsConnect.java

import com.offbytwo.jenkins.JenkinsServer;
import com.offbytwo.jenkins.client.JenkinsHttpClient;

import java.net.URI;
import java.net.URISyntaxException;

public class JenkinsConnect {
    private JenkinsConnect(){}

    static final String JENKINS_URL = "http://11.20.13.16:8081/";
    static final String JENKINS_USER = "admin";
    static final String JENKINS_PASSWORD = "admin";

    public static JenkinsHttpClient getClient() {
        JenkinsHttpClient jenkinsHttpClient = null;

        try {
            jenkinsHttpClient = new JenkinsHttpClient(new URI(JENKINS_URL),JENKINS_USER,JENKINS_PASSWORD);
        }catch (URISyntaxException e) {
            e.printStackTrace();
        }
        return jenkinsHttpClient;
    }

    public static JenkinsServer connection() {
        JenkinsServer jenkinsServer = null;
        try {
            jenkinsServer = new JenkinsServer(new URI(JENKINS_URL),JENKINS_USER,JENKINS_PASSWORD);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        return jenkinsServer;
    }

    public static void main(String[] args) {

    }
}

JobApi.java

import com.offbytwo.jenkins.JenkinsServer;
import com.offbytwo.jenkins.client.JenkinsHttpClient;
import com.offbytwo.jenkins.model.Job;
import com.offbytwo.jenkins.model.JobWithDetails;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class JobApi {

    private JenkinsServer jenkinsServer;
    private JenkinsHttpClient jenkinsHttpClient;

    JobApi() {
        JenkinsApi jenkinsApi = new JenkinsApi();
        jenkinsServer = JenkinsConnect.connection();
        jenkinsHttpClient = JenkinsConnect.getClient();
    }

    public void getJob() {
        try {
            JobWithDetails job = jenkinsServer.getJob("kobe-goods");
            System.out.println(job.getName());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void getJobList() {
        try {
            Map<String, Job> jobs = jenkinsServer.getJobs();
            for (Job job : jobs.values()) {
                // 拿到所有的kobe项目
                if (job.getName().startsWith("kobe")) {
//                    System.out.println(job.getName());
                    // 参数化构建
                    Map<String,String> param = new HashMap<>();
                    // 参数化构建需要指定key和value,key就是jenkins里面新建job时候自己定义的名字,value就是对应的值了
                    param.put("BRANCH","origin/release/2.6.2");
                    jenkinsServer.getJob(job.getName()).build(param);
                }
            }
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Main.java

public class Main {
    public static void main(String[] args) {

        JobApi jobApi = new JobApi();
        jobApi.getJob();

        jobApi.getJobList();
    }
}

这个脚本主要是用来操作jenkins批量参数化构建所有的kobe项目,其他操作再慢慢研究

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐