forked from deployphp/deployer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcimonitor.php
166 lines (138 loc) · 4.49 KB
/
cimonitor.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
<?php
/*
Monitor your deployments on [CIMonitor](https://github.com/CIMonitor/CIMonitor).
![CIMonitorGif](https://www.steefmin.xyz/deployer-example.gif)
Add tasks on deploy:
```php
before('deploy', 'cimonitor:notify');
after('deploy:success', 'cimonitor:notify:success');
after('deploy:failed', 'cimonitor:notify:failure');
```
## Configuration
- `cimonitor_webhook` – CIMonitor server webhook url, **required**
```
set('cimonitor_webhook', 'https://cimonitor.enrise.com/webhook/deployer');
```
- `cimonitor_title` – the title of application, default the username\reponame combination from `{{repository}}`
```
set('cimonitor_title', '');
```
- `cimonitor_user` – User object with name and email, default gets information from `git config`
```
set('cimonitor_user', function () {
return [
'name' => 'John Doe',
'email' => '[email protected]',
];
});
```
Various cimonitor statusses are set, in case you want to change these yourselves. See the [CIMonitor documentation](https://cimonitor.readthedocs.io/en/latest/) for the usages of different states.
## Usage
If you want to notify only about beginning of deployment add this line only:
```php
before('deploy', 'cimonitor:notify');
```
If you want to notify about successful end of deployment add this too:
```php
after('deploy:success', 'cimonitor:notify:success');
```
If you want to notify about failed deployment add this too:
```php
after('deploy:failed', 'cimonitor:notify:failure');
```
*/
namespace Deployer;
use Deployer\Utility\Httpie;
// Title of project based on git repo
set('cimonitor_title', function () {
$repo = get('repository');
$pattern = '/\w+\/\w+/';
return preg_match($pattern, $repo, $titles) ? $titles[0] : $repo;
});
set('cimonitor_user', function () {
return [
'name' => runLocally('git config --get user.name'),
'email' => runLocally('git config --get user.email'),
];
});
// CI monitor status states and job states
set('cimonitor_status_info', 'info');
set('cimonitor_status_warning', 'warning');
set('cimonitor_status_error', 'error');
set('cimonitor_status_success', 'success');
set('cimonitor_job_state_info', get('cimonitor_status_info'));
set('cimonitor_job_state_pending', 'pending');
set('cimonitor_job_state_running', 'running');
set('cimonitor_job_state_warning', get('cimonitor_status_warning'));
set('cimonitor_job_state_error', get('cimonitor_status_error'));
set('cimonitor_job_state_success', get('cimonitor_status_success'));
desc('Notifies CIMonitor');
task('cimonitor:notify', function () {
if (!get('cimonitor_webhook', false)) {
return;
}
$body = [
'state' => get('cimonitor_status_warning'),
'branch' => get('branch'),
'title' => get('cimonitor_title'),
'user' => get('cimonitor_user'),
'stages' => [get('stage', '')],
'jobs' => [
[
'name' => 'Deploying...',
'stage' => '',
'state' => get('cimonitor_job_state_running'),
]
],
];
Httpie::post(get('cimonitor_webhook'))->jsonBody($body)->send();
})
->once()
->hidden();
desc('Notifies CIMonitor about deploy finish');
task('cimonitor:notify:success', function () {
if (!get('cimonitor_webhook', false)) {
return;
}
$depstage = 'Deployed to '.get('stage', '');
$body = [
'state' => get('cimonitor_status_success'),
'branch' => get('branch'),
'title' => get('cimonitor_title'),
'user' => get('cimonitor_user'),
'stages' => [$depstage],
'jobs' => [
[
'name' => 'Deploy',
'stage' => $depstage,
'state' => get('cimonitor_job_state_success'),
]
],
];
Httpie::post(get('cimonitor_webhook'))->jsonBody($body)->send();
})
->once()
->hidden();
desc('Notifies CIMonitor about deploy failure');
task('cimonitor:notify:failure', function () {
if (!get('cimonitor_webhook', false)) {
return;
}
$body = [
'state' => get('cimonitor_status_error'),
'branch' => get('branch'),
'title' => get('cimonitor_title'),
'user' => get('cimonitor_user'),
'stages' => [get('stage', '')],
'jobs' => [
[
'name' => 'Deploy',
'stage' => '',
'state' => get('cimonitor_job_state_error'),
]
],
];
Httpie::post(get('cimonitor_webhook'))->jsonBody($body)->send();
})
->once()
->hidden();