-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathworkplace.php
More file actions
122 lines (97 loc) · 3.42 KB
/
Copy pathworkplace.php
File metadata and controls
122 lines (97 loc) · 3.42 KB
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
<?php
/*
This recipes works with Custom Integrations and Publishing Bots.
Add hook on deploy:
```
before('deploy', 'workplace:notify');
```
## Configuration
- `workplace_webhook` - incoming workplace webhook **required**
```
// With custom integration
set('workplace_webhook', 'https://graph.facebook.com/<GROUP_ID>/feed?access_token=<ACCESS_TOKEN>');
// With publishing bot
set('workplace_webhook', 'https://graph.facebook.com/v3.0/group/feed?access_token=<ACCESS_TOKEN>');
// Use markdown on message
set('workplace_webhook', 'https://graph.facebook.com/<GROUP_ID>/feed?access_token=<ACCESS_TOKEN>&formatting=MARKDOWN');
```
- `workplace_text` - notification message
```
set('workplace_text', '_{{user}}_ deploying `{{what}}` to *{{where}}*');
```
- `workplace_success_text` – success template, default:
```
set('workplace_success_text', 'Deploy to *{{where}}* successful');
```
- `workplace_failure_text` – failure template, default:
```
set('workplace_failure_text', 'Deploy to *{{where}}* failed');
```
- `workplace_edit_post` – whether to create a new post for deploy result, or edit the first one created, default creates a new post:
```
set('workplace_edit_post', false);
```
## Usage
If you want to notify only about beginning of deployment add this line only:
```php
before('deploy', 'workplace:notify');
```
If you want to notify about successful end of deployment add this too:
```php
after('deploy:success', 'workplace:notify:success');
```
If you want to notify about failed deployment add this too:
```php
after('deploy:failed', 'workplace:notify:failure');
```
*/
namespace Deployer;
use Deployer\Utility\Httpie;
// Deploy message
set('workplace_text', '_{{user}}_ deploying `{{what}}` to *{{where}}*');
set('workplace_success_text', 'Deploy to *{{where}}* successful');
set('workplace_failure_text', 'Deploy to *{{where}}* failed');
// By default, create a new post for every message
set('workplace_edit_post', false);
desc('Notifies Workplace');
task('workplace:notify', function () {
if (!get('workplace_webhook', false)) {
return;
}
$url = get('workplace_webhook') . '&message=' . urlencode(get('workplace_text'));
$response = Httpie::post($url)->getJson();
if (get('workplace_edit_post', false)) {
// Endpoint will be something like: https//graph.facebook.com/<POST_ID>?<QUERY_PARAMS>
$url = sprintf(
'%s://%s/%s?%s',
parse_url(get('workplace_webhook'), PHP_URL_SCHEME),
parse_url(get('workplace_webhook'), PHP_URL_HOST),
$response['id'],
parse_url(get('workplace_webhook'), PHP_URL_QUERY),
);
// Replace the webhook with a url that points to the created post
set('workplace_webhook', $url);
}
})
->once()
->hidden();
desc('Notifies Workplace about deploy finish');
task('workplace:notify:success', function () {
if (!get('workplace_webhook', false)) {
return;
}
$url = get('workplace_webhook') . '&message=' . urlencode(get('workplace_success_text'));
Httpie::post($url)->send();
})
->once()
->hidden();
desc('Notifies Workplace about deploy failure');
task('workplace:notify:failure', function () {
if (!get('workplace_webhook', false)) {
return;
}
$url = get('workplace_webhook') . '&message=' . urlencode(get('workplace_failure_text'));
Httpie::post($url)->send();
})
->once()
->hidden();