Jenkins REST API to get job and job console log

允我心安 提交于 2020-08-22 06:19:05

问题


How to get the details of the job along with it console output using Jenkins REST API

example of builds

console output:

I am using following commands to get the path of console log

echo $JENKINS_HOME/jobs/$JOB_NAME/builds/${BUILD_NUMBER}/log

echo $BUILD_URL/consoleText

It would provide the path to console log

http://localhost:8080/job/Echo/25//consoleText

but if i try to get the data from it using c#.net it would through me a exception

I am using following code to get the data

 public string Download_Contents(string URI)
    {
        string Data = string.Empty;
        try
        {
            using (var wc = new System.Net.WebClient())
                Data = wc.DownloadString(URI);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return Data;
    }

Exception:


回答1:


So for using the consoleFull i'm getting very dirty output using curl

example:

curl -s -S  -u "user":"password" "http://jenkins.domain.com/job/my_job_name/1077/consoleFull"

output: many lines wrapped with html stuff:

 <span class="timestamp"><b>09:04:32</b> </span><span style="color: #00CD00;">ok:</span>

so my solution is to use:

curl -s -S  -u "user":"password" "http://jenkins.domain.com/job/my_job_name/1077/logText/progressiveText?start=0"

and you will get the same console log output without the html,span stuff




回答2:


To make scripted clients (such as wget) invoke operations that require authorization (such as scheduling a build), use HTTP BASIC authentication to specify the user name and the API token.

See Authentication with samples




回答3:


we can get console log with Above URL mentioned http://localhost:8080/job/Echo/25//consoleText

URL urls = new URL("http://localhost:8080/job/Echo/25//consoleText"); 
HttpURLConnection connection = (HttpURLConnection) urls.openConnection(); 
connection.setDoOutput(true); 
//connection.setRequestProperty("User-Agent", "Mozilla/5.0");
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.29 Safari/537.36");

System.setProperty("http.agent", "Chrome");
connection.setInstanceFollowRedirects(false); 
connection.setRequestMethod("GET"); 
connection.setRequestProperty("Content-Type", "application/json");

// Convert to a JSON object to print data
/*HttpServletRequest request;*/
BufferedReader br = new BufferedReader(new InputStreamReader(
        (connection.getInputStream())));

it worked for me if any queries please ping me



来源:https://stackoverflow.com/questions/45025231/jenkins-rest-api-to-get-job-and-job-console-log

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!