Jenkins Rest api Using PHP

This Rest Api tutorial will show you how to use the PHP Guzzle rest client to connect to Jenkins’ rest API. To access the Jenkins Rest API, you can use any framework and any programming language. We will get information about all CI jobs from the Jenkins server.

Jenkins is a well-known self-contained and open-source build tool. Jenkins can be used to build, test, and deploy software to a server. Jenkins API information can be found at the bottom of the Jenkins server.
The URL would be look like, https://jenkins-hostname/job/CI_Jobs/api/json

Where is :

  • http://jenkins-hostname : This is the Hostname of the Jenkins server.
  • job/CI_Jobs : The Rest endpoint.
  • api/json : This is a postfix of each rest API, that tells the rest API data format for request and response.

What is Jenkins

The leading open-source automation server, Jenkins provides hundreds of plugins to support building, deploying, and automating any project. You can get structural information from here.

This rest API example tutorial help to get all CI jobs information from Jenkins.

Get ALL CI Jobs Using Jenkins Rest API

I’m using the guzzle rest client to get a list of CI jobs from Jenkins via the rest api. We require a rest api endpoint but do not require an authentication token to access CI jobs.

How To Create Jenkins Server HTTP Client

We will create client objects using like below,

require_once 'vendor/autoload.php';
use GuzzleHttp\Client;
$token = 'cf auth token';
$client = new Client([
// Base URI is used with relative requests
'base_uri' => 'https://jenkins-hostname/',
// You can set any number of default request options.
'timeout'  => 2.0,
'headers' => ['Content-Type' => 'application/json', "Accept" => "application/json"],
//ssl false
'verify' => false
]); 

Please change the token string as per your REST API credentials. The parameters are:

  • base_uri: The Pivotal Cloud Foundry host API URL path
  • headers: This will contain request header parameters
  • timeout: Request timeout in sec.
  • verify: SSL verification false when the host belongs to https protocol.

How to Fetch CI Jobs list using Jenkins Rest API

We have created CI rest client using token and rest API URL. We will use rest-client and pass /CI_Jobs endpoint with GET HTTP request. This rest call returns the CI jobs list as JSON objects.

require_once 'vendor/autoload.php';
use GuzzleHttp\Client;
$response = $client->get('job/CI_Jobs/api/json/');
echo "
";
echo $response->getBody()->getContents();

I hope it helps you access Jenkins Rest API using the Guzzle rest client.

HTTP Jenkins Rest Api Using Token>

If you are using token-based Jenkins rest API authentication then you need to pass token within guzzle client like below,

'headers' => ['Authorization' => "Bearer " . $token,'Content-Type' => 'application/json', "Accept" => "application/json"]

Genislab Technologies

NexGeneration complete end-2-end software testing & modern development operations tooling & solutions

Do you want to discuss your testing requirements with us? please don’t hesitate to hit the contact us button below, and we will get back to you at our earliest..

Share this article:

Facebook
Twitter
LinkedIn
WhatsApp