Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins REST API to get job and job console log

How to get the details of the job along with it console output using Jenkins REST API

example of builds

Builds

console output:

console Output for the build

I am using following commands to get the path of console log

echo $JENKINS_HOME/jobs/$JOB_NAME/builds/${BUILD_NUMBER}/log

echo $BUILD_URL/consoleText

It would provide the path to console log

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

but if i try to get the data from it using c#.net it would through me a exception

I am using following code to get the data

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

Exception:

Exception

like image 433
Sandeep Dvs Avatar asked Jul 11 '17 04:07

Sandeep Dvs


People also ask

How does REST API trigger Jenkins job?

The REST API feature can be enabled per Job basis. To enable REST API trigger for a Job, Navigate to Your JobName ->Configure -> Build triggers TAB and Check on 'Trigger build remotely'. Jenkins using combination of user credential based authentication and API token authentication.

How do I access REST API Jenkins?

They are available at /…/api/ where “…” portion is the object for which you'd like to access. The simplest way to access Jenkins REST API is to gather the User Token which is available by selecting your User and clicking on Configure. Now you will use the token as parameter for your authentication.


1 Answers

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

like image 167
dsaydon Avatar answered Oct 12 '22 23:10

dsaydon