如何使用Jenkins REST API获取作业的详细信息以及控制台输出
构建示例
控制台输出:
我使用以下命令获取控制台日志的路径
echo $ JENKINS_HOME / jobs / $ JOB_NAME / builds / $ {BUILD_NUMBER} / log
echo $ 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;
}
例外:
答案 0 :(得分:6)
因此使用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 stuff
答案 1 :(得分:0)
使脚本化客户端(例如wget)调用需要的操作 授权(例如调度构建),使用HTTP BASIC 用于指定用户名和API令牌的身份验证。
请参阅带有示例的Authentication
答案 2 :(得分:0)
我们可以获得带有上述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())));
如果有任何疑问,请为我ping通
答案 3 :(得分:0)
您可以尝试使用 Jenkins API 获取基于身份验证(user/pass 或 user/token)的 crumbs。
我将在下面粘贴一些代码来说明如何做到这一点(它是 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