deployer/contrib/cimonitor.php

167 lines
4.5 KiB
PHP
Raw Normal View History

2020-04-25 23:00:08 +03:00
<?php
2020-10-02 00:11:13 +02:00
/*
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');
2020-10-29 23:16:36 +01:00
after('deploy:success', 'cimonitor:notify:success');
2020-10-02 00:11:13 +02:00
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' => 'john@enrise.com',
];
});
```
2020-04-25 23:00:08 +03:00
2020-10-02 00:11:13 +02:00
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
2020-10-29 23:16:36 +01:00
after('deploy:success', 'cimonitor:notify:success');
2020-10-02 00:11:13 +02:00
```
If you want to notify about failed deployment add this too:
```php
after('deploy:failed', 'cimonitor:notify:failure');
```
*/
2020-04-25 23:00:08 +03:00
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'));
2021-11-08 22:59:39 +01:00
desc('Notifies CIMonitor');
2020-04-25 23:00:08 +03:00
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'),
]
],
];
2021-11-07 10:49:16 +01:00
Httpie::post(get('cimonitor_webhook'))->jsonBody($body)->send();
2020-04-25 23:00:08 +03:00
})
->once()
->hidden();
2021-11-08 22:59:39 +01:00
desc('Notifies CIMonitor about deploy finish');
2020-04-25 23:00:08 +03:00
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'),
]
],
];
2021-11-07 10:49:16 +01:00
Httpie::post(get('cimonitor_webhook'))->jsonBody($body)->send();
2020-04-25 23:00:08 +03:00
})
->once()
->hidden();
2021-11-08 22:59:39 +01:00
desc('Notifies CIMonitor about deploy failure');
2020-04-25 23:00:08 +03:00
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'),
]
],
];
2021-11-07 10:49:16 +01:00
Httpie::post(get('cimonitor_webhook'))->jsonBody($body)->send();
2020-04-25 23:00:08 +03:00
})
->once()
->hidden();