Skip to content

Commit 188f900

Browse files
committed
🚧 Auto deploy with token
1 parent 8cc7c90 commit 188f900

11 files changed

+228
-111
lines changed

composer.json

+22-22
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
{
2-
"name": "juzaweb/auto-deploy",
3-
"description": "Automate deployment deployment using git and webhooks",
4-
"extra": {
5-
"laravel": {
6-
"providers": [
7-
"Juzaweb\\AutoDeploy\\Providers\\AutoDeployServiceProvider"
8-
]
9-
},
10-
"juzaweb": {
11-
"name": "Automate deployment",
12-
"domain": "deploy",
13-
"cms_min": "3.3",
14-
"version": "1.0"
15-
}
16-
},
17-
"autoload": {
18-
"psr-4": {
19-
"Juzaweb\\AutoDeploy\\": "src/"
20-
}
21-
}
22-
}
1+
{
2+
"name": "juzaweb/auto-deploy",
3+
"description": "Automate deployment deployment using git and webhooks",
4+
"extra": {
5+
"laravel": {
6+
"providers": [
7+
"Juzaweb\\AutoDeploy\\Providers\\AutoDeployServiceProvider"
8+
]
9+
},
10+
"juzaweb": {
11+
"name": "Automate deployment",
12+
"domain": "deploy",
13+
"cms_min": "3.3",
14+
"version": "1.0"
15+
}
16+
},
17+
"autoload": {
18+
"psr-4": {
19+
"Juzaweb\\AutoDeploy\\": "src/"
20+
}
21+
}
22+
}

config/deploy.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
return [
44
'enable' => env('DEPLOY_ENABLE', false),
5-
5+
66
'github' => [
77
'secret' => env('DEPLOY_GITHUB_SECRET'),
8+
9+
'verify' => env('DEPLOY_GITHUB_VERIFY_SECRET', true),
810
],
11+
912
/**
1013
* Run command deploy with method
1114
* Support: queue, cron
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
return new class extends Migration {
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up(): void
14+
{
15+
Schema::create(
16+
'deploy_deploy_tokens',
17+
function (Blueprint $table) {
18+
$table->bigIncrements('id');
19+
$table->uuid()->unique();
20+
$table->json('params')->nullable();
21+
$table->timestamps();
22+
}
23+
);
24+
}
25+
26+
/**
27+
* Reverse the migrations.
28+
*
29+
* @return void
30+
*/
31+
public function down(): void
32+
{
33+
Schema::dropIfExists('deploy_deploy_tokens');
34+
}
35+
};

src/Commands/AutoDeployCommand.php

+12-51
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
namespace Juzaweb\AutoDeploy\Commands;
44

55
use Illuminate\Console\Command;
6-
use Illuminate\Support\Facades\Log;
6+
use Juzaweb\AutoDeploy\Contrasts\AutoDeploy;
77
use Symfony\Component\Console\Input\InputArgument;
8-
use Symfony\Component\Process\Process;
9-
use Noodlehaus\Config;
108

119
class AutoDeployCommand extends Command
1210
{
11+
public function __construct(protected AutoDeploy $autoDeploy)
12+
{
13+
parent::__construct();
14+
}
15+
1316
/**
1417
* The console command name.
1518
*
@@ -32,59 +35,17 @@ class AutoDeployCommand extends Command
3235
public function handle(): int
3336
{
3437
$action = $this->argument('action');
35-
if (config('deploy.method') == 'cron') {
36-
$schedule = get_config('deploy_schedules', []);
37-
if (empty($schedule)) {
38-
return self::SUCCESS;
39-
}
40-
41-
$action = array_key_first($schedule);
42-
unset($schedule[$action]);
43-
set_config('deploy_schedules', $schedule);
44-
45-
$this->runAction($action);
46-
return self::SUCCESS;
47-
}
48-
49-
$this->runAction($action);
50-
51-
return self::SUCCESS;
52-
}
53-
54-
protected function runAction(string $action): int
55-
{
56-
$config = Config::load(base_path('.deploy.yml'));
57-
$commands = $config->get("{$action}.commands", []);
58-
if (empty($commands)) {
59-
$this->error("Action not found.");
60-
return self::SUCCESS;
61-
}
62-
63-
foreach ($commands as $command) {
64-
$this->info("Running '{$command}'");
65-
$cmd = explode(' ', trim($command));
66-
if ($cmd[0] == 'php' && $cmd[1] == 'artisan') {
67-
$this->call($cmd[2]);
68-
} else {
69-
$process = new Process($cmd);
70-
71-
$process->run(
72-
function ($type, $buffer) {
73-
$this->info($buffer);
74-
}
75-
);
76-
}
77-
}
78-
79-
Log::info("Deploy success {$action}");
80-
38+
39+
$this->autoDeploy->run($action);
40+
8141
return self::SUCCESS;
8242
}
83-
43+
8444
protected function getArguments(): array
8545
{
8646
return [
87-
['action', InputArgument::OPTIONAL, 'The action to run.'],
47+
['action', InputArgument::REQUIRED, 'The action to run.'],
48+
['token', InputArgument::REQUIRED, 'The token to run.'],
8849
];
8950
}
9051
}

src/Contrasts/AutoDeploy.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* JUZAWEB CMS - Laravel CMS for Your Project
4+
*
5+
* @package juzaweb/juzacms
6+
* @author The Anh Dang
7+
* @link https://juzaweb.com
8+
* @license GNU V2
9+
*/
10+
11+
namespace Juzaweb\AutoDeploy\Contrasts;
12+
13+
use Illuminate\Contracts\Foundation\Application;
14+
use Illuminate\Contracts\Routing\ResponseFactory;
15+
use Illuminate\Http\Request;
16+
use Illuminate\Http\Response;
17+
18+
interface AutoDeploy
19+
{
20+
public function run(string $action): bool;
21+
22+
public function webhook(Request $request, string $action, string $token): Response;
23+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* JUZAWEB CMS - Laravel CMS for Your Project
4+
*
5+
* @package juzaweb/juzacms
6+
* @author The Anh Dang
7+
* @link https://juzaweb.com
8+
* @license GNU V2
9+
*/
10+
11+
namespace Juzaweb\AutoDeploy\Exceptions;
12+
13+
use Exception;
14+
15+
class AutoDeployException extends Exception
16+
{
17+
18+
}

src/Http/Controllers/DeployController.php

+9-31
Original file line numberDiff line numberDiff line change
@@ -11,44 +11,22 @@
1111
namespace Juzaweb\AutoDeploy\Http\Controllers;
1212

1313
use Illuminate\Http\Request;
14-
use Illuminate\Support\Facades\Artisan;
15-
use Illuminate\Support\Facades\Log;
16-
use Juzaweb\AutoDeploy\Commands\AutoDeployCommand;
14+
use Illuminate\Http\Response;
15+
use Juzaweb\AutoDeploy\Contrasts\AutoDeploy;
1716
use Juzaweb\CMS\Http\Controllers\ApiController;
18-
use Symfony\Component\Console\Output\BufferedOutput;
1917

2018
class DeployController extends ApiController
2119
{
22-
public function github(Request $request, string $action): \Illuminate\Http\Response
20+
public function __construct(protected AutoDeploy $autoDeploy)
21+
{
22+
}
23+
24+
public function handle(Request $request, string $action, string $token): Response
2325
{
2426
if (!config('deploy.enable')) {
2527
return response("Deploy is not enabled.", 403);
2628
}
27-
28-
$outputLog = new BufferedOutput();
29-
$githubPayload = $request->getContent();
30-
$githubHash = $request->header('X-Hub-Signature');
31-
$localToken = config('deploy.github.secret');
32-
$localHash = 'sha1='.hash_hmac('sha1', $githubPayload, $localToken);
33-
34-
if (!hash_equals($githubHash, $localHash)) {
35-
abort(403);
36-
}
37-
38-
Log::info("Deploy: ". json_encode($request->all()));
39-
40-
switch (config('deploy.method')) {
41-
case 'cron':
42-
$schedule = get_config('deploy_schedules', []);
43-
$schedule[$action] = date('Y-m-d H:i:s');
44-
set_config('deploy_schedules', $schedule);
45-
return response("Deploy command is running...");
46-
case 'queue':
47-
Artisan::queue(AutoDeployCommand::class, ['action' => $action]);
48-
return response("Deploy command is running...");
49-
default:
50-
Artisan::call(AutoDeployCommand::class, ['action' => $action], $outputLog);
51-
return response($outputLog->fetch());
52-
}
29+
30+
return $this->autoDeploy->webhook($request, $action, $token);
5331
}
5432
}

src/Models/DeployToken.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Juzaweb\AutoDeploy\Models;
4+
5+
use Juzaweb\CMS\Models\Model;
6+
7+
class DeployToken extends Model
8+
{
9+
protected $table = 'deploy_deploy_tokens';
10+
protected $fillable = [];
11+
}

src/Providers/AutoDeployServiceProvider.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44

55
use Illuminate\Console\Scheduling\Schedule;
66
use Juzaweb\AutoDeploy\Commands\AutoDeployCommand;
7+
use Juzaweb\AutoDeploy\Contrasts\AutoDeploy as AutoDeployContrast;
8+
use Juzaweb\AutoDeploy\Support\AutoDeploy;
79
use Juzaweb\CMS\Support\ServiceProvider;
810

911
class AutoDeployServiceProvider extends ServiceProvider
1012
{
1113
public function boot()
1214
{
1315
$this->commands([AutoDeployCommand::class]);
14-
16+
1517
$this->app->booted(
1618
function () {
1719
$schedule = $this->app->make(Schedule::class);
@@ -21,9 +23,11 @@ function () {
2123
}
2224
);
2325
}
24-
26+
2527
public function register()
2628
{
2729
$this->mergeConfigFrom(__DIR__.'/../../config/deploy.php', 'deploy');
30+
31+
$this->app->singleton(AutoDeployContrast::class, AutoDeploy::class);
2832
}
2933
}

0 commit comments

Comments
 (0)