Jenkins REST API to get job and job console log

后端 未结 4 1425
日久生厌
日久生厌 2021-02-10 03:18

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 foll

相关标签:
4条回答
  • 2021-02-10 03:43

    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

    0 讨论(0)
  • 2021-02-10 03:59

    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

    0 讨论(0)
  • 2021-02-10 04:05

    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

    0 讨论(0)
  • 2021-02-10 04:07

    You can try to use Jenkins API to obtain the crumbs based on the authentication (user/pass or user/token).

    I'll paste below some code illustrating how to do that (it's powershell but the idea is the same and it's straight-forward to translate it to 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
    
    

    Source: https://octopus.com/blog/jenkins-rest-api

    0 讨论(0)
提交回复
热议问题