PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/deploy.php

https://bitbucket.org/figurluk/unit_prague_2018
PHP | 145 lines | 97 code | 37 blank | 11 comment | 6 complexity | dd33b0ec7e63f98d2e82365985c87fe5 MD5 | raw file
  1. <?php
  2. namespace Deployer;
  3. require 'recipe/common.php';
  4. require 'recipe/laravel.php';
  5. function updateProductSeeders($seeder)
  6. {
  7. $reading = fopen('database/seeds/production-seeds.php', 'r');
  8. $writing = fopen('database/seeds/production-seeds.tmp', 'w');
  9. $replaced = false;
  10. while (!feof($reading)) {
  11. $line = fgets($reading);
  12. if (strpos($line, $seeder)) {
  13. fputs($writing, $line);
  14. fgets($reading);
  15. $line = "\t\t'executed' => '" . date('d.m.Y H:i:s') . "',\n";
  16. $replaced = true;
  17. }
  18. fputs($writing, $line);
  19. }
  20. fclose($reading);
  21. fclose($writing);
  22. // might as well not overwrite the file if we didn't replace anything
  23. if ($replaced) {
  24. unlink('database/seeds/production-seeds.php');
  25. rename('database/seeds/production-seeds.tmp', 'database/seeds/production-seeds.php');
  26. } else {
  27. unlink('database/seeds/production-seeds.tmp');
  28. }
  29. }
  30. // Configuration
  31. set('ssh_type', 'native');
  32. set('ssh_multiplexing', true);
  33. //set('repository', 'git@domain.com:username/repository.git');
  34. set('repository', 'git@bitbucket.org:figurluk/unit_prague_2018.git');
  35. // Laravel shared file
  36. add('shared_files', []);
  37. set('shared_dirs', []);
  38. add('writable_dirs', [
  39. 'storage/system',
  40. 'config'
  41. ]);
  42. // Servers
  43. host('byclick.miniserver.cz')
  44. ->stage('production')
  45. ->user('admin')
  46. ->set('branch', 'deploy-prod')
  47. ->set('deploy_path', '/home/admin/www/slavokozar_sk/unit')
  48. ->roles('app');
  49. // Tasks
  50. desc('Question "Are you sure?"');
  51. task('sure_question', function () {
  52. if (!askConfirmation('Are you sure to deploy on production server ?', $default = false)) {
  53. writeln('<comment>Aborted</comment>');
  54. exit(0);
  55. }
  56. })->onStage(['production']);
  57. desc('Restart PHP-FPM service');
  58. task('php-fpm:restart', function () {
  59. // The user must have rights for restart service
  60. // /etc/sudoers: username ALL=NOPASSWD:/bin/systemctl restart php-fpm.service
  61. run('sudo systemctl restart php-fpm.service');
  62. });
  63. desc('Execute artisan migrate reset');
  64. task('artisan:migrate:reset', function () {
  65. run('{{bin/php}} {{release_path}}/artisan migrate:reset --force');
  66. })->onStage('test');
  67. desc('Execute artisan db:seed on test');
  68. task('artisan:db:seed-test', function () {
  69. run('{{bin/php}} {{release_path}}/artisan db:seed --force');
  70. })->onStage('test');
  71. desc('Execute artisan db:seed on production');
  72. task('artisan:db:seed-product', function () {
  73. $productSeeds = require('database/seeds/production-seeds.php');
  74. foreach ($productSeeds as $seeder => $info) {
  75. if (!$info['executed']) {
  76. run('{{bin/php}} {{release_path}}/artisan db:seed --force --class=' . $seeder);
  77. updateProductSeeders($seeder);
  78. }
  79. }
  80. })->onStage('production');
  81. // [Optional] if deploy fails automatically unlock.
  82. after('deploy:failed', 'deploy:unlock');
  83. desc('Execute artisan copy-env');
  84. task('copy-env', function () {
  85. run('rm -f {{release_path}}/.env');
  86. run('yes | cp {{deploy_path}}/shared/.env {{release_path}}');
  87. });
  88. desc('Execute artisan config:clear');
  89. task('artisan:config:clear', function () {
  90. run('{{bin/php}} {{release_path}}/artisan config:clear');
  91. });
  92. desc('Execute artisan route:clear');
  93. task('artisan:route:clear', function () {
  94. run('{{bin/php}} {{release_path}}/artisan route:clear');
  95. });
  96. before('deploy:prepare', 'sure_question');
  97. //for bad deploys fix
  98. before('deploy:lock', 'deploy:unlock');
  99. after('deploy:shared', 'copy-env');
  100. // Migrate database before symlink new release.
  101. before('deploy:symlink', 'artisan:migrate');
  102. before('artisan:migrate', 'artisan:migrate:reset');
  103. after('artisan:migrate', 'artisan:db:seed-test');
  104. after('artisan:db:seed-test', 'artisan:db:seed-product');
  105. after('artisan:config:cache', 'artisan:config:clear');
  106. after('artisan:route:cache', 'artisan:route:clear');
  107. after('artisan:config:cache', 'artisan:config:clear');
  108. after('artisan:route:cache', 'artisan:route:clear');