【问题标题】:Jenkins REST API to get job and job console logJenkins REST API 获取作业和作业控制台日志
【发布时间】:2017-07-11 04:09:57
【问题描述】:

如何使用 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


【解决方案1】:

所以对于使用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 的东西

【讨论】:

  • Dsaydon.. 该解决方案对我有用。但是我试图不在请求中发送内部版本号,尝试使用 lastbuild 选项但没有成功。这里有什么建议吗?
  • 您可能需要通过几个步骤来完成:1) 等待最后一个构建完成 2) 获取最后一个构建的内部版本号 3) 获取控制台日志。
  • 我发现在您开始工作时获取内部版本号,然后将内部版本号增加 1。这对我有用。
【解决方案2】:

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

参见Authentication 示例

【讨论】:

    【解决方案3】:

    我们可以通过上面提到的 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())));
    

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

    【讨论】:

      【解决方案4】:

      您可以尝试使用 Jenkins API 根据身份验证(用户/通行证或用户/令牌)获取 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
      
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-27
        • 1970-01-01
        • 2019-09-10
        • 1970-01-01
        • 1970-01-01
        • 2015-10-09
        • 2020-09-02
        • 1970-01-01
        相关资源
        最近更新 更多