Skip to content

Instantly share code, notes, and snippets.

@emaia
Last active November 27, 2023 16:52
Show Gist options
  • Save emaia/71bf0437acab4bc785d6b0856f02583f to your computer and use it in GitHub Desktop.
Save emaia/71bf0437acab4bc785d6b0856f02583f to your computer and use it in GitHub Desktop.
Simple CI/CD api script
<?php
use Hyperf\Nano\Factory\AppFactory;
use Hyperf\HttpServer\Contract\RequestInterface;
require_once __DIR__.'/vendor/autoload.php';
$app = AppFactory::create('0.0.0.0', 9051);
$app->post('/deployer/api/webhook/gh', function (RequestInterface $request) {
$secret = "mysupersecretkey"; // github webhook secret key
$signature = $request->getHeaders()['x-hub-signature'][0] ?? '';
if ($signature) {
$hash = "sha1=".hash_hmac('sha1', $request->getBody(), $secret);
if (strcmp($signature, $hash) == 0) {
session_write_close();
shell_exec("/home/ubuntu/deployer/deploy.sh > /dev/null 2>/dev/null &");
return ['status' => 'processed'];
}
}
return ['status' => 'ok'];
});
$app->run();
[program:deployer-api]
command=php /home/ubuntu/deployer/api-deployer.php start
user=ubuntu
autostart=true
autorestart=true
starttries=3
stdout_logfile=/home/ubuntu/app/storage/logs/deploy-worker.log
{
"name": "deployer/php-api-deployer",
"require": {
"hyperf/nano": "^2.0"
}
}
#!/usr/bin/env bash
cd /home/ubuntu/app/
git pull
php /usr/local/bin/composer install --no-interaction --prefer-dist --optimize-autoloader --no-dev
/home/ubuntu/.bun/bin/bun install
/home/ubuntu/.bun/bin/bun run build
php artisan cache:clear
php artisan responsecache:clear
php artisan optimize
php artisan octane:reload
cd storage/logs && touch deployed.log && echo 'Deployed at '$(date '+%Y-%m-%d %H:%M:%S') >> deployed.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment