1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
|
4
|
|
|
### Configuration options |
5
|
|
|
|
6
|
|
|
- **organization** *(required)*: the slug of the organization the release belongs to. |
7
|
|
|
- **projects** *(required)*: array of slugs of the projects to create a release for. |
8
|
|
|
- **token** *(required)*: authentication token. Can be created at [https://sentry.io/settings/account/api/auth-tokens/] |
9
|
|
|
- **version** *(required)* – a version identifier for this release. |
10
|
|
|
Can be a version number, a commit hash etc. (Defaults is set to git log -n 1 --format="%h".) |
11
|
|
|
- **version_prefix** *(optional)* - a string prefixed to version. |
12
|
|
|
Releases are global per organization so indipentent projects needs to prefix version number with unique string to avoid conflicts |
13
|
|
|
- **environment** *(optional)* - the environment you’re deploying to. By default framework's environment is used. |
14
|
|
|
For example for symfony, *symfony_env* configuration is read otherwise defaults to 'prod'. |
15
|
|
|
- **ref** *(optional)* – an optional commit reference. This is useful if a tagged version has been provided. |
16
|
|
|
- **refs** *(optional)* - array to indicate the start and end commits for each repository included in a release. |
17
|
|
|
Head commits must include parameters *repository* and *commit*) (the HEAD sha). |
18
|
|
|
They can optionally include *previousCommit* (the sha of the HEAD of the previous release), |
19
|
|
|
which should be specified if this is the first time you’ve sent commit data. |
20
|
|
|
- **commits** *(optional)* - array commits data to be associated with the release. |
21
|
|
|
Commits must include parameters *id* (the sha of the commit), and can optionally include *repository*, |
22
|
|
|
*message*, *author_name*, *author_email* and *timestamp*. By default will send all new commits, |
23
|
|
|
unless it's a first release, then only first 200 will be sent. |
24
|
|
|
- **url** *(optional)* – a URL that points to the release. This can be the path to an online interface to the sourcecode for instance. |
25
|
|
|
- **date_released** *(optional)* – date that indicates when the release went live. If not provided the current time is assumed. |
26
|
|
|
- **sentry_server** *(optional)* – sentry server (if you host it yourself). defaults to hosted sentry service. |
27
|
|
|
- **date_deploy_started** *(optional)* - date that indicates when the deploy started. Defaults to current time. |
28
|
|
|
- **date_deploy_finished** *(optional)* - date that indicates when the deploy ended. If not provided, the current time is used. |
29
|
|
|
- **deploy_name** *(optional)* - name of the deploy |
30
|
|
|
- **git_version_command** *(optional)* - the command that retrieves the git version information (Defaults is set to git log -n 1 --format="%h", other options are git describe --tags --abbrev=0) |
31
|
|
|
|
32
|
|
|
```php |
33
|
|
|
// deploy.php |
34
|
|
|
|
35
|
|
|
set('sentry', [ |
36
|
|
|
'organization' => 'exampleorg', |
37
|
|
|
'projects' => [ |
38
|
|
|
'exampleproj' |
39
|
|
|
], |
40
|
|
|
'token' => 'd47828...', |
41
|
|
|
'version' => '0.0.1', |
42
|
|
|
|
43
|
|
|
]); |
44
|
|
|
``` |
45
|
|
|
|
46
|
|
|
### Suggested Usage |
47
|
|
|
|
48
|
|
|
Since you should only notify Sentry of a successful deployment, the deploy:sentry task should be executed right at the end. |
49
|
|
|
|
50
|
|
|
```php |
51
|
|
|
// deploy.php |
52
|
|
|
|
53
|
|
|
after('deploy', 'deploy:sentry'); |
54
|
|
|
``` |
55
|
|
|
|
56
|
|
|
*/ |
57
|
|
|
|
58
|
|
|
namespace Deployer; |
59
|
|
|
|
60
|
|
|
use Closure; |
61
|
|
|
use DateTime; |
62
|
|
|
use Deployer\Exception\ConfigurationException; |
63
|
|
|
use Deployer\Utility\Httpie; |
64
|
|
|
|
65
|
|
|
desc('Notifies Sentry of deployment'); |
66
|
|
|
task( |
67
|
|
|
'deploy:sentry', |
68
|
|
|
static function () { |
69
|
|
|
$now = date('c'); |
70
|
|
|
|
71
|
|
|
$defaultConfig = [ |
72
|
|
|
'version' => getReleaseGitRef(), |
73
|
|
|
'version_prefix' => null, |
74
|
|
|
'refs' => [], |
75
|
|
|
'ref' => null, |
76
|
|
|
'commits' => getGitCommitsRefs(), |
77
|
|
|
'url' => null, |
78
|
|
|
'date_released' => $now, |
79
|
|
|
'date_deploy_started' => $now, |
80
|
|
|
'date_deploy_finished' => $now, |
81
|
|
|
'sentry_server' => 'https://sentry.io', |
82
|
|
|
'previous_commit' => null, |
83
|
|
|
'environment' => get('symfony_env', 'prod'), |
84
|
|
|
'deploy_name' => null, |
85
|
|
|
]; |
86
|
|
|
|
87
|
|
|
$config = array_merge($defaultConfig, (array) get('sentry')); |
88
|
|
|
array_walk( |
89
|
|
|
$config, |
90
|
|
|
static function (&$value) use ($config) { |
91
|
|
|
if (is_callable($value)) { |
92
|
|
|
$value = $value($config); |
93
|
|
|
} |
94
|
|
|
}, |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
if ( |
98
|
|
|
!isset($config['organization'], $config['token'], $config['version']) |
99
|
|
|
|| (empty($config['projects']) || !is_array($config['projects'])) |
100
|
|
|
) { |
101
|
|
|
throw new \RuntimeException( |
102
|
|
|
<<<EXAMPLE |
103
|
|
|
Required data missing. Please configure sentry: |
104
|
|
|
set( |
105
|
|
|
'sentry', |
106
|
|
|
[ |
107
|
|
|
'organization' => 'exampleorg', |
108
|
|
|
'projects' => [ |
109
|
|
|
'exampleproj', |
110
|
|
|
'exampleproje2' |
111
|
|
|
], |
112
|
|
|
'token' => 'd47828...', |
113
|
|
|
] |
114
|
|
|
);" |
115
|
|
|
EXAMPLE, |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$releaseData = array_filter( |
120
|
|
|
[ |
121
|
|
|
'version' => ($config['version_prefix'] ?? '') . $config['version'], |
122
|
|
|
'refs' => $config['refs'], |
123
|
|
|
'ref' => $config['ref'], |
124
|
|
|
'url' => $config['url'], |
125
|
|
|
'commits' => array_slice($config['commits'] ?? [], 0), // reset keys to serialize as array in json |
126
|
|
|
'dateReleased' => $config['date_released'], |
127
|
|
|
'projects' => $config['projects'], |
128
|
|
|
'previousCommit' => $config['previous_commit'], |
129
|
|
|
], |
130
|
|
|
); |
131
|
|
|
|
132
|
|
|
$releasesApiUrl = $config['sentry_server'] . '/api/0/organizations/' . $config['organization'] . '/releases/'; |
133
|
|
|
$response = Httpie::post( |
134
|
|
|
$releasesApiUrl, |
135
|
|
|
) |
136
|
|
|
->setopt(CURLOPT_TIMEOUT, 10) |
137
|
|
|
->header('Authorization', sprintf('Bearer %s', $config['token'])) |
138
|
|
|
->jsonBody($releaseData) |
139
|
|
|
->getJson(); |
140
|
|
|
|
141
|
|
|
if (!isset($response['version'], $response['projects'])) { |
142
|
|
|
throw new \RuntimeException(sprintf('Unable to create a release: %s', print_r($response, true))); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
writeln( |
146
|
|
|
sprintf( |
147
|
|
|
'<info>Sentry:</info> Release of version <comment>%s</comment> ' . |
148
|
|
|
'for projects: <comment>%s</comment> created successfully.', |
149
|
|
|
$response['version'], |
150
|
|
|
implode(', ', array_column($response['projects'], 'slug')), |
151
|
|
|
), |
152
|
|
|
); |
153
|
|
|
|
154
|
|
|
$deployData = array_filter( |
155
|
|
|
[ |
156
|
|
|
'environment' => $config['environment'], |
157
|
|
|
'name' => $config['deploy_name'], |
158
|
|
|
'url' => $config['url'], |
159
|
|
|
'dateStarted' => $config['date_deploy_started'], |
160
|
|
|
'dateFinished' => $config['date_deploy_finished'], |
161
|
|
|
], |
162
|
|
|
); |
163
|
|
|
|
164
|
|
|
$response = Httpie::post( |
165
|
|
|
$releasesApiUrl . $response['version'] . '/deploys/', |
166
|
|
|
) |
167
|
|
|
->setopt(CURLOPT_TIMEOUT, 10) |
168
|
|
|
->header('Authorization', sprintf('Bearer %s', $config['token'])) |
169
|
|
|
->jsonBody($deployData) |
170
|
|
|
->getJson(); |
171
|
|
|
|
172
|
|
|
if (!isset($response['id'], $response['environment'])) { |
173
|
|
|
throw new \RuntimeException(sprintf('Unable to create a deployment: %s', print_r($response, true))); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
writeln( |
177
|
|
|
sprintf( |
178
|
|
|
'<info>Sentry:</info> Deployment <comment>%s</comment> ' . |
179
|
|
|
'for environment <comment>%s</comment> created successfully', |
180
|
|
|
$response['id'], |
181
|
|
|
$response['environment'], |
182
|
|
|
), |
183
|
|
|
); |
184
|
|
|
}, |
185
|
|
|
); |
186
|
|
|
|
187
|
|
|
function getPreviousReleaseRevision() |
188
|
|
|
{ |
189
|
|
|
switch (get('update_code_strategy')) { |
190
|
|
|
case 'archive': |
191
|
|
|
if (has('previous_release')) { |
192
|
|
|
return run('cat {{previous_release}}/REVISION'); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return null; |
196
|
|
|
case 'clone': |
197
|
|
|
if (has('previous_release')) { |
198
|
|
|
cd('{{previous_release}}'); |
199
|
|
|
return trim(run('git rev-parse HEAD')); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return null; |
203
|
|
|
default: |
204
|
|
|
throw new ConfigurationException(parse("Unknown `update_code_strategy` option: {{update_code_strategy}}.")); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
function getCurrentReleaseRevision() |
209
|
|
|
{ |
210
|
|
|
switch (get('update_code_strategy')) { |
211
|
|
|
case 'archive': |
212
|
|
|
return run('cat {{release_path}}/REVISION'); |
213
|
|
|
|
214
|
|
|
case 'clone': |
215
|
|
|
cd('{{release_path}}'); |
216
|
|
|
return trim(run('git rev-parse HEAD')); |
217
|
|
|
|
218
|
|
|
default: |
219
|
|
|
throw new ConfigurationException(parse("Unknown `update_code_strategy` option: {{update_code_strategy}}.")); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
function getReleaseGitRef(): Closure |
224
|
|
|
{ |
225
|
|
|
return static function ($config = []): string { |
226
|
|
|
if (get('update_code_strategy') === 'archive') { |
227
|
|
|
if (isset($config['git_version_command'])) { |
228
|
|
|
cd('{{deploy_path}}/.dep/repo'); |
229
|
|
|
|
230
|
|
|
return trim(run($config['git_version_command'])); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return run('cat {{current_path}}/REVISION'); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
cd('{{release_path}}'); |
237
|
|
|
|
238
|
|
|
if (isset($config['git_version_command'])) { |
239
|
|
|
return trim(run($config['git_version_command'])); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
return trim(run('git log -n 1 --format="%h"')); |
243
|
|
|
}; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
function getGitCommitsRefs(): Closure |
247
|
|
|
{ |
248
|
|
|
return static function ($config = []): array { |
|
|
|
|
249
|
|
|
$previousReleaseRevision = getPreviousReleaseRevision(); |
250
|
|
|
$currentReleaseRevision = getCurrentReleaseRevision() ?: 'HEAD'; |
251
|
|
|
|
252
|
|
|
if ($previousReleaseRevision === null) { |
253
|
|
|
$commitRange = $currentReleaseRevision; |
254
|
|
|
} else { |
255
|
|
|
$commitRange = $previousReleaseRevision . '..' . $currentReleaseRevision; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
try { |
259
|
|
|
if (get('update_code_strategy') === 'archive') { |
260
|
|
|
cd('{{deploy_path}}/.dep/repo'); |
261
|
|
|
} else { |
262
|
|
|
cd('{{release_path}}'); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
$result = run(sprintf('git rev-list --pretty="%s" %s', 'format:%H#%an#%ae#%at#%s', $commitRange)); |
266
|
|
|
$lines = array_filter( |
267
|
|
|
// limit number of commits for first release with many commits |
268
|
|
|
array_map('trim', array_slice(explode("\n", $result), 0, 200)), |
269
|
|
|
static function (string $line): bool { |
270
|
|
|
return !empty($line) && strpos($line, 'commit') !== 0; |
271
|
|
|
}, |
272
|
|
|
); |
273
|
|
|
|
274
|
|
|
return array_map( |
275
|
|
|
static function (string $line): array { |
276
|
|
|
[$ref, $authorName, $authorEmail, $timestamp, $message] = explode('#', $line, 5); |
277
|
|
|
|
278
|
|
|
return [ |
279
|
|
|
'id' => $ref, |
280
|
|
|
'author_name' => $authorName, |
281
|
|
|
'author_email' => $authorEmail, |
282
|
|
|
'message' => $message, |
283
|
|
|
'timestamp' => date(\DateTime::ATOM, (int) $timestamp), |
284
|
|
|
]; |
285
|
|
|
}, |
286
|
|
|
$lines, |
287
|
|
|
); |
288
|
|
|
|
289
|
|
|
} catch (\Deployer\Exception\RunException $e) { |
290
|
|
|
writeln($e->getMessage()); |
291
|
|
|
return []; |
292
|
|
|
} |
293
|
|
|
}; |
294
|
|
|
} |
295
|
|
|
|