Skip to content
This repository has been archived by the owner on Apr 26, 2020. It is now read-only.

Commit

Permalink
Add rocketchat integration (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
Baachi authored and antonmedv committed Jun 27, 2019
1 parent f89d424 commit 84b3229
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 0 deletions.
69 changes: 69 additions & 0 deletions docs/rocketchat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# RocketChat Recipe

## Installing

Create a RocketChat incoming webhook, through the administration panel.

Require the new recipe into your `deploy.php`

```php
require 'recipe/rocketchat.php';
```

Add hook on deploy:

```
before('deploy', 'rocketchat:notify');
```

## Configuration

- `rocketchat_webhook` - incoming rocketchat webook **required**
```
set('rocketchat_webook', 'https://rocketchat.yourcompany.com/hooks/XXXXX');
```

- `rocketchat_title` - the title of the application, defaults to `{{application}}`
- `rocketchat_text` - notification message
```
set('rocketchat_text', '_{{user}}_ deploying {{branch}} to {{target}}');
```

- `rocketchat_success_text` – success template, default:
```
set('rocketchat_success_text', 'Deploy to *{{target}}* successful');
```
- `rocketchat_failure_text` – failure template, default:
```
set('rocketchat_failure_text', 'Deploy to *{{target}}* failed');
```

- `rocketchat_color` – color's attachment
- `rocketchat_success_color` – success color's attachment
- `rocketchat_failure_color` – failure color's attachment

## Tasks

- `rocketchat:notify` – send message to rocketchat
- `rocketchat:notify:success` – send success message to rocketchat
- `rocketchat:notify:failure` – send failure message to rocketchat

## Usage

If you want to notify only about beginning of deployment add this line only:

```php
before('deploy', 'rocketchat:notify');
```

If you want to notify about successful end of deployment add this too:

```php
after('success', 'rocketchat:notify:success');
```

If you want to notify about failed deployment add this too:

```php
after('deploy:failed', 'rocketchat:notify:failure');
```
120 changes: 120 additions & 0 deletions recipe/rocketchat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php
/* (c) Markus Bachmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Deployer;

use Deployer\Utility\Httpie;

set('rockchat_title', function() {
return get('application', 'Project');
});

set('rocketchat_icon_emoji', ':robot:');
set('rocketchat_icon_url', null);

set('rocketchat_channel', null);
set('rocketchat_room_id', null);
set('rocketchat_username', null);
set('rocketchat_webhook', null);

set('rocketchat_color', '#000000');
set('rocketchat_success_color', '#00c100');
set('rocketchat_failure_color', '#ff0909');

set('rocketchat_text', '_{{user}}_ deploying `{{branch}}` to *{{target}}*');
set('rocketchat_success_text', 'Deploy to *{{target}}* successful');
set('rocketchat_failure_text', 'Deploy to *{{target}}* failed');

desc('Notify RocketChat');
task('rocketchat:notify', function() {
if (null === get('rocketchat_webhook')) {
return;
}

$body = [
'text' => get('rockchat_title'),
'username' => get('rocketchat_username'),
'attachments' => [[
'text' => get('rocketchat_text'),
'color' => get('rocketchat_color'),
]]
];

if (get('rocketchat_channel')) {
$body['channel'] = get('rocketchat_channel');
}
if (get('rocketchat_room_id')) {
$body['roomId'] = get('rocketchat_room_id');
}
if (get('rocketchat_icon_url')) {
$body['avatar'] = get('rocketchat_icon_url');
} elseif (get('rocketchat_icon_emoji')) {
$body['emoji'] = get('rocketchat_icon_emoji');
}

Httpie::post(get('rocketchat_webhook'))->body($body)->send();
});

desc('Notifying RocketChat about deploy finish');
task('rocketchat:notify:success', function() {
if (null === get('rocketchat_webhook')) {
return;
}

$body = [
'text' => get('rockchat_title'),
'username' => get('rocketchat_username'),
'attachments' => [[
'text' => get('rocketchat_success_text'),
'color' => get('rocketchat_success_color'),
]]
];

if (get('rocketchat_channel')) {
$body['channel'] = get('rocketchat_channel');
}
if (get('rocketchat_room_id')) {
$body['roomId'] = get('rocketchat_room_id');
}
if (get('rocketchat_icon_url')) {
$body['avatar'] = get('rocketchat_icon_url');
} elseif (get('rocketchat_icon_emoji')) {
$body['emoji'] = get('rocketchat_icon_emoji');
}

Httpie::post(get('rocketchat_webhook'))->body($body)->send();
});

desc('Notifying RocketChat about deploy failure');
task('rocketchat:notify:failure', function() {
if (null === get('rocketchat_webhook')) {
return;
}

$body = [
'text' => get('rockchat_title'),
'username' => get('rocketchat_username'),
'attachments' => [[
'color' => get('rocketchat_failure_color'),
'text' => get('rocketchat_failure_text')
]]
];

if (get('rocketchat_channel')) {
$body['channel'] = get('rocketchat_channel');
}
if (get('rocketchat_room_id')) {
$body['roomId'] = get('rocketchat_room_id');
}
if (get('rocketchat_icon_url')) {
$body['avatar'] = get('rocketchat_icon_url');
} elseif (get('rocketchat_icon_emoji')) {
$body['emoji'] = get('rocketchat_icon_emoji');
}

Httpie::post(get('rocketchat_webhook'))->body($body)->send();
});

0 comments on commit 84b3229

Please sign in to comment.