Skip to content
This repository was archived by the owner on Apr 26, 2020. It is now read-only.

Commit 2e541d9

Browse files
committed
Merge pull request #27 from kbsali/slack-msg
add support for place holders in slack messages
2 parents 73a3aee + b01a68c commit 2e541d9

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

docs/slack.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ require 'vendor/deployphp/recipes/recipes/slack.php';
1414

1515
You can provide also other configuration options:
1616

17-
- *message* - default is **Deployment to '{$host}' on *{$prod}* was successful\n({$releasePath})**
17+
- *message* - default is **Deployment to `{{host}}` on *{{stage}}* was successful\n({{release_path}})**
18+
- the available placeholders for the message parameter are:
19+
- {{release_path}}
20+
- {{host}}
21+
- {{stage}}
22+
- {{user}}
23+
- {{branch}}
24+
- {{app_name}}
1825
- *channel* - default is **#general**
1926
- *icon* - default is **:sunny:**
2027
- *username* - default is **Deploy**
@@ -26,6 +33,7 @@ You can provide also other configuration options:
2633
set('slack', [
2734
'token' => 'xoxp-...',
2835
'team' => 'team name',
36+
'app' => 'app name',
2937
]);
3038
```
3139

recipes/slack.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,27 @@
1414
$config = get('slack', []);
1515

1616
if (!isset($config['message'])) {
17-
$releasePath = env('release_path');
18-
$host = env('server.host');
19-
$stage = env('stages')[0];
20-
$config['message'] = "Deployment to '{$host}' on *{$stage}* was successful\n($releasePath)";
17+
$config['message'] = "Deployment to `{{host}}` on *{{stage}}* was successful\n({{release_path}})";
2118
}
2219

20+
$server = \Deployer\Task\Context::get()->getServer()->getConfiguration();
21+
$host = $server->getHost();
22+
$user = !$server->getUser() ? null : $server->getUser();
23+
$messagePlaceHolders = [
24+
'{{release_path}}' => env('release_path'),
25+
'{{host}}' => env('server.host'),
26+
'{{stage}}' => env('stages')[0],
27+
'{{user}}' => $user,
28+
'{{branch}}' => env('branch'),
29+
'{{app_name}}' => isset($config['app']) ? ['app'] : 'app-name',
30+
];
31+
$config['message'] = strtr($config['message'], $messagePlaceHolders);
32+
2333
$defaultConfig = [
2434
'channel' => '#general',
2535
'icon' => ':sunny:',
2636
'username' => 'Deploy',
37+
'message' => "Deployment to `{{host}}` on *{{stage}}* was successful\n({{release_path}})",
2738
];
2839

2940
$config = array_merge($defaultConfig, $config);
@@ -50,7 +61,6 @@
5061
}
5162

5263
$url = 'https://slack.com/api/chat.postMessage?' . http_build_query($urlParams);
53-
5464
$result = @file_get_contents($url);
5565

5666
if (!$result) {

0 commit comments

Comments
 (0)