如何为詹金斯申请面包屑发行人

如何为詹金斯申请面包屑发行人,第1张

如何为詹金斯申请面包屑发行人

我也没有在文档中找到这个。此代码已针对较旧的Jenkins(1.466)进行了测试,但仍可以使用。

要发出面包屑,请使用

crumbIssuer

// left out: you need to authenticate with user & password -> sample belowHttpGet httpGet = new HttpGet(jenkinsUrl + "crumbIssuer/api/json");String crumbResponse = toString(httpclient, httpGet);CrumbJson crumbJson = new Gson().fromJson(crumbResponse, CrumbJson.class);

这会给你这样的回应

{"crumb":"fb171d526b9cc9e25afe80b356e12cb7","crumbRequestField":".crumb"}

其中包含您需要的两条信息

  1. 您需要传递面包屑的字段名称
  2. 面包屑本身

如果您现在想从詹金斯(Jenkins)那里获取东西,请将面包屑添加为标题。在下面的示例中,我获取了最新的构建结果。

HttpPost httpost = new HttpPost(jenkinsUrl + "rssLatest");httpost.addHeader(crumbJson.crumbRequestField, crumbJson.crumb);

这里是整体示例代码。我正在使用gson
2.2.4
解析响应,其余部分则使用Apache的httpclient
4.2.3

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 = "your-jenkins-host.com";        int port = 8080;        String usernName = "username";        String password = "passwort";        DefaultHttpClient httpclient = new DefaultHttpClient();        httpclient.getCredentialsProvider().setCredentials(     new AuthScope(host, port),      new UsernamePasswordCredentials(usernName, password));        String jenkinsUrl = protocol + "://" + host + ":" + port + "/jenkins/";        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); // 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 + "rssLatest"); 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;    }    private static String toString(DefaultHttpClient client,         HttpRequestbase request) throws Exception {        ResponseHandler<String> responseHandler = new BasicResponseHandler();        String responseBody = client.execute(request, responseHandler);        System.out.println(responseBody + "n");        return responseBody;    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5163746.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-18
下一篇 2022-11-18

发表评论

登录后才能评论

评论列表(0条)

保存