forked from deployphp/recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack.php
79 lines (65 loc) · 2.42 KB
/
slack.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
75
76
77
78
79
<?php
/* (c) Tomas Majer <[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"));
});
// Do not skip slack notifications by default
set('slack_skip_notification', false);
desc('Notifying Slack channel of deployment');
task('deploy:slack', function () {
if (true === get('slack_skip_notification')) {
return;
}
global $php_errormsg;
$defaultConfig = [
'channel' => '#general',
'icon' => ':sunny:',
'username' => 'Deploy',
'message' => "Deployment to `{{host}}` on *{{stage}}* was successful\n({{release_path}})",
'app' => 'app-name',
];
$config = array_merge($defaultConfig, (array) get('slack'));
if (!is_array($config) || !isset($config['token']) || !isset($config['team']) || !isset($config['channel'])) {
throw new \RuntimeException("Please configure new slack: set('slack', ['token' => 'xoxp...', 'team' => 'team', 'channel' => '#channel', 'messsage' => 'message to send']);");
}
$server = \Deployer\Task\Context::get()->getServer();
if ($server instanceof \Deployer\Server\Local) {
$user = get('local_user');
} else {
$user = $server->getConfiguration()->getUser() ? : null;
}
$messagePlaceHolders = [
'{{release_path}}' => get('release_path'),
'{{host}}' => get('server.host'),
'{{stage}}' => get('stages')[0],
'{{user}}' => $user,
'{{branch}}' => get('branch'),
'{{app_name}}' => isset($config['app']) ? $config['app'] : 'app-name',
];
$config['message'] = strtr($config['message'], $messagePlaceHolders);
$urlParams = [
'channel' => $config['channel'],
'token' => $config['token'],
'text' => $config['message'],
'username' => $config['username'],
'icon_emoji' => $config['icon'],
'pretty' => true
];
if (isset($config['icon_url'])) {
unset($urlParams['icon_emoji']);
$urlParams['icon_url'] = $config['icon_url'];
}
$url = 'https://slack.com/api/chat.postMessage?' . http_build_query($urlParams);
$result = @file_get_contents($url);
if (!$result) {
throw new \RuntimeException($php_errormsg);
}
});