-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkins.php
112 lines (99 loc) · 2.91 KB
/
Jenkins.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
/**
* PHPCI - Continuous Integration for PHP
*
* @copyright Copyright 2015, Clounce.com
* @license https://github.com/kdemanuele/PHPCI-Jenkins-Plugin/blob/master/LICENSE
* @link https://www.clounce.com/
*/
namespace PHPCI\Plugin;
use PHPCI;
use PHPCI\Builder;
use PHPCI\Model\Build;
/**
* Jenkins trigger for Builds
*
* Extension assumes that Jenkins doesn't require authentication to trigger a build process
*
* @author Karlston D'Emanuele (kd@clounce.com)
* @package PHPCI
* @subpackage Plugins
*/
class Jenkins implements PHPCI\Plugin
{
/**
* @var \PHPCI\Builder
*/
protected $phpci;
/**
* @var String
*/
protected $jenkinsUrl;
/**
* @var String
*/
protected $jenkinsProject;
/**
* @var String
*/
protected $jenkinsToken;
/**
* @param \Psr\Log\LoggerInterface $log
* @param \PHPCI\Builder $phpci
* @param \PHPCI\Model\Build $build
* @param array $options
*/
public function __construct(Builder $phpci, Build $build, array $options = array())
{
$this->phpci = $phpci;
$this->build = $build;
$this->directory = $phpci->buildPath;
$this->standard = 'PSR2';
$this->jenkinsUrl = '';
$this->jenkinsProject = '';
$this->jenkinsToken = null;
$this->setOptions($options);
}
/**
* Handle this plugin's options.
* @param $options
*/
protected function setOptions($options)
{
foreach (array('url', 'project', 'token') as $key) {
if (array_key_exists($key, $options)) {
$prop = 'jenkins' . ucfirst($key);
$this->{$prop} = $options[$key];
}
}
}
/**
* Triggers Jenkins Build for the project
*/
public function execute()
{
$success = true;
if ($this->build->isSuccessful()) {
// Builds the Jenkins trigger
$jenkins = $this->jenkinsUrl . '/job/' . rawurlencode($this->jenkinsProject) . '/build?delay=0sec';
if ($this->jenkinsToken && !empty($this->jenkinsToken)) {
$jenkins .= '&token=%s';
}
$jenkins = sprintf($jenkins, rawurlencode($this->jenkinsProject), $this->jenkinsToken);
$this->phpci->log($jenkins);
$curlHandler = curl_init($jenkins);
curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, 1);
curl_exec($curlHandler);
$status = curl_getinfo($curlHandler, CURLINFO_HTTP_CODE);
$curlUrl = curl_getinfo($curlHandler, CURLINFO_EFFECTIVE_URL);
curl_close($curlHandler);
if ($status != '200') {
$this->phpci->logFailure($curlUrl . ' return with status code ' . $status);
return false;
}
} else {
$this->phpci->log('Skipping due to failed Build');
}
return $success;
}
}