Jay Blanchard
Jay Blanchard

Reputation: 34416

Connecting to Jenkins Remote API with PHP

In the effort to create a unified CICD process for our applications I am trying to connect to my Jenkins remote access API utilizing this (poorly documented) Jenkins-PHP API which is essentially a wrapper for the cURL functions used for accessing a remote website.

Here is my test connection:

$uname = '';
$pword = '';
$api_token = ''; // not yet used

$jenkins = new \JenkinsKhan\Jenkins("http://$uname:[email protected]/jobs");
var_dump($jenkins->isAvailable());

This returns:

bool(false)

The isAvailable() function, part of the Jenkins API class is:

public function isAvailable()
{
    $curl = curl_init($this->baseUrl . '/api/json');
    curl_setopt($curl, \CURLOPT_RETURNTRANSFER, 1);
    curl_exec($curl);

    if (curl_errno($curl)) {
        return false;
    } else {
        try {
            $this->getQueue();
        } catch (RuntimeException $e) {
            //en cours de lancement de jenkins, on devrait passer par là
            return false;
        }
    }

    return true;
}

EDIT: I added an echo curl_error($curl); to the conditional in the function and it returns:

Could not resolve host %mADA

NOTE: the web server I am running and the Jenkins instance are on the same network and the Jenkins instance is pingable.

I have found some unanswered question on Stack Overflow which are similar, like this one but no others which directly address the problem of connecting to the Jenkins instance.

If I change the URL string (remove the user name, replace the password with the API token, etc.) I get authentication errors at the most and a 403 (access denied) at the very least, so I feel as though I am making some headway but I am lead to believe that no real authentication occurs with the Jenkins instance.

Am I missing something obvious here? How can I make a real connection to the Jenkins remote API?

Upvotes: 0

Views: 1812

Answers (1)

Jay Blanchard
Jay Blanchard

Reputation: 34416

It turns out there are a couple of problems using the JenkinsKahn API class. I can only blame myself for not seeing that there had been no maintenance on the project in a year, so my current version of Jenkins wouldn't respond properly, but it did give me some hints.

Given the error was:

Could not resolve host

I started looking at the host string I was trying to access and determined that putting the user name and password in the string were making the host un-resolvable (thanks to some questions/interrogation/prodding by a good friend). Using the host string alone resulted in:

access denied (403)

First, this means I was reaching the host, but I was unable to access. Consequently I wrote some pure cURL to deal with the issue of not including the user name and password in the host string but letting cURL do the authentication.

Second, if I appended '/api/json' (according to Jenkin's docs on the subject) to the URL in my web browser I would get back a JSON string. So I made sure to include '/api/json' at the end of the URL for my cURL call.

Here is what I ended up with:

$url = "http://jenkins.svc.local/jobs/api/json";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$uname:$pword");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTHBASIC);
$output = curl_exec($ch);
curl_close($ch);

print_r(json_decode($output, true));

This now returns the array of information I was expecting to see and interact with. All that's left to do is use the data as needed and execute commands when required.

Upvotes: 1

Related Questions