1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* this file is part of pipelines */ |
4
|
|
|
|
5
|
|
|
namespace Ktomk\Pipelines\File\Pipeline; |
6
|
|
|
|
7
|
|
|
use Ktomk\Pipelines\File\Artifacts; |
8
|
|
|
use Ktomk\Pipelines\File\Image; |
9
|
|
|
use Ktomk\Pipelines\File\ParseException; |
10
|
|
|
use Ktomk\Pipelines\File\Pipeline; |
11
|
|
|
|
12
|
|
|
class Step |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
private $step; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var int number of the step, starting at one |
21
|
|
|
*/ |
22
|
|
|
private $index; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Pipeline |
26
|
|
|
*/ |
27
|
|
|
private $pipeline; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array step environment variables |
31
|
|
|
* BITBUCKET_PARALLEL_STEP - zero-based index of the current step in the group, e.g. 0, 1, 2, ... |
32
|
|
|
* BITBUCKET_PARALLEL_STEP_COUNT - total number of steps in the group, e.g. 5. |
33
|
|
|
*/ |
34
|
|
|
private $env; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Step constructor. |
38
|
|
|
* |
39
|
|
|
* @param Pipeline $pipeline |
40
|
|
|
* @param int $index |
41
|
|
|
* @param array $step |
42
|
|
|
* @param array $env [optional] environment variables in array notation for the new step |
43
|
|
|
*/ |
44
|
16 |
|
public function __construct(Pipeline $pipeline, $index, array $step, array $env = array()) |
45
|
|
|
{ |
46
|
|
|
// quick validation: image name |
47
|
16 |
|
Image::validate($step); |
48
|
|
|
|
49
|
|
|
// quick validation: script |
50
|
15 |
|
$this->parseScript($step); |
51
|
|
|
|
52
|
12 |
|
$this->pipeline = $pipeline; |
53
|
12 |
|
$this->index = $index; |
54
|
12 |
|
$this->step = $step; |
55
|
12 |
|
$this->env = $env; |
56
|
12 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @throws \Ktomk\Pipelines\File\ParseException |
60
|
|
|
* @return null|Artifacts |
61
|
|
|
*/ |
62
|
3 |
|
public function getArtifacts() |
63
|
|
|
{ |
64
|
3 |
|
return isset($this->step['artifacts']) |
65
|
1 |
|
? new Artifacts($this->step['artifacts']) |
66
|
3 |
|
: null; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @throws \Ktomk\Pipelines\File\ParseException |
71
|
|
|
* @return Image |
72
|
|
|
*/ |
73
|
3 |
|
public function getImage() |
74
|
|
|
{ |
75
|
3 |
|
return isset($this->step['image']) |
76
|
1 |
|
? new Image($this->step['image']) |
77
|
3 |
|
: $this->pipeline->getFile()->getImage(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return null|string |
82
|
|
|
*/ |
83
|
3 |
|
public function getName() |
84
|
|
|
{ |
85
|
3 |
|
return isset($this->step['name']) |
86
|
1 |
|
? (string)$this->step['name'] |
87
|
3 |
|
: null; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return StepServices |
92
|
|
|
*/ |
93
|
1 |
|
public function getServices() |
94
|
|
|
{ |
95
|
1 |
|
$services = isset($this->step['services']) ? $this->step['services'] : array(); |
96
|
|
|
|
97
|
1 |
|
return new StepServices($this, $services); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return array|string[] |
102
|
|
|
*/ |
103
|
2 |
|
public function getScript() |
104
|
|
|
{ |
105
|
2 |
|
return $this->step['script']; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Specify data which should be serialized to JSON |
110
|
|
|
* |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
1 |
|
public function jsonSerialize() |
114
|
|
|
{ |
115
|
1 |
|
$image = $this->getImage(); |
116
|
1 |
|
$image = null === $image ? '' : $image->jsonSerialize(); |
117
|
|
|
|
118
|
|
|
return array( |
119
|
1 |
|
'name' => $this->getName(), |
120
|
1 |
|
'image' => $image, |
121
|
1 |
|
'script' => $this->getScript(), |
122
|
1 |
|
'artifacts' => $this->getArtifacts(), |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return int |
128
|
|
|
*/ |
129
|
1 |
|
public function getIndex() |
130
|
|
|
{ |
131
|
1 |
|
return $this->index; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return Pipeline |
136
|
|
|
* @codeCoverageIgnore |
137
|
|
|
*/ |
138
|
|
|
public function getPipeline() |
139
|
|
|
{ |
140
|
|
|
return $this->pipeline; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return array step container environment variables (e.g. parallel a step) |
145
|
|
|
*/ |
146
|
1 |
|
public function getEnv() |
147
|
|
|
{ |
148
|
1 |
|
return $this->env; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Parse a step script section |
153
|
|
|
* |
154
|
|
|
* @param array $step |
155
|
|
|
* @throws \Ktomk\Pipelines\File\ParseException |
156
|
|
|
*/ |
157
|
15 |
|
private function parseScript(array $step) |
158
|
|
|
{ |
159
|
15 |
|
if (!isset($step['script'])) { |
160
|
1 |
|
ParseException::__("'step' requires a script"); |
161
|
|
|
} |
162
|
14 |
|
if (!is_array($step['script']) || !count($step['script'])) { |
163
|
1 |
|
ParseException::__("'script' requires a list of commands"); |
164
|
|
|
} |
165
|
|
|
|
166
|
13 |
|
foreach ($step['script'] as $index => $line) { |
167
|
13 |
|
if (!is_scalar($line) && null !== $line) { |
168
|
1 |
|
ParseException::__( |
169
|
1 |
|
sprintf( |
170
|
1 |
|
"'script' requires a list of commands, step #%d is not a command", |
171
|
1 |
|
$index |
172
|
|
|
) |
173
|
|
|
); |
174
|
|
|
} |
175
|
|
|
} |
176
|
12 |
|
} |
177
|
|
|
} |
178
|
|
|
|