forked from deployphp/deployer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmattermost.php
159 lines (120 loc) · 4.12 KB
/
mattermost.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
Create a Mattermost incoming webhook, through the administration panel.
Require the new recipe into your `deploy.php`
```php
require 'contrib/mattermost.php';
```
Add hook on deploy:
```
before('deploy', 'mattermost:notify');
```
## Configuration
- `mattermost_webhook` - incoming mattermost webook **required**
```
set('mattermost_webook', 'https://{your-mattermost-site}/hooks/xxx-generatedkey-xxx');
```
- `mattermost_channel` - overrides the channel the message posts in
```
set('mattermost_channel', 'town-square');
```
- `mattermost_username` - overrides the username the message posts as
```
set('mattermost_username', 'deployer');
```
- `mattermost_icon_url` - overrides the profile picture the message posts with
```
set('mattermost_icon_url', 'https://domain.com/your-icon.png');
```
- `mattermost_text` - notification message
```
set('mattermost_text', '_{{user}}_ deploying `{{branch}}` to **{{target}}**');
```
- `mattermost_success_text` – success template, default:
```
set('mattermost_success_text', 'Deploy to **{{target}}** successful {{mattermost_success_emoji}}');
```
- `mattermost_failure_text` – failure template, default:
```
set('mattermost_failure_text', 'Deploy to **{{target}}** failed {{mattermost_failure_emoji}}');
```
- `mattermost_success_emoji` – emoji added at the end of success text
- `mattermost_failure_emoji` – emoji added at the end of failure text
For detailed information about Mattermost hooks see: https://developers.mattermost.com/integrate/incoming-webhooks/
## Usage
If you want to notify only about beginning of deployment add this line only:
```php
before('deploy', 'mattermost:notify');
```
If you want to notify about successful end of deployment add this too:
```php
after('success', 'mattermost:notify:success');
```
If you want to notify about failed deployment add this too:
```php
after('deploy:failed', 'mattermost:notify:failure');
```
*/
namespace Deployer;
use Deployer\Utility\Httpie;
set('mattermost_webhook', null);
set('mattermost_channel', null);
set('mattermost_username', 'deployer');
set('mattermost_icon_url', null);
set('mattermost_success_emoji', ':white_check_mark:');
set('mattermost_failure_emoji', ':x:');
set('mattermost_text', '_{{user}}_ deploying `{{branch}}` to **{{target}}**');
set('mattermost_success_text', 'Deploy to **{{target}}** successful {{mattermost_success_emoji}}');
set('mattermost_failure_text', 'Deploy to **{{target}}** failed {{mattermost_failure_emoji}}');
desc('Notify mattermost');
task('mattermost:notify', function() {
if (null === get('mattermost_webhook')) {
return;
}
$body = [
'text' => get('mattermost_text'),
'username' => get('mattermost_username'),
];
if (get('mattermost_channel')) {
$body['channel'] = get('mattermost_channel');
}
if (get('mattermost_icon_url')) {
$body['icon_url'] = get('mattermost_icon_url');
}
Httpie::post(get('mattermost_webhook'))->body($body)->send();
});
desc('Notifying mattermost about deploy finish');
task('mattermost:notify:success', function() {
if (null === get('mattermost_webhook')) {
return;
}
$body = [
'text' => get('mattermost_success_text'),
'username' => get('mattermost_username'),
];
if (get('mattermost_channel')) {
$body['channel'] = get('mattermost_channel');
}
if (get('mattermost_icon_url')) {
$body['icon_url'] = get('mattermost_icon_url');
}
Httpie::post(get('mattermost_webhook'))->body($body)->send();
});
desc('Notifying mattermost about deploy failure');
task('mattermost:notify:failure', function() {
if (null === get('mattermost_webhook')) {
return;
}
$body = [
'text' => get('mattermost_failure_text'),
'username' => get('mattermost_username'),
];
if (get('mattermost_channel')) {
$body['channel'] = get('mattermost_channel');
}
if (get('mattermost_icon_url')) {
$body['icon_url'] = get('mattermost_icon_url');
}
Httpie::post(get('mattermost_webhook'))->body($body)->send();
});