Jenkins Laravel API is a set of classes designed to interact with Jenkins CI using its API.
The recommended way to install Jenkins Laravel API is through Composer.
curl -sS https://getcomposer.org/installer | php
Then, run the Composer command to install the latest version:
In bash terminal
composer.phar require thusithawijethunga/jenkins-laravel-api
In windows cmd terminal
composer require thusithawijethunga/jenkins-laravel-api
or Update your composer json
"require": {
...,
"thusithawijethunga/jenkins-laravel-api": "^1.0"
}
php artisan vendor:publish --tag=config
or
php artisan vendor:publish --provider="JenkinsLaravel\JenkinServiceProvider" --tag="config"
Before anything, you need to instantiate the client :
Update your Laravel Env File
# Url Is Https
JENKINS_URL_HTTPS = false
JENKINS_DOMAIN = host.org
JENKINS_PORT = 8080
JENKINS_USER = admin
JENKINS_TOKEN = token
or update config file
\config\jenkinapi.php
$jenkins = new \JenkinsLaravel\Jenkins();
If your Jenkins needs authentication, you need to pass a URL like this : 'http://user:[email protected]:8080'
.
https://{jenkins}/user/{user-name}/configure
Here are some examples of how to use it:
use JenkinsLaravel\Jenkins as JenkinsApi;
$jenkins = new JenkinsApi();
$jenkins->initialize();
$job = $jenkins->getJob("dev2-pull");
$job->getName();
$job->getFullDisplayName();
$job->getColor();
$job->getIsDisabled();
$job->getNextBuildNumber();
$job->getUrl();
foreach ($job->getHealthReport() as $health) {
$health->iconClassName;
$health->description;
$health->score;
}
$job->getColor(); // blue,red,notbuilt
if($job->getQueueItem())
{
$job->getQueueItem()->getUrl();
$job->getQueueItem()->getInQueueSince();
$job->getQueueItem()->getWhy();
}
foreach ($job->getBuilds() as $build) {
$build->getUrl();
}
if($job->getLastBuild())
{
$job->getLastBuild()->getUrl();
$job->getLastBuild()->getNumber();
}
if($job->getLastCompletedBuild())
{
$job->getLastCompletedBuild()->getUrl();
$job->getLastCompletedBuild()->getNumber();
}
if($job->getLastFailedBuild())
{
$job->getLastFailedBuild()->getUrl();
$job->getLastFailedBuild()->getNumber();
}
if($job->getLastStableBuild())
{
$job->getLastStableBuild()->getUrl();
$job->getLastStableBuild()->getNumber();
}
if($job->getLastSuccessfulBuild())
{
$job->getLastSuccessfulBuild()->getUrl();
$job->getLastSuccessfulBuild()->getNumber();
}
if($job->getLastUnstableBuild())
{
$job->getLastUnstableBuild()->getUrl();
$job->getLastUnstableBuild()->getNumber();
}
if($job->getLastUnsuccessfulBuild())
{
$job->getLastUnsuccessfulBuild()->getUrl();
$job->getLastUnsuccessfulBuild()->getNumber();
}
// is Job Buildable?
$job->getBuildable();
$allJobs = $jenkins->getJobs();
foreach ($allJobs as $job) {
# color
$job->getColor()
# name
$job->getName()
# url
$job->getUrl()
}
$jenkins = new JenkinsApi();
$jenkins->initialize();
$job = $jenkins->launchJob("clone-deploy");
var_dump($job);
// bool(true) if successful or throws a RuntimeException
$jenkins = new JenkinsApi();
$jenkins->initialize();
$view = $jenkins->getView('madb_deploy');
foreach ($view->getJobs() as $job) {
var_dump($job->getName());
}
//string(13) "altlinux-pull"
//string(8) "dev-pull"
//string(9) "dev2-pull"
//string(11) "fedora-pull"
$allViews = $jenkins->getViews();
foreach ($allViews as $view) {
# name
$job->getName()
# url
$job->getUrl()
}
$jenkins = new JenkinsApi();
$jenkins->initialize();
$job = $jenkins->getJob('dev2-pull');
foreach ($job->getBuilds() as $build) {
var_dump($build->getNumber());
var_dump($build->getResult());
}
//int(122)
//string(7) "SUCCESS"
//int(121)
//string(7) "FAILURE"
var_dump($jenkins->isAvailable());
//bool(true);
var_dump($jenkins->getJenkinsVersion());
//string(7) "2.361.1";
use JenkinsLaravel\Facade\Jenkin as JenkinsFacade;
# Call initialize function before calling each of other functions
JenkinsFacade::initialize();
$isAvailable = JenkinsFacade::isAvailable();
$jenkinsVersion = JenkinsFacade::getJenkinsVersion();
$allJobs = JenkinsFacade::getJobs();
$allViews = JenkinsFacade::getViews();
For more information, see the Jenkins API.
This projects follows PSR-0, PSR-1, PSR-2, PSR-4
- createJob function need confirm