栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

高版本Jenkins 通过HTTP开启任务

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

高版本Jenkins 通过HTTP开启任务

下载Build Authorization Token Root Plugin插件 下载 Strict Crumb Issuer Plugin 插件

因为高版本Jenkins认为跨域不安全,直接把允许跨域给X掉了只能借助这个插件来访问API

修改安全配置

Jenkins Manager -> Configure Global Security

开启匿名读取访问

设置关闭 Session ID

这里切换成 Strict Crumb Issuer

具体如何开启项目任务看下Java代码吧(国外的一个老哥写的,直接搬轮子过来用)
简单的说下

package ;
import org.apache.http.auth.*;
import org.apache.http.client.*;
import org.apache.http.client.methods.*;
import org.apache.http.impl.client.*;

import com.google.gson.Gson;

public class JenkinsMonitor {

    public static void main(String[] args) throws Exception {

        String protocol = "http";
        String host = "10.128.22.132";
        int port = 4898;
        String usernName = "admin";
        String password = "xxx@123";

        DefaultHttpClient httpclient = new DefaultHttpClient();
        httpclient.getCredentialsProvider().setCredentials(
                new AuthScope(host, port),
                new UsernamePasswordCredentials(usernName, password));

        String jenkinsUrl = protocol + "://" + host + ":" + port;

        try {
            // get the crumb from Jenkins
            // do this only once per HTTP session
            // keep the crumb for every coming request
            System.out.println("... issue crumb");
            HttpGet httpGet = new HttpGet(jenkinsUrl + "/crumbIssuer/api/json");
            String crumbResponse= toString(httpclient, httpGet);
            CrumbJson crumbJson = new Gson()
                    .fromJson(crumbResponse, CrumbJson.class);
            System.err.println("接收登录返回?");
            System.err.println(crumbJson);
            // add the issued crumb to each request header
            // the header field name is also contained in the json response
            System.out.println("... issue rss of latest builds");
            HttpPost httpost = new HttpPost(jenkinsUrl + "/job/test_op/buildWithParameters?token=110a00fc92994e0b711d3370ee193ef4be&BRANCH=123"); //这里的URL根据需求改.我这里使用了Parameter插件了,所以传入一个参数
//            httpost.addHeader("token","110a00fc92994e0b711d3370ee193ef4be");
            httpost.addHeader(crumbJson.crumbRequestField, crumbJson.crumb);
            toString(httpclient, httpost);

        } finally {
            httpclient.getConnectionManager().shutdown();
        }

    }

    // helper construct to deserialize crumb json into

    public static class CrumbJson {
        public String crumb;
        public String crumbRequestField;

        @Override
        public String toString() {
            return "CrumbJson{" +
                    "crumb='" + crumb + ''' +
                    ", crumbRequestField='" + crumbRequestField + ''' +
                    '}';
        }
    }

    private static String toString(DefaultHttpClient client,
                                   HttpRequestBase request) throws Exception {
        ResponseHandler responseHandler = new BasicResponseHandler();
        String responseBody = client.execute(request, responseHandler);
        System.out.println(responseBody + "n");
        return responseBody;
    }
}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1028482.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号