deployer/contrib/hangouts.php

184 lines
5.3 KiB
PHP
Raw Normal View History

2020-04-25 23:00:08 +03:00
<?php
2020-10-02 00:11:13 +02:00
/*
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
2020-04-25 23:00:08 +03:00
2020-10-02 00:11:13 +02:00
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
2020-10-29 23:16:36 +01:00
after('deploy:success', 'chat: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', 'chat:notify:failure');
```
*/
2020-04-25 23:00:08 +03:00
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}}');
2021-11-08 22:59:39 +01:00
desc('Notifies Google Hangouts Chat');
2020-04-25 23:00:08 +03:00
task('chat:notify', function () {
if (!get('chat_webhook', false)) {
return;
}
$card = [
'header' => [
2021-11-08 22:59:39 +01:00
'title' => get('chat_title'),
'subtitle' => get('chat_subtitle'),
'imageUrl' => get('favicon'),
2020-04-25 23:00:08 +03:00
'imageStyle' => 'IMAGE'
],
'sections' => [
'widgets' => [
'keyValue' => [
2021-11-08 22:59:39 +01:00
'topLabel' => get('chat_line1'),
'content' => get('chat_line2'),
2020-04-25 23:00:08 +03:00
'contentMultiline' => false,
2021-11-08 22:59:39 +01:00
'bottomLabel' => 'started',
2020-04-25 23:00:08 +03:00
// Use 'iconUrl' to set a custom icon URL (png)
2021-11-08 22:59:39 +01:00
'icon' => 'CLOCK',
'button' => [
2020-04-25 23:00:08 +03:00
'textButton' => [
2021-11-08 22:59:39 +01:00
'text' => 'Visit site',
2020-04-25 23:00:08 +03:00
'onClick' => [
'openLink' => [
'url' => get('hostname')
]
]
]
]
]
]
]
];
2021-11-07 10:49:16 +01:00
Httpie::post(get('chat_webhook'))->jsonBody(['cards' => $card])->send();
2020-04-25 23:00:08 +03:00
})
->once()
->hidden();
2021-11-08 22:59:39 +01:00
desc('Notifies Google Hangouts Chat about deploy finish');
2020-04-25 23:00:08 +03:00
task('chat:notify:success', function () {
if (!get('chat_webhook', false)) {
return;
}
$card = [
'header' => [
2021-11-08 22:59:39 +01:00
'title' => get('chat_title'),
'subtitle' => get('chat_subtitle'),
'imageUrl' => get('favicon'),
2020-04-25 23:00:08 +03:00
'imageStyle' => 'IMAGE'
],
'sections' => [
'widgets' => [
'keyValue' => [
2021-11-08 22:59:39 +01:00
'topLabel' => get('chat_line1'),
'content' => get('chat_line2'),
2020-04-25 23:00:08 +03:00
'contentMultiline' => false,
2021-11-08 22:59:39 +01:00
'bottomLabel' => 'succeeded',
2020-04-25 23:00:08 +03:00
// Use 'iconUrl' to set a custom icon URL (png)
2021-11-08 22:59:39 +01:00
'icon' => 'STAR',
'button' => [
2020-04-25 23:00:08 +03:00
'textButton' => [
2021-11-08 22:59:39 +01:00
'text' => 'Visit site',
2020-04-25 23:00:08 +03:00
'onClick' => [
'openLink' => [
'url' => get('hostname')
]
]
]
]
]
]
]
];
2021-11-07 10:49:16 +01:00
Httpie::post(get('chat_webhook'))->jsonBody(['cards' => $card])->send();
2020-04-25 23:00:08 +03:00
})
->once()
->hidden();
2021-11-08 22:59:39 +01:00
desc('Notifies Google Hangouts Chat about deploy failure');
2020-04-25 23:00:08 +03:00
task('chat:notify:failure', function () {
if (!get('chat_webhook', false)) {
return;
}
$card = [
'header' => [
2021-11-08 22:59:39 +01:00
'title' => get('chat_title'),
'subtitle' => get('chat_subtitle'),
'imageUrl' => get('favicon'),
2020-04-25 23:00:08 +03:00
'imageStyle' => 'IMAGE'
],
'sections' => [
'widgets' => [
'keyValue' => [
2021-11-08 22:59:39 +01:00
'topLabel' => get('chat_line1'),
'content' => get('chat_line2'),
2020-04-25 23:00:08 +03:00
'contentMultiline' => false,
2021-11-08 22:59:39 +01:00
'bottomLabel' => 'failed',
2020-04-25 23:00:08 +03:00
// 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
2021-11-08 22:59:39 +01:00
'button' => [
2020-04-25 23:00:08 +03:00
'textButton' => [
2021-11-08 22:59:39 +01:00
'text' => 'Visit site',
2020-04-25 23:00:08 +03:00
'onClick' => [
'openLink' => [
'url' => get('hostname')
]
]
]
]
]
]
]
];
2021-11-07 10:49:16 +01:00
Httpie::post(get('chat_webhook'))->jsonBody(['cards' => $card])->send();
2020-04-25 23:00:08 +03:00
})
->once()
->hidden();