Jenkins REST API to get job and job console log

后端 未结 4 1419
日久生厌
日久生厌 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 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

提交回复
热议问题