nickmous

deployer.php config

Jan 9th, 2025
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.10 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Deployer;
  4.  
  5. use Deployer\Exception\Exception;
  6.  
  7. require 'contrib/npm.php';
  8. require 'contrib/sentry.php';
  9. require 'recipe/deploy/check_remote.php';
  10. require 'recipe/laravel.php';
  11.  
  12. set('repository', '[email protected]:NickMous/zcdb.git');
  13. set('dotenv', __DIR__.'/.env');
  14. set('bin/bun', function () {
  15.     return run('which bun');
  16. });
  17.  
  18. add('shared_files', []);
  19. add('shared_dirs', []);
  20. add('writable_dirs', []);
  21.  
  22. // Hosts
  23. foreach (range(1, 5) as $i) {
  24.     host('zcdb0'.$i)
  25.         ->setHostname('<ip>')
  26.         ->setRemoteUser('deployer')
  27.         ->setPort(<port>)
  28.         ->setDeployPath('/var/www/zcdb/0'.$i);
  29. }
  30.  
  31. task('git:check-local-changes', function () {
  32.     $result = runLocally('git status --porcelain');
  33.  
  34.     if (! empty($result)) {
  35.         throw new Exception('Local changes detected! Commit your changes before deploying.');
  36.     }
  37.  
  38.     $localBranch = runLocally('git rev-parse --abbrev-ref HEAD');
  39.     $remoteBranch = runLocally('git rev-parse --abbrev-ref @{u}');
  40.     $result = runLocally("git rev-list --left-right --count $localBranch...$remoteBranch");
  41.     [$ahead, $behind] = explode("\t", $result);
  42.  
  43.     if ($ahead > 0) {
  44.         throw new Exception('Local branch is ahead of the remote branch! Push your changes before deploying.');
  45.     }
  46.  
  47.     if ($behind > 0) {
  48.         throw new Exception('Local branch is behind the remote branch! Pull the changes before deploying.');
  49.     }
  50. });
  51.  
  52. task('local:copy-env', function () {
  53.     if (file_exists('.env')) {
  54.         runLocally('cp .env .env.backup');
  55.         set('env_backup', true);
  56.         download('{{deploy_path}}/shared/.env', '.env');
  57.     } else {
  58.         writeln('<comment>.env file not found on server</comment>');
  59.         set('env_backup', false);
  60.     }
  61. });
  62.  
  63. task('local:restore-env', function () {
  64.     if (get('env_backup')) {
  65.         runLocally('mv .env.backup .env');
  66.     }
  67. });
  68.  
  69. task('local:bun:install', function () {
  70.     runLocally('bun i');
  71. });
  72.  
  73. task('local:bun:build', function () {
  74.     runLocally('bun run build');
  75. });
  76.  
  77. task('local:upload-assets', function () {
  78.     upload('public/build/', '{{release_path}}/public/build');
  79. });
  80.  
  81. task('dotenv:load', function () {
  82.     $dotenv = get('dotenv');
  83.     if (! file_exists($dotenv)) {
  84.         throw new Exception('.env file not found');
  85.     }
  86.  
  87.     $content = file_get_contents($dotenv);
  88.     $lines = explode("\n", $content);
  89.  
  90.     foreach ($lines as $line) {
  91.         if (empty($line) || ! str_contains($line, '=')) {
  92.             continue;
  93.         }
  94.  
  95.         [$key, $value] = explode('=', $line, 2);
  96.         $key = trim($key);
  97.         $value = trim($value);
  98.  
  99.         putenv("$key=$value");
  100.     }
  101. });
  102.  
  103. task('local:check-sentry-env', function () {
  104.     invoke('dotenv:load');
  105.     $sentryAuthToken = getenv('SENTRY_AUTH_TOKEN');
  106.     if (! $sentryAuthToken) {
  107.         throw new Exception('SENTRY_LARAVEL_DSN not found in .env');
  108.     }
  109.     set('sentry_auth_token', $sentryAuthToken);
  110. });
  111.  
  112. task('local:set-sentry-env', function () {
  113.     invoke('dotenv:load');
  114.     $host = get('alias');
  115.     $host = str_replace('zcdb', 'stag', $host);
  116.     set('sentry', [
  117.         'organization' => 'de-brug',
  118.         'projects' => [
  119.             $host.'-frontend',
  120.             $host.'-backend',
  121.         ],
  122.         'token' => get('sentry_auth_token'),
  123.         'sentry_server' => 'https://sentry.nickmous.com',
  124.         'environment' => getenv('APP_ENV'),
  125.     ]);
  126. });
  127.  
  128. task('deploy', [
  129.     'deploy:prepare',
  130.     'deploy:vendors',
  131.     'local:copy-env',
  132.     'local:bun:install',
  133.     'local:bun:build',
  134.     'local:upload-assets',
  135.     'artisan:storage:link',
  136.     'artisan:config:cache',
  137.     'artisan:route:cache',
  138.     'artisan:view:cache',
  139.     'artisan:event:cache',
  140.     'artisan:migrate',
  141.     'deploy:publish',
  142. ]);
  143.  
  144. // Hooks
  145. before('deploy', 'deploy:check_remote');
  146. before('deploy', 'git:check-local-changes');
  147. before('deploy', 'local:check-sentry-env');
  148. after('deploy:failed', 'deploy:unlock');
  149. after('deploy:failed', 'local:restore-env');
  150. after('deploy', 'local:set-sentry-env');
  151. after('deploy', 'deploy:sentry');
  152. after('deploy', 'local:restore-env');
  153.  
Advertisement
Add Comment
Please, Sign In to add comment