[Deprecated] Old repository of switching.software - we moved to https://codeberg.org/swiso/website
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

144 lines
3.7 KiB

  1. <?php
  2. /**
  3. * Deploy script for staging and production site of switching.software
  4. *
  5. * @category None
  6. * @package None
  7. * @author switching.software <info@switching.software>
  8. * @license CC-BY-SA 4.0
  9. * @link https://switching.software
  10. */
  11. $secret = getenv('CODEBERG_DEPLOY_SECRET');
  12. $hugo = 'hugo';
  13. $source_dir = '/home/swiso/src/website/';
  14. $public_dir = '/home/swiso/www/';
  15. // check secret is set
  16. if (empty($secret)) {
  17. error('FAILED - CODEBERG_DEPLOY_SECRET not set');
  18. }
  19. // check for POST request
  20. if ($_SERVER['REQUEST_METHOD'] != 'POST') {
  21. error('not POST - '. $_SERVER['REQUEST_METHOD']);
  22. }
  23. // get content type
  24. $content_type = isset($_SERVER['CONTENT_TYPE']) ? strtolower(trim($_SERVER['CONTENT_TYPE'])) : '';
  25. if ($content_type != 'application/json') {
  26. error('not application/json - '. $content_type);
  27. }
  28. // get payload
  29. $payload = trim(file_get_contents("php://input"));
  30. if (empty($payload)) {
  31. error('no payload');
  32. }
  33. // get header signature
  34. if (!isset($_SERVER['HTTP_X_GITEA_SIGNATURE'])) {
  35. error('header signature missing');
  36. }
  37. $header_signature = $_SERVER['HTTP_X_GITEA_SIGNATURE'];
  38. // check payload signature against header signature
  39. $payload_signature = hash_hmac('sha256', $payload, $secret, false);
  40. if ($header_signature != $payload_signature) {
  41. error('payload signature');
  42. }
  43. // convert json to array
  44. $data = json_decode($payload);
  45. if (json_last_error() !== JSON_ERROR_NONE) {
  46. error('json decode failed - '. json_last_error());
  47. }
  48. // determine changed branch
  49. if (!isset($data->ref)) {
  50. error('no target branch');
  51. }
  52. if (substr($data->ref, 0, 11) !== "refs/heads/") {
  53. error('couldn\'t determine branch - '. $data->ref);
  54. }
  55. $branch = substr($data->ref, 11);
  56. $subdomain = ($branch == "main") ? '' : $branch . '.';
  57. if ($branch !== "main" && $branch !== "develop") {
  58. error('unknown branch - '. $branch);
  59. }
  60. // collect commit messages
  61. $commit_message = '';
  62. foreach ($data->commits as $commit) {
  63. $commit_message .= ' ' . $commit->message;
  64. }
  65. if (empty($commit_message)) {
  66. error('no commits');
  67. }
  68. // Do a git checkout
  69. $output = array();
  70. $return = 0;
  71. exec('cd ' . $source_dir . '&& git fetch --all --prune && git reset --hard origin/' . $branch, $output, $return);
  72. if ($return != 0) {
  73. error('git fetch/reset failed');
  74. }
  75. // Run Hugo for switching.software
  76. exec('cd ' . $source_dir . '&& ' . $hugo . ' -b https://' . $subdomain . 'switching.software -d ' . $public_dir . 'switching-software/' . $branch . ' --cleanDestinationDir', $output, $return);
  77. if ($return != 0 && $return != 255) {
  78. error('hugo build failed (switching.software)');
  79. }
  80. // Run Hugo for swiso.org
  81. exec('cd ' . $source_dir . '&& ' . $hugo . ' -b https://' . $subdomain . 'swiso.org -d ' . $public_dir . 'swiso-org/' . $branch . ' --cleanDestinationDir', $output, $return);
  82. if ($return != 0 && $return != 255) {
  83. error('hugo build failed (swiso.org)');
  84. }
  85. // Update deploy script
  86. if ($branch == "develop") {
  87. exec('cp ' . $source_dir . '/deploy.php ' . getcwd() . '/deploy.php', $output, $return);
  88. if ($return != 0 && $return != 255) {
  89. error('update of deploy script failed');
  90. }
  91. }
  92. // Log the deployment
  93. file_put_contents(
  94. $public_dir . 'deploy.txt',
  95. date('Y-m-d H:i:s') . " on " . $branch . ": " . $commit_message,
  96. FILE_APPEND
  97. );
  98. /**
  99. * Print error message to log and return error code
  100. *
  101. * @param String $text the error message
  102. * @return void
  103. */
  104. function error($text)
  105. {
  106. // log error message
  107. error_log('deploy failed - ' . $text);
  108. file_put_contents(
  109. $public_dir . 'deploy.txt',
  110. date('Y-m-d H:i:s') . " error: " . $text,
  111. FILE_APPEND
  112. );
  113. // return error message to sender
  114. http_response_code(404);
  115. echo($text);
  116. exit(4);
  117. };