Jenkins REST API 用于获取作业和作业控制台日志

问题描述 投票:0回答:5

如何使用 Jenkins REST API 获取作业的详细信息及其控制台输出

构建示例

控制台输出:

我正在使用以下命令来获取控制台日志的路径

回显 $JENKINS_HOME/jobs/$JOB_NAME/builds/${BUILD_NUMBER}/log

回显$BUILD_URL/consoleText

它将提供控制台日志的路径

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

但是如果我尝试使用 c#.net 从中获取数据,那么我会遇到异常

我正在使用以下代码来获取数据

 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;
    }

例外:

c# jenkins jenkins-plugins jenkins-pipeline
5个回答
20
投票

因此,对于使用

consoleFull
,我使用curl
得到非常

的输出

示例:

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

输出: 许多行都用 html 内容包裹着:

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

所以我的解决方案是使用:

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

您将获得相同的控制台日志输出,而无需 html、span 等内容


2
投票

我们可以通过上面提到的 URL 获取控制台日志 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())));

它对我有用,如果有任何疑问请联系我


1
投票

您可以尝试使用Jenkins API根据身份验证(用户/通行证或用户/令牌)获取面包屑。

我将在下面粘贴一些代码来说明如何做到这一点(它是 powershell,但想法是相同的,并且可以直接将其转换为 C#):


$user = 'user'
$pass = 'password'

# The header is the username and password concatenated together
$pair = "$($user):$($pass)"
# The combined credentials are converted to Base 64
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
# The base 64 credentials are then prefixed with "Basic"
$basicAuthValue = "Basic $encodedCreds"
# This is passed in the "Authorization" header
$Headers = @{
    Authorization = $basicAuthValue
}
# Make a request to get a crumb. This will be returned as JSON
$json = Invoke-WebRequest -Uri 'http://jenkinsserver/jenkins/crumbIssuer/api/json' -Headers $Headers
# Parse the JSON so we can get the value we need
$parsedJson = $json | ConvertFrom-Json
# See the value of the crumb
Write-Host "The Jenkins crumb is $($parsedJson.crumb)"
# Extract the crumb filed from the returned json, and assign it to the "Jenkins-Crumb" header
$BuildHeaders = @{
    "Jenkins-Crumb" = $parsedJson.crumb
    Authorization = $basicAuthValue
}
Invoke-WebRequest -Uri "http://jenkinsserver/jenkins/job/Run%20a%20script/build" -Headers $BuildHeaders -Method Post

来源:https://octopus.com/blog/jenkins-rest-api


0
投票

使脚本化客户端(例如 wget)调用需要的操作 授权(例如安排构建),使用 HTTP BASIC 身份验证以指定用户名和 API 令牌。

请参阅身份验证以及示例


0
投票

Python 示例在这里:

import requests

# create your own jenkins cred from jenkins setting page
USER = '***'
PASS = '***'
AUTH = (USER, PASS)

URL = "https://$JENKINS_URL/job/$JOB_NAME/$BUILD_ID/consoleText"

response = requests.get(URL, auth=AUTH)
open("my_log.txt", "wb").write(response.content)
© www.soinside.com 2019 - 2024. All rights reserved.