|
1
|
|
|
<?php |
|
2
|
|
|
/* (c) Viacheslav Ostrovskiy <[email protected]> |
|
3
|
|
|
* |
|
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
5
|
|
|
* file that was distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Deployer; |
|
9
|
|
|
|
|
10
|
|
|
use Closure; |
|
11
|
|
|
use DateTime; |
|
12
|
|
|
use Deployer\Utility\Httpie; |
|
13
|
|
|
|
|
14
|
|
|
desc('Notifying Sentry of deployment'); |
|
15
|
|
|
task( |
|
16
|
|
|
'deploy:sentry', |
|
17
|
|
|
static function () { |
|
18
|
|
|
$now = date('c'); |
|
19
|
|
|
|
|
20
|
|
|
$defaultConfig = [ |
|
21
|
|
|
'version' => null, |
|
22
|
|
|
'version_prefix' => null, |
|
23
|
|
|
'refs' => [], |
|
24
|
|
|
'ref' => null, |
|
25
|
|
|
'commits' => null, |
|
26
|
|
|
'url' => null, |
|
27
|
|
|
'date_released' => $now, |
|
28
|
|
|
'date_deploy_started' => $now, |
|
29
|
|
|
'date_deploy_finished' => $now, |
|
30
|
|
|
'sentry_server' => 'https://sentry.io', |
|
31
|
|
|
'previous_commit' => null, |
|
32
|
|
|
'environment' => get('symfony_env', 'prod'), |
|
33
|
|
|
'deploy_name' => null, |
|
34
|
|
|
]; |
|
35
|
|
|
|
|
36
|
|
|
if (releaseIsGitDirectory()) { |
|
37
|
|
|
$defaultConfig['version'] = getReleaseGitRef(); |
|
38
|
|
|
$defaultConfig['commits'] = getGitCommitsRefs(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$config = array_merge($defaultConfig, (array) get('sentry')); |
|
42
|
|
|
array_walk( |
|
43
|
|
|
$config, |
|
44
|
|
|
static function (&$value) use ($config) { |
|
45
|
|
|
if (is_callable($value)) { |
|
46
|
|
|
$value = $value($config); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
|
|
if ( |
|
52
|
|
|
!isset($config['organization'], $config['token'], $config['version']) |
|
53
|
|
|
|| (empty($config['projects']) || !is_array($config['projects'])) |
|
54
|
|
|
) { |
|
55
|
|
|
throw new \RuntimeException( |
|
56
|
|
|
<<<EXAMPLE |
|
57
|
|
|
Required data missing. Please configure sentry: |
|
58
|
|
|
set( |
|
59
|
|
|
'sentry', |
|
60
|
|
|
[ |
|
61
|
|
|
'organization' => 'exampleorg', |
|
62
|
|
|
'projects' => [ |
|
63
|
|
|
'exampleproj', |
|
64
|
|
|
'exampleproje2' |
|
65
|
|
|
], |
|
66
|
|
|
'token' => 'd47828...', |
|
67
|
|
|
] |
|
68
|
|
|
);" |
|
69
|
|
|
EXAMPLE |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$releaseData = array_filter( |
|
74
|
|
|
[ |
|
75
|
|
|
'version' => ($config['version_prefix'] ?? '') . $config['version'], |
|
76
|
|
|
'refs' => $config['refs'], |
|
77
|
|
|
'ref' => $config['ref'], |
|
78
|
|
|
'url' => $config['url'], |
|
79
|
|
|
'commits' => array_slice($config['commits'] ?? [], 0), // reset keys to serialize as array in json |
|
80
|
|
|
'dateReleased' => $config['date_released'], |
|
81
|
|
|
'projects' => $config['projects'], |
|
82
|
|
|
'previousCommit' => $config['previous_commit'], |
|
83
|
|
|
] |
|
84
|
|
|
); |
|
85
|
|
|
|
|
86
|
|
|
$releasesApiUrl = $config['sentry_server'] . '/api/0/organizations/' . $config['organization'] . '/releases/'; |
|
87
|
|
|
$response = Httpie::post( |
|
88
|
|
|
$releasesApiUrl |
|
89
|
|
|
) |
|
90
|
|
|
->header(sprintf('Authorization: Bearer %s', $config['token'])) |
|
91
|
|
|
->body($releaseData) |
|
92
|
|
|
->getJson(); |
|
93
|
|
|
|
|
94
|
|
|
if (!isset($response['version'], $response['projects'])) { |
|
95
|
|
|
throw new \RuntimeException(sprintf('Unable to create a release: %s', print_r($response, true))); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
writeln( |
|
99
|
|
|
sprintf( |
|
100
|
|
|
'<info>Sentry:</info> Release of version <comment>%s</comment> ' . |
|
101
|
|
|
'for projects: <comment>%s</comment> created successfully.', |
|
102
|
|
|
$response['version'], |
|
103
|
|
|
implode(', ', array_column($response['projects'], 'slug')) |
|
104
|
|
|
) |
|
105
|
|
|
); |
|
106
|
|
|
|
|
107
|
|
|
$deployData = array_filter( |
|
108
|
|
|
[ |
|
109
|
|
|
'environment' => $config['environment'], |
|
110
|
|
|
'name' => $config['deploy_name'], |
|
111
|
|
|
'url' => $config['url'], |
|
112
|
|
|
'dateStarted' => $config['date_deploy_started'], |
|
113
|
|
|
'dateFinished' => $config['date_deploy_finished'], |
|
114
|
|
|
] |
|
115
|
|
|
); |
|
116
|
|
|
|
|
117
|
|
|
$response = Httpie::post( |
|
118
|
|
|
$releasesApiUrl . $response['version'] . '/deploys/' |
|
119
|
|
|
) |
|
120
|
|
|
->header(sprintf('Authorization: Bearer %s', $config['token'])) |
|
121
|
|
|
->body($deployData) |
|
122
|
|
|
->getJson(); |
|
123
|
|
|
|
|
124
|
|
|
if (!isset($response['id'], $response['environment'])) { |
|
125
|
|
|
throw new \RuntimeException(sprintf('Unable to create a deployment: %s', print_r($response, true))); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
writeln( |
|
129
|
|
|
sprintf( |
|
130
|
|
|
'<info>Sentry:</info> Deployment <comment>%s</comment> ' . |
|
131
|
|
|
'for environment <comment>%s</comment> created successfully', |
|
132
|
|
|
$response['id'], |
|
133
|
|
|
$response['environment'] |
|
134
|
|
|
) |
|
135
|
|
|
); |
|
136
|
|
|
} |
|
137
|
|
|
); |
|
138
|
|
|
|
|
139
|
|
|
function releaseIsGitDirectory() |
|
140
|
|
|
{ |
|
141
|
|
|
return (bool) run('cd {{release_path}} && git rev-parse --git-dir > /dev/null 2>&1 && echo 1 || echo 0'); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
function getReleaseGitRef(): Closure |
|
145
|
|
|
{ |
|
146
|
|
|
return static function ($config = []): string { |
|
147
|
|
|
cd('{{release_path}}'); |
|
148
|
|
|
|
|
149
|
|
|
if (isset($config['git_version_command'])) { |
|
150
|
|
|
return trim(run($config['git_version_command'])); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return trim(run('git log -n 1 --format="%h"')); |
|
154
|
|
|
}; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
function getGitCommitsRefs(): Closure |
|
158
|
|
|
{ |
|
159
|
|
|
return static function ($config = []): array { |
|
|
|
|
|
|
160
|
|
|
$previousReleaseRevision = null; |
|
161
|
|
|
|
|
162
|
|
|
if (has('previous_release')) { |
|
163
|
|
|
cd('{{previous_release}}'); |
|
164
|
|
|
$previousReleaseRevision = trim(run('git rev-parse HEAD')); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
if ($previousReleaseRevision === null) { |
|
168
|
|
|
$commitRange = 'HEAD'; |
|
169
|
|
|
} else { |
|
170
|
|
|
$commitRange = $previousReleaseRevision . '..HEAD'; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
cd('{{release_path}}'); |
|
174
|
|
|
|
|
175
|
|
|
try { |
|
176
|
|
|
$result = run(sprintf('git rev-list --pretty="%s" %s', 'format:%H#%an#%ae#%at#%s', $commitRange)); |
|
177
|
|
|
$lines = array_filter( |
|
178
|
|
|
// limit number of commits for first release with many commits |
|
179
|
|
|
array_map('trim', array_slice(explode("\n", $result), 0, 200)), |
|
180
|
|
|
static function (string $line): bool { |
|
181
|
|
|
return !empty($line) && strpos($line, 'commit') !== 0; |
|
182
|
|
|
} |
|
183
|
|
|
); |
|
184
|
|
|
|
|
185
|
|
|
return array_map( |
|
186
|
|
|
static function (string $line): array { |
|
187
|
|
|
list($ref, $authorName, $authorEmail, $timestamp, $message) = explode('#', $line, 5); |
|
188
|
|
|
|
|
189
|
|
|
return [ |
|
190
|
|
|
'id' => $ref, |
|
191
|
|
|
'author_name' => $authorName, |
|
192
|
|
|
'author_email' => $authorEmail, |
|
193
|
|
|
'message' => $message, |
|
194
|
|
|
'timestamp' => date(DateTime::ATOM, (int) $timestamp), |
|
195
|
|
|
]; |
|
196
|
|
|
}, |
|
197
|
|
|
$lines |
|
198
|
|
|
); |
|
199
|
|
|
|
|
200
|
|
|
} catch (\Deployer\Exception\RunException $e) { |
|
201
|
|
|
writeln($e->getMessage()); |
|
202
|
|
|
return []; |
|
203
|
|
|
} |
|
204
|
|
|
}; |
|
205
|
|
|
} |
|
206
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.