forked from deployphp/recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollbar.php
74 lines (61 loc) · 2.47 KB
/
rollbar.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
<?php
/* (c) Laurent Goussard <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Deployer;
/**
* Get local username
*/
set('local_user', function () {
return trim(run("whoami"));
});
desc('Notifying Rollbar of deployment');
task('deploy:rollbar', function () {
global $php_errormsg;
$defaultConfig = [
'access_token' => null,
'environment' => get('stages')[0],
'revision' => trim(runLocally('git log -n 1 --format="%h"')),
'local_username' => trim(runLocally('git config user.name')),
'rollbar_username' => null,
'comment' => "Deployment to `{{host}}` on *{{stage}}* was successful\n({{release_path}})",
];
$config = array_merge($defaultConfig, (array) get('rollbar', []));
if (!is_array($config) || !isset($config['access_token'])) {
throw new \RuntimeException("Please configure new rollbar: set('rollbar', ['access_token' => 'c09a3...', 'revision' => 'v4.3', 'rollbar_username' => 'John Doe', 'comment' => 'Brand new version']);");
}
$server = \Deployer\Task\Context::get()->getServer();
if ($server instanceof \Deployer\Server\Local) {
$user = get('local_user');
} else {
$user = $server->getConfiguration()->getUser() ? : null;
}
$commentPlaceHolders = [
'{{release_path}}' => get('release_path'),
'{{host}}' => get('server.host'),
'{{stage}}' => get('stages')[0],
'{{user}}' => $user,
'{{branch}}' => get('branch'),
];
$config['comment'] = strtr($config['comment'], $commentPlaceHolders);
$urlParams = [
'access_token' => $config['access_token'],
'environment' => $config['environment'],
'revision' => $config['revision'],
'local_username' => $config['local_username'],
'rollbar_username' => $config['rollbar_username'],
'comment' => $config['comment'],
];
$options = array('http' => array(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'content' => http_build_query($urlParams),
));
$context = stream_context_create($options);
$result = @file_get_contents('https://api.rollbar.com/api/1/deploy/', false, $context);
if (!$result) {
throw new \RuntimeException($php_errormsg);
}
});