-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathJenkinsRunner.php
More file actions
162 lines (131 loc) · 4.42 KB
/
JenkinsRunner.php
File metadata and controls
162 lines (131 loc) · 4.42 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
namespace TryLib;
use Exception;
/**
* Abstract class for interfacing with Jenkins projects
* Every Jenkins project should be invokable via CLI using a
* "build" command. By default, the FreeStyle project's command is
* build. Any project can potentially take command line arguments.
* Finally, upon calling the "build" command via the CLI, the output
* from the CLI should be parseable to provide the results of the build.
*/
abstract class JenkinsRunner {
protected $jenkins_url;
protected $jenkins_cli;
protected $try_job_name;
protected $cmd_runner;
public $try_status;
public $try_base_url;
private $branch;
private $options;
private $callbacks;
private $ssh_key_path;
public function __construct(
$jenkins_url,
$jenkins_cli,
$try_job_name,
$cmd_runner
) {
if (filter_var($jenkins_url, FILTER_VALIDATE_URL) !== false) {
$this->jenkins_url = $jenkins_url;
} else {
throw new Exception("jenkins url must include protocol, ie http://, and trailing /, $jenkins_url given");
}
$this->jenkins_cli = $jenkins_cli;
$this->try_job_name = $try_job_name;
$this->cmd_runner = $cmd_runner;
$this->options = [];
$this->callbacks = [];
$this->ssh_key_path = null;
$this->branch = null;
$this->try_status = '';
$this->try_base_url = '';
}
abstract protected function pollForCompletion($pretty);
abstract protected function getBuildCommand();
abstract protected function getBuildExtraArguments($show_results, $show_progress);
public function runJenkinsCommand($command) {
$cmd = sprintf( "java -jar %s -s %s %s",
$this->jenkins_cli,
$this->jenkins_url,
$command
);
$this->cmd_runner->run($cmd, false, false);
}
/**
* Start the Jenkins job
*/
public function startJenkinsJob($show_results=false, $show_progress = false) {
// Build up the jenkins command incrementally
$cli_command = $this->buildCLICommand($show_results, $show_progress);
// Run the job
$this->runJenkinsCommand($cli_command);
if ($show_results || $show_progress || $this->getCallbacks()) {
$this->pollForCompletion($show_progress);
$this->executeCallbacks();
}
}
public function getOptions() {
return $this->options;
}
public function setParam($key, $value) {
$param = sprintf('-p %s=%s', $key, $value);
if (!in_array($param, $this->options)) {
$this->options[] = $param;
}
}
public function setSshKey($ssh_key_path) {
if (file_exists($ssh_key_path)) {
$this->ssh_key_path = $ssh_key_path;
}
}
public function getSsKey() {
return $this->ssh_key_path;
}
public function setPatch($patch) {
if (file_exists($patch)) {
$this->options[] = '-p patch.diff=' . $patch;
} else {
$this->cmd_runner->terminate("Patch file not found (${patch})");
}
}
public function addCallback($callback) {
if (is_null($callback)) {
return;
} else if (is_string($callback)) {
$this->callbacks[] = $callback;
} else {
$this->cmd_runner->warn('Invalid callback - must be a string');
}
}
public function getCallbacks() {
return $this->callbacks;
}
/**
* Build the Jenkins CLI command, based on all options
*/
function buildCLICommand($show_results, $show_progress) {
$command = [];
if (!is_null($this->getSsKey())) {
$command[] = '-i ' . $this->getSsKey();
}
$command[] = $this->getBuildCommand();
$command[] = $this->try_job_name;
$extra_args = $this->getBuildExtraArguments($show_results, $show_progress);
$options = $this->getOptions();
return implode(' ', array_merge($command, $extra_args, $options));
}
function executeCallbacks() {
foreach ($this->getCallbacks() as $callback) {
$this->executeCallback($callback);
}
}
function executeCallback($callback) {
$callback = str_replace(
['${status}', '${url}'],
[$this->try_status, $this->try_base_url],
$callback
);
$this->cmd_runner->run($callback, false, true);
}
}