forked from deployphp/deployer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatwork.php
171 lines (144 loc) · 4.86 KB
/
chatwork.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
<?php
/*
# Chatwork Recipe
## Installing
1. Create chatwork account by any manual in the internet
2. Take chatwork token (Like: b29a700e2d15bef3f26ae6a5c142d1ea) and set `chatwork_token` parameter
3. Take chatwork room id from url after clicked on the room, and set `chatwork_room_id` parameter
4. If you want, you can edit `chatwork_notify_text`, `chatwork_success_text` or `chatwork_failure_text`
5. Require chatwork recipe in your `deploy.php` file
```php
# https://deployer.org/recipes.html
require 'recipe/chatwork.php';
```
Add hook on deploy:
```php
before('deploy', 'chatwork:notify');
```
## Configuration
- `chatwork_token` – chatwork bot token, **required**
- `chatwork_room_id` — chatwork room to push messages to **required**
- `chatwork_notify_text` – notification message template
```
[info]
[title](*) Deployment Status: Deploying[/title]
Repo: {{repository}}
Branch: {{branch}}
Server: {{hostname}}
Release Path: {{release_path}}
Current Path: {{current_path}}
[/info]
```
- `chatwork_success_text` – success template, default:
```
[info]
[title](*) Deployment Status: Successfully[/title]
Repo: {{repository}}
Branch: {{branch}}
Server: {{hostname}}
Release Path: {{release_path}}
Current Path: {{current_path}}
[/info]"
```
- `chatwork_failure_text` – failure template, default:
```
[info]
[title](*) Deployment Status: Failed[/title]
Repo: {{repository}}
Branch: {{branch}}
Server: {{hostname}}
Release Path: {{release_path}}
Current Path: {{current_path}}
[/info]"
```
## Tasks
- `chatwork:notify` – send message to chatwork
- `chatwork:notify:success` – send success message to chatwork
- `chatwork:notify:failure` – send failure message to chatwork
## Usage
If you want to notify only about beginning of deployment add this line only:
```php
before('deploy', 'chatwork:notify');
```
If you want to notify about successful end of deployment add this too:
```php
after('success', 'chatwork:notify:success');
```
If you want to notify about failed deployment add this too:
```php
after('deploy:failed', 'chatwork:notify:failure');
```
*/
namespace Deployer;
use Deployer\Utility\Httpie;
// Chatwork settings
set('chatwork_token', function () {
throw new \RuntimeException('Please configure "chatwork_token" parameter.');
});
set('chatwork_room_id', function () {
throw new \RuntimeException('Please configure "chatwork_room_id" parameter.');
});
set('chatwork_api', function () {
return 'https://api.chatwork.com/v2/rooms/' . get('chatwork_room_id') . '/messages';
});
// The Messages
set('chatwork_notify_text', "[info]\n[title](*) Deployment Status: Deploying[/title]\nRepo: {{repository}}\nBranch: {{branch}}\nServer: {{hostname}}\nRelease Path: {{release_path}}\nCurrent Path: {{current_path}}\n[/info]");
set('chatwork_success_text', "[info]\n[title](*) Deployment Status: Successfully[/title]\nRepo: {{repository}}\nBranch: {{branch}}\nServer: {{hostname}}\nRelease Path: {{release_path}}\nCurrent Path: {{current_path}}\n[/info]");
set('chatwork_failure_text', "[info]\n[title](*) Deployment Status: Failed[/title]\nRepo: {{repository}}\nBranch: {{branch}}\nServer: {{hostname}}\nRelease Path: {{release_path}}\nCurrent Path: {{current_path}}\n[/info]");
// Helpers
task('chatwork_send_message', function() {
Httpie::post(get('chatwork_api'))
->query(['body' => get('chatwork_message'),])
->header("X-ChatWorkToken", get('chatwork_token'))
->send();
});
// Tasks
desc('Tests messages');
task('chatwork:test', function () {
set('chatwork_message', get('chatwork_notify_text'));
invoke('chatwork_send_message');
set('chatwork_message', get('chatwork_success_text'));
invoke('chatwork_send_message');
set('chatwork_message', get('chatwork_failure_text'));
invoke('chatwork_send_message');
})
->once();
desc('Notifies Chatwork');
task('chatwork:notify', function () {
if (!get('chatwork_token', false)) {
return;
}
if (!get('chatwork_room_id', false)) {
return;
}
set('chatwork_message', get('chatwork_notify_text'));
invoke('chatwork_send_message');
})
->once()
->hidden();
desc('Notifies Chatwork about deploy finish');
task('chatwork:notify:success', function () {
if (!get('chatwork_token', false)) {
return;
}
if (!get('chatwork_room_id', false)) {
return;
}
set('chatwork_message', get('chatwork_success_text'));
invoke('chatwork_send_message');
})
->once()
->hidden();
desc('Notifies Chatwork about deploy failure');
task('chatwork:notify:failure', function () {
if (!get('chatwork_token', false)) {
return;
}
if (!get('chatwork_room_id', false)) {
return;
}
set('chatwork_message', get('chatwork_failure_text'));
invoke('chatwork_send_message');
})
->once()
->hidden();