forked from deployphp/deployer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack.php
159 lines (127 loc) · 4.09 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/*
## Installing
<a href="https://slack.com/oauth/authorize?&client_id=113734341365.225973502034&scope=incoming-webhook"><img alt="Add to Slack" height="40" width="139" src="https://platform.slack-edge.com/img/add_to_slack.png" srcset="https://platform.slack-edge.com/img/add_to_slack.png 1x, https://platform.slack-edge.com/img/[email protected] 2x" /></a>
Require slack recipe in your `deploy.php` file:
```php
require 'contrib/slack.php';
```
Add hook on deploy:
```php
before('deploy', 'slack:notify');
```
## Configuration
- `slack_webhook` – slack incoming webhook url, **required**
```
set('slack_webhook', 'https://hooks.slack.com/...');
```
- `slack_title` – the title of application, default `{{application}}`
- `slack_text` – notification message template, markdown supported
```
set('slack_text', '_{{user}}_ deploying `{{branch}}` to *{{target}}*');
```
- `slack_success_text` – success template, default:
```
set('slack_success_text', 'Deploy to *{{target}}* successful');
```
- `slack_failure_text` – failure template, default:
```
set('slack_failure_text', 'Deploy to *{{target}}* failed');
```
- `slack_color` – color's attachment
- `slack_success_color` – success color's attachment
- `slack_failure_color` – failure color's attachment
## Usage
If you want to notify only about beginning of deployment add this line only:
```php
before('deploy', 'slack:notify');
```
If you want to notify about successful end of deployment add this too:
```php
after('success', 'slack:notify:success');
```
If you want to notify about failed deployment add this too:
```php
after('deploy:failed', 'slack:notify:failure');
```
*/
namespace Deployer;
use Deployer\Utility\Httpie;
// Title of project
set('slack_title', function () {
return get('application', 'Project');
});
// Deploy message
set('slack_text', '_{{user}}_ deploying `{{branch}}` to *{{target}}*');
set('slack_success_text', 'Deploy to *{{target}}* successful');
set('slack_failure_text', 'Deploy to *{{target}}* failed');
set('slack_rollback_text', '_{{user}}_ rolled back changes on *{{target}}*');
// Color of attachment
set('slack_color', '#4d91f7');
set('slack_success_color', '#00c100');
set('slack_failure_color', '#ff0909');
set('slack_rollback_color', '#eba211');
desc('Notifying Slack');
task('slack:notify', function () {
if (!get('slack_webhook', false)) {
return;
}
$attachment = [
'title' => get('slack_title'),
'text' => get('slack_text'),
'color' => get('slack_color'),
'mrkdwn_in' => ['text'],
];
Httpie::post(get('slack_webhook'))->body(['attachments' => [$attachment]])->send();
})
->once()
->shallow()
->hidden();
desc('Notifying Slack about deploy finish');
task('slack:notify:success', function () {
if (!get('slack_webhook', false)) {
return;
}
$attachment = [
'title' => get('slack_title'),
'text' => get('slack_success_text'),
'color' => get('slack_success_color'),
'mrkdwn_in' => ['text'],
];
Httpie::post(get('slack_webhook'))->body(['attachments' => [$attachment]])->send();
})
->once()
->shallow()
->hidden();
desc('Notifying Slack about deploy failure');
task('slack:notify:failure', function () {
if (!get('slack_webhook', false)) {
return;
}
$attachment = [
'title' => get('slack_title'),
'text' => get('slack_failure_text'),
'color' => get('slack_failure_color'),
'mrkdwn_in' => ['text'],
];
Httpie::post(get('slack_webhook'))->body(['attachments' => [$attachment]])->send();
})
->once()
->shallow()
->hidden();
desc('Notifying Slack about rollback');
task('slack:notify:rollback', function () {
if (!get('slack_webhook', false)) {
return;
}
$attachment = [
'title' => get('slack_title'),
'text' => get('slack_rollback_text'),
'color' => get('slack_rollback_color'),
'mrkdwn_in' => ['text'],
];
Httpie::post(get('slack_webhook'))->body(['attachments' => [$attachment]])->send();
})
->once()
->shallow()
->hidden();