吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1499|回复: 7
收起左侧

[Java 原创] JenkinsUtil 获取Jenkins的信息

[复制链接]
我是白小白 发表于 2023-1-14 21:51



前端小伙伴总是问 中台今天发布没。。

就写了个 定时任务 每天 定时提醒他们

新手入行不久 。。。
you bug 希望大佬 指点


下面就是拿到 Jenkins 的各种信息了

我是写个定时任务 每天定点去 执行 /或者写个接口 配置到 Jenkins

就可以推送到 各个系统了




[Java] 纯文本查看 复制代码
public class JenkinsUtils {
    private final  static String SUFFIX="/api/json";
    private final  static String JOB="/job/";

    private static String BASE_URL="";
    private static String JENKINS_TOKEN = "";
    private static String JENKINS_USERNAME = "";


    public  JenkinsUtils(String url,String token,String username){
        BASE_URL = url;
        JENKINS_TOKEN = token;
        JENKINS_USERNAME = username;
    }



    public  String customHttpMsg(String url, HttpRequest httpRequest) throws Exception {
        URI uri = new URI(url);

        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(new AuthScope(uri.getHost(), uri.getPort()), new UsernamePasswordCredentials(JENKINS_USERNAME, JENKINS_TOKEN));
        AuthCache authCache = (AuthCache) new BasicAuthCache();
        HttpHost host = new HttpHost(uri.getHost(), uri.getPort());
        BasicScheme basicScheme = new BasicScheme();
        authCache.put(host,basicScheme);
        
        try (CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).build()) {
            HttpClientContext httpClientContext = HttpClientContext.create();
            httpClientContext.setAuthCache(authCache);

            CloseableHttpResponse response = httpClient.execute(host, httpRequest, httpClientContext);
            return EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
        }
    }

    public    JSONArray getJobs()  {
        String JENKINS_URL =BASE_URL+SUFFIX;
        HttpPost httpPost = new HttpPost(JENKINS_URL);
        String res = null;
        try {
            res = customHttpMsg(JENKINS_URL, httpPost);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        JSONObject parse = (JSONObject) JSONObject.parse(res);
        return parse.getJSONArray("jobs");
    }

    public   JSONArray getNumbers(String job) {
        String JENKINS_URL =BASE_URL+JOB+job+SUFFIX;
        HttpPost httpPost = new HttpPost(JENKINS_URL);
        String res = null;
        try {
            res = customHttpMsg(JENKINS_URL, httpPost);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        JSONObject parse = (JSONObject) JSONObject.parse(res);
        return parse.getJSONArray("builds");
    }

    public   JSONObject getBuild(String job,String build)  {
        String JENKINS_URL =BASE_URL+JOB+job+"/"+build+SUFFIX;
        HttpPost httpPost = new HttpPost(JENKINS_URL);
        String res = null;
        try {
            res = customHttpMsg(JENKINS_URL, httpPost);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return  (JSONObject) JSONObject.parse(res);
    }


}

免费评分

参与人数 1吾爱币 +5 热心值 +1 收起 理由
wushaominkk + 5 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| 我是白小白 发表于 2023-1-14 21:53
后续 可以自己加上 发任务 各种接口 啥的
markdik31 发表于 2023-1-15 08:49
debug_cat 发表于 2023-1-15 11:16
邓振振 发表于 2023-2-7 17:32
1.jpg

2.jpg

通过githooks 来实现的jenkins自动发布,发布失败自动预警
sknbs 发表于 2023-2-8 21:41
这个是jenkins的插件吗?
kuschzzp 发表于 2023-2-9 20:28
这接口是jenkins的接口吗?
newyuxing 发表于 2023-3-14 17:46
代码托管平台不是有一个推送机制嘛 比如设置代码提交了 就自动给jenkins发消息 然后Jenkins 自动拉取代码 自动发布
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-6-5 14:44

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表