簡體   English   中英

通過Java在Jenkins中編輯構建描述

[英]Editing Build Description in Jenkins through Java

我正在嘗試編輯詹金斯的職位描述。 以前我通過bash做'curl --data-urlencode'description = ABCDEFGH'--data-urlencode“Submit = Submit”“ https:// Login / APItoken @ jenkinsURL / job / JobName / Build / submitDescription ”。 現在,我想通過Java做同樣的事情。 這就是我到目前為止所擁有的。 一如既往,在我看來,“它應該有效,但它不會”:

import java.io.*;
import java.util.*;
import java.net.URL;
import java.net.URLEncoder;
import java.net.HttpURLConnection;


public class JenkinsDesUpdate {

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

        URL url = new URL("https://Login:APItoken@jenkinsURL/job/JobName/Build/configSubmit");


         Map <String,Object> params = new LinkedHashMap<>();
            params.put("description", "THIS IS A NEW DESCRIPTION");
            params.put(DisplayName","THIS IS A NEW DISPLAY NAME");
            StringBuilder postData = new StringBuilder();

            for (Map.Entry <String,Object> param : params.entrySet()) {
                if (postData.length() != 0) postData.append('&');
                postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
                postData.append('=');
                postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8")); 
            }


            byte[] postDataBytes = postData.toString().getBytes("UTF-8");

            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
            conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
            conn.setDoOutput(true);
            conn.getOutputStream().write(postDataBytes);

            Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));

            for (int c; (c = in.read()) >= 0;)
                System.out.print((char)c);

    }

}

好的,讓我聽聽你的想法。 我得到的是一個網站的HTML:

function showTranslationDialog() {
  if(!translation.launchDialog)
    loadScript("address/plugin/translation/dialog.js");
  else
    translation.launchDialog();
  return false; 
}</script></body></html>

如果我的網址是“... / Build”或

服務器返回HTTP響應代碼:403

當我的URL在代碼中的“... / Build / configSubmit”時如上所示。

提前致謝!

我建議使用jenkins客戶端sdk來進行交互。

Java - https://github.com/jenkinsci/java-client-api

Python - https://pypi.python.org/pypi/jenkinsapi

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM