Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send Slack Notification on Deployment #78

Merged
merged 35 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
f16e559
Add Slack Webhook URL config setting
shawnhooper Mar 3, 2024
af89d05
Configure Slack Announcement
shawnhooper Mar 3, 2024
9d66452
Register the Notification Service Provider
shawnhooper Mar 3, 2024
69ba69f
Remove debug code
shawnhooper Mar 3, 2024
45c03b4
Try it with the bus service provider
shawnhooper Mar 3, 2024
d3f91d6
Reference notification by class name
shawnhooper Mar 3, 2024
d113c6b
Specify Channel Name
shawnhooper Mar 3, 2024
2697e61
Pass channel as "route"
shawnhooper Mar 3, 2024
f011f5a
Pass channel as "route"
shawnhooper Mar 3, 2024
5602c24
Pass channel as "route"
shawnhooper Mar 3, 2024
c562ceb
DEbug
shawnhooper Mar 3, 2024
8d99a9b
DEbug
shawnhooper Mar 3, 2024
ee365a4
DEbug
shawnhooper Mar 3, 2024
8ce2db5
DEbug
shawnhooper Mar 3, 2024
588fa77
DEbug
shawnhooper Mar 3, 2024
5369e29
Use NotifyNow so we don't need the bus
shawnhooper Mar 3, 2024
f606a57
Get it right from the env
shawnhooper Mar 3, 2024
739507d
Manual token ID
shawnhooper Mar 3, 2024
63b4d1f
Add SLACK_ANNOUNCEMENT_ENABLED support
shawnhooper Mar 3, 2024
cd23205
Dump the token length
shawnhooper Mar 3, 2024
1aaa95c
Dump the token length
shawnhooper Mar 3, 2024
367e6ca
Fix title text
shawnhooper Mar 3, 2024
b4b1e96
Fix title text
shawnhooper Mar 3, 2024
88ae950
Fix title text
shawnhooper Mar 3, 2024
4bb3d22
Fix title text
shawnhooper Mar 3, 2024
3567c84
Use default slack bot env variables
shawnhooper Mar 3, 2024
1ab4915
Fix branch name display
shawnhooper Mar 3, 2024
41760d1
Only announce once
shawnhooper Mar 3, 2024
978a444
Link to correct branch
shawnhooper Mar 3, 2024
b373214
Only display branch name
shawnhooper Mar 3, 2024
743b8ec
Merge remote-tracking branch 'upstream/main' into slack
shawnhooper Mar 3, 2024
c9d73fb
Removed slack token & channel condition check
mehrancodes Mar 5, 2024
c7f3731
Made the class strict
mehrancodes Mar 5, 2024
f9db886
Run Pint
mehrancodes Mar 5, 2024
7a18338
Updated AnnounceSiteOnSlack comment
mehrancodes Mar 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/Commands/ProvisionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace App\Commands;

use App\Services\Forge\ForgeService;
use App\Services\Forge\Pipeline\AnnounceSiteOnSlack;
use App\Services\Forge\Pipeline\CreateDatabase;
use App\Services\Forge\Pipeline\DeploySite;
use App\Services\Forge\Pipeline\EnableQuickDeploy;
Expand Down Expand Up @@ -58,6 +59,7 @@ public function handle(ForgeService $service): void
RunOptionalCommands::class,
EnsureJobScheduled::class,
PutCommentOnPullRequest::class,
AnnounceSiteOnSlack::class,
])
->then(function () use ($service) {
$this->success('Provisioning complete! Your environment is now set up and ready to use.');
Expand Down
50 changes: 50 additions & 0 deletions app/Notifications/SiteProvisionedNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

/**
* This file is part of Laravel Harbor.
*
* (c) Mehran Rasulian <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App\Notifications;

use App\Services\Forge\ForgeService;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Slack\BlockKit\Blocks\SectionBlock;
use Illuminate\Notifications\Slack\SlackChannel;
use Illuminate\Notifications\Slack\SlackMessage;

class SiteProvisionedNotification extends Notification
{
public function __construct(protected ForgeService $service)
{
//
}

public function via(object $notifiable): array
{
return [SlackChannel::class];
}

public function toSlack(object $notifiable): SlackMessage
{
return (new SlackMessage)
->to($this->service->setting->slackChannel)
->text('A new site has been provisioned!')
->sectionBlock(function (SectionBlock $block) {
$block->text('An preview site has been deployed with Laravel Harbor');

$block->field("*Branch Name:*\n{$this->service->setting->branch}")->markdown();
$block->field("*Environment URL:*\n{$this->service->getSiteLink()}")->markdown();
})
->dividerBlock()
->sectionBlock(function (SectionBlock $block) {
$block->text('Congratulations!');
});
}
}
18 changes: 18 additions & 0 deletions app/Services/Forge/ForgeSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,21 @@ class ForgeSetting
*/
public ?string $environmentUrl;

/**
* Is the Slack integration enabled?
*/
public bool $slackAnnouncementEnabled;

/**
* The OAuth Token for the Slack Bot
*/
public ?string $slackBotUserOauthToken;

/**
* Which channel to announce to on Slack
*/
public ?string $slackChannel;

/**
* The validation rules.
*/
Expand Down Expand Up @@ -191,6 +206,9 @@ class ForgeSetting
'git_token' => ['exclude_if:git_comment_enabled,false', 'required', 'string'],
'subdomain_name' => ['nullable', 'string', 'regex:/^[a-zA-Z0-9-_]+$/'],
'environment_url' => ['nullable', 'url'],
'slack_announcement_enabled' => ['required', 'boolean'],
'slack_bot_user_oauth_token' => ['exclude_if:slack_announcement_enabled,false', 'required', 'string'],
'slack_channel' => ['exclude_if:slack_announcement_enabled,false', 'required', 'string'],
];

public function __construct()
Expand Down
52 changes: 52 additions & 0 deletions app/Services/Forge/Pipeline/AnnounceSiteOnSlack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

/**
* This file is part of Laravel Harbor.
*
* (c) Mehran Rasulian <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App\Services\Forge\Pipeline;

use App\Notifications\SiteProvisionedNotification;
use App\Services\Forge\ForgeService;
use App\Traits\Outputifier;
use Closure;
use Illuminate\Notifications\Slack\SlackChannel;
use Illuminate\Notifications\Slack\SlackRoute;
use Illuminate\Support\Facades\Notification;

class AnnounceSiteOnSlack
{
use Outputifier;

public function __invoke(ForgeService $service, Closure $next)
{
// End early if the slack bot token and channel are not set in the Forge service settings
if (! $service->setting->slackAnnouncementEnabled) {
return $next($service);
}

if (! $service->siteNewlyMade) {
$this->information('Skipping Slack announcement as the site is not newly made.');

return $next($service);
}

$this->information('Announce the site on Slack.');

Notification::route(
SlackChannel::class,
new SlackRoute()
)->notify(
(new SiteProvisionedNotification($service))
);

return $next($service);
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"illuminate/validation": "^10.24",
"laravel-zero/framework": "^10.0.2",
"laravel/forge-sdk": "^3.13",
"laravel/slack-notification-channel": "^3.2",
"lorisleiva/laravel-actions": "^2.7",
"nunomaduro/termwind": "^1.15"
},
Expand Down
Loading
Loading