2020-04-25 23:00:08 +03:00
|
|
|
|
<?php
|
2020-10-02 00:11:13 +02:00
|
|
|
|
/*
|
|
|
|
|
## Installing
|
|
|
|
|
|
|
|
|
|
<a href="https://slack.com/oauth/authorize?&client_id=113734341365.225973502034&scope=incoming-webhook"><img alt="Add to Slack" height="40" width="139" src="https://platform.slack-edge.com/img/add_to_slack.png" srcset="https://platform.slack-edge.com/img/add_to_slack.png 1x, https://platform.slack-edge.com/img/add_to_slack@2x.png 2x" /></a>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Add hook on deploy:
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
before('deploy', 'slack:notify');
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Configuration
|
|
|
|
|
|
|
|
|
|
- `slack_webhook` – slack incoming webhook url, **required**
|
|
|
|
|
```
|
|
|
|
|
set('slack_webhook', 'https://hooks.slack.com/...');
|
|
|
|
|
```
|
2021-02-22 14:14:59 +01:00
|
|
|
|
- `slack_channel` - channel to send notification to. The default is the channel configured in the webhook
|
2020-10-02 00:11:13 +02:00
|
|
|
|
- `slack_title` – the title of application, default `{{application}}`
|
|
|
|
|
- `slack_text` – notification message template, markdown supported
|
|
|
|
|
```
|
|
|
|
|
set('slack_text', '_{{user}}_ deploying `{{branch}}` to *{{target}}*');
|
|
|
|
|
```
|
|
|
|
|
- `slack_success_text` – success template, default:
|
|
|
|
|
```
|
|
|
|
|
set('slack_success_text', 'Deploy to *{{target}}* successful');
|
|
|
|
|
```
|
|
|
|
|
- `slack_failure_text` – failure template, default:
|
|
|
|
|
```
|
|
|
|
|
set('slack_failure_text', 'Deploy to *{{target}}* failed');
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
- `slack_color` – color's attachment
|
|
|
|
|
- `slack_success_color` – success color's attachment
|
|
|
|
|
- `slack_failure_color` – failure color's attachment
|
2021-11-30 12:58:07 +01:00
|
|
|
|
- `slack_fields` - set attachments fields for pretty output in Slack, default:
|
|
|
|
|
```
|
|
|
|
|
set('slack_fields', []);
|
|
|
|
|
```
|
2020-04-25 23:00:08 +03:00
|
|
|
|
|
2020-10-02 00:11:13 +02:00
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
|
|
If you want to notify only about beginning of deployment add this line only:
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
before('deploy', 'slack: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', 'slack: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', 'slack:notify:failure');
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
*/
|
2020-04-25 23:00:08 +03:00
|
|
|
|
namespace Deployer;
|
|
|
|
|
|
|
|
|
|
use Deployer\Utility\Httpie;
|
|
|
|
|
|
2021-04-27 07:18:37 +02:00
|
|
|
|
// Channel to publish to, when false the default channel the webhook will be used
|
|
|
|
|
set('slack_channel', false);
|
2021-02-22 14:14:59 +01:00
|
|
|
|
|
2020-04-25 23:00:08 +03:00
|
|
|
|
// Title of project
|
|
|
|
|
set('slack_title', function () {
|
|
|
|
|
return get('application', 'Project');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Deploy message
|
2021-09-26 10:41:25 +02:00
|
|
|
|
set('slack_text', '_{{user}}_ deploying `{{target}}` to *{{hostname}}*');
|
2020-04-25 23:00:08 +03:00
|
|
|
|
set('slack_success_text', 'Deploy to *{{target}}* successful');
|
|
|
|
|
set('slack_failure_text', 'Deploy to *{{target}}* failed');
|
|
|
|
|
set('slack_rollback_text', '_{{user}}_ rolled back changes on *{{target}}*');
|
2021-11-30 12:58:07 +01:00
|
|
|
|
set('slack_fields', []);
|
2020-04-25 23:00:08 +03:00
|
|
|
|
|
|
|
|
|
// Color of attachment
|
|
|
|
|
set('slack_color', '#4d91f7');
|
|
|
|
|
set('slack_success_color', '#00c100');
|
|
|
|
|
set('slack_failure_color', '#ff0909');
|
|
|
|
|
set('slack_rollback_color', '#eba211');
|
|
|
|
|
|
2021-07-23 15:19:29 +02:00
|
|
|
|
function checkSlackAnswer($result)
|
|
|
|
|
{
|
|
|
|
|
if ('invalid_token' === $result) {
|
|
|
|
|
warning('Invalid Slack token');
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-08 22:59:39 +01:00
|
|
|
|
desc('Notifies Slack');
|
2020-04-25 23:00:08 +03:00
|
|
|
|
task('slack:notify', function () {
|
|
|
|
|
if (!get('slack_webhook', false)) {
|
2023-03-02 08:49:57 +01:00
|
|
|
|
warning('No Slack webhook configured');
|
2020-04-25 23:00:08 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$attachment = [
|
|
|
|
|
'title' => get('slack_title'),
|
|
|
|
|
'text' => get('slack_text'),
|
|
|
|
|
'color' => get('slack_color'),
|
|
|
|
|
'mrkdwn_in' => ['text'],
|
|
|
|
|
];
|
|
|
|
|
|
2021-11-07 10:49:16 +01:00
|
|
|
|
$result = Httpie::post(get('slack_webhook'))->jsonBody(['channel' => get('slack_channel'), 'attachments' => [$attachment]])->send();
|
2021-07-23 15:19:29 +02:00
|
|
|
|
checkSlackAnswer($result);
|
2020-04-25 23:00:08 +03:00
|
|
|
|
})
|
|
|
|
|
->once()
|
|
|
|
|
->hidden();
|
|
|
|
|
|
2021-11-08 22:59:39 +01:00
|
|
|
|
desc('Notifies Slack about deploy finish');
|
2020-04-25 23:00:08 +03:00
|
|
|
|
task('slack:notify:success', function () {
|
|
|
|
|
if (!get('slack_webhook', false)) {
|
2023-03-02 08:49:57 +01:00
|
|
|
|
warning('No Slack webhook configured');
|
2020-04-25 23:00:08 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$attachment = [
|
|
|
|
|
'title' => get('slack_title'),
|
|
|
|
|
'text' => get('slack_success_text'),
|
|
|
|
|
'color' => get('slack_success_color'),
|
2021-11-30 12:58:07 +01:00
|
|
|
|
'fields' => get('slack_fields'),
|
2020-04-25 23:00:08 +03:00
|
|
|
|
'mrkdwn_in' => ['text'],
|
|
|
|
|
];
|
|
|
|
|
|
2021-11-07 10:49:16 +01:00
|
|
|
|
$result = Httpie::post(get('slack_webhook'))->jsonBody(['channel' => get('slack_channel'), 'attachments' => [$attachment]])->send();
|
2021-07-23 15:19:29 +02:00
|
|
|
|
checkSlackAnswer($result);
|
2020-04-25 23:00:08 +03:00
|
|
|
|
})
|
|
|
|
|
->once()
|
|
|
|
|
->hidden();
|
|
|
|
|
|
2021-11-08 22:59:39 +01:00
|
|
|
|
desc('Notifies Slack about deploy failure');
|
2020-04-25 23:00:08 +03:00
|
|
|
|
task('slack:notify:failure', function () {
|
|
|
|
|
if (!get('slack_webhook', false)) {
|
2023-03-02 08:49:57 +01:00
|
|
|
|
warning('No Slack webhook configured');
|
2020-04-25 23:00:08 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$attachment = [
|
|
|
|
|
'title' => get('slack_title'),
|
|
|
|
|
'text' => get('slack_failure_text'),
|
|
|
|
|
'color' => get('slack_failure_color'),
|
|
|
|
|
'mrkdwn_in' => ['text'],
|
|
|
|
|
];
|
|
|
|
|
|
2021-11-07 10:49:16 +01:00
|
|
|
|
$result = Httpie::post(get('slack_webhook'))->jsonBody(['channel' => get('slack_channel'), 'attachments' => [$attachment]])->send();
|
2021-07-23 15:19:29 +02:00
|
|
|
|
checkSlackAnswer($result);
|
2020-04-25 23:00:08 +03:00
|
|
|
|
})
|
|
|
|
|
->once()
|
|
|
|
|
->hidden();
|
|
|
|
|
|
2021-11-08 22:59:39 +01:00
|
|
|
|
desc('Notifies Slack about rollback');
|
2020-04-25 23:00:08 +03:00
|
|
|
|
task('slack:notify:rollback', function () {
|
|
|
|
|
if (!get('slack_webhook', false)) {
|
2023-03-02 08:49:57 +01:00
|
|
|
|
warning('No Slack webhook configured');
|
2020-04-25 23:00:08 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$attachment = [
|
|
|
|
|
'title' => get('slack_title'),
|
|
|
|
|
'text' => get('slack_rollback_text'),
|
|
|
|
|
'color' => get('slack_rollback_color'),
|
|
|
|
|
'mrkdwn_in' => ['text'],
|
|
|
|
|
];
|
|
|
|
|
|
2021-11-07 10:49:16 +01:00
|
|
|
|
$result = Httpie::post(get('slack_webhook'))->jsonBody(['channel' => get('slack_channel'), 'attachments' => [$attachment]])->send();
|
2021-07-23 15:19:29 +02:00
|
|
|
|
checkSlackAnswer($result);
|
2020-04-25 23:00:08 +03:00
|
|
|
|
})
|
|
|
|
|
->once()
|
|
|
|
|
->hidden();
|