forked from deployphp/deployer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangouts.php
183 lines (157 loc) · 5.31 KB
/
hangouts.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/*
Add hook on deploy:
```php
before('deploy', 'chat:notify');
```
## Configuration
- `chat_webhook` – chat incoming webhook url, **required**
- `chat_title` – the title of your notification card, default `{{application}}`
- `chat_subtitle` – the subtitle of your card, default `{{hostname}}`
- `chat_favicon` – an image for the header of your card, default `http://{{hostname}}/favicon.png`
- `chat_line1` – first line of the text in your card, default: `{{branch}}`
- `chat_line2` – second line of the text in your card, default: `{{stage}}`
## Usage
If you want to notify only about beginning of deployment add this line only:
```php
before('deploy', 'chat:notify');
```
If you want to notify about successful end of deployment add this too:
```php
after('deploy:success', 'chat:notify:success');
```
If you want to notify about failed deployment add this too:
```php
after('deploy:failed', 'chat:notify:failure');
```
*/
namespace Deployer;
use Deployer\Utility\Httpie;
// Title of project
set('chat_title', function () {
return get('application', 'Project');
});
set('chat_subtitle', get('hostname'));
// If 'favicon' is set Google Hangouts Chat will decorate your card with an image.
set('favicon', 'http://{{hostname}}/favicon.png');
// Deploy messages
set('chat_line1', '{{branch}}');
set('chat_line2', '{{stage}}');
desc('Notifies Google Hangouts Chat');
task('chat:notify', function () {
if (!get('chat_webhook', false)) {
return;
}
$card = [
'header' => [
'title' => get('chat_title'),
'subtitle' => get('chat_subtitle'),
'imageUrl' => get('favicon'),
'imageStyle' => 'IMAGE'
],
'sections' => [
'widgets' => [
'keyValue' => [
'topLabel' => get('chat_line1'),
'content' => get('chat_line2'),
'contentMultiline' => false,
'bottomLabel' => 'started',
// Use 'iconUrl' to set a custom icon URL (png)
'icon' => 'CLOCK',
'button' => [
'textButton' => [
'text' => 'Visit site',
'onClick' => [
'openLink' => [
'url' => get('hostname')
]
]
]
]
]
]
]
];
Httpie::post(get('chat_webhook'))->jsonBody(['cards' => $card])->send();
})
->once()
->hidden();
desc('Notifies Google Hangouts Chat about deploy finish');
task('chat:notify:success', function () {
if (!get('chat_webhook', false)) {
return;
}
$card = [
'header' => [
'title' => get('chat_title'),
'subtitle' => get('chat_subtitle'),
'imageUrl' => get('favicon'),
'imageStyle' => 'IMAGE'
],
'sections' => [
'widgets' => [
'keyValue' => [
'topLabel' => get('chat_line1'),
'content' => get('chat_line2'),
'contentMultiline' => false,
'bottomLabel' => 'succeeded',
// Use 'iconUrl' to set a custom icon URL (png)
'icon' => 'STAR',
'button' => [
'textButton' => [
'text' => 'Visit site',
'onClick' => [
'openLink' => [
'url' => get('hostname')
]
]
]
]
]
]
]
];
Httpie::post(get('chat_webhook'))->jsonBody(['cards' => $card])->send();
})
->once()
->hidden();
desc('Notifies Google Hangouts Chat about deploy failure');
task('chat:notify:failure', function () {
if (!get('chat_webhook', false)) {
return;
}
$card = [
'header' => [
'title' => get('chat_title'),
'subtitle' => get('chat_subtitle'),
'imageUrl' => get('favicon'),
'imageStyle' => 'IMAGE'
],
'sections' => [
'widgets' => [
'keyValue' => [
'topLabel' => get('chat_line1'),
'content' => get('chat_line2'),
'contentMultiline' => false,
'bottomLabel' => 'failed',
// Use 'iconUrl' to set a custom icon URL (png)
// or use 'icon' and pick from this list:
// https://developers.google.com/hangouts/chat/reference/message-formats/cards#customicons
'button' => [
'textButton' => [
'text' => 'Visit site',
'onClick' => [
'openLink' => [
'url' => get('hostname')
]
]
]
]
]
]
]
];
Httpie::post(get('chat_webhook'))->jsonBody(['cards' => $card])->send();
})
->once()
->hidden();