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.

127 line
3.3KB

  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. if [ "$#" -lt 3 ] || [ "$#" -gt 4 ]; then
  4. echo "Usage: scripts/deploy-raspi.sh <ssh-user> <ssh-host> <git-repo> [domain]"
  5. exit 1
  6. fi
  7. ssh_user="$1"
  8. ssh_host="$2"
  9. repo_url="$3"
  10. domain="${4:-DietPi.ntp.kentcommunications.com}"
  11. deploy_path="${DEPLOY_PATH:-/var/www/warehouse}"
  12. deploy_ref="${DEPLOY_REF:-main}"
  13. php_version="${PHP_VERSION:-8.2}"
  14. ssh "${ssh_user}@${ssh_host}" bash -s -- "$repo_url" "$domain" "$deploy_path" "$deploy_ref" "$php_version" <<'REMOTE'
  15. set -euo pipefail
  16. repo_url="$1"
  17. domain="$2"
  18. deploy_path="$3"
  19. deploy_ref="$4"
  20. php_version="$5"
  21. owner="$(id -un)"
  22. group="$(id -gn)"
  23. php_fpm_service="php${php_version}-fpm"
  24. php_fpm_socket="/run/php/${php_fpm_service}.sock"
  25. set_env() {
  26. file="$1"
  27. key="$2"
  28. value="$3"
  29. if grep -q "^${key}=" "$file"; then
  30. sed -i "s|^${key}=.*|${key}=${value}|" "$file"
  31. else
  32. printf '%s=%s\n' "$key" "$value" >> "$file"
  33. fi
  34. }
  35. sudo apt-get update
  36. sudo apt-get install -y git nginx composer \
  37. "php${php_version}" "php${php_version}-cli" "php${php_version}-fpm" \
  38. "php${php_version}-mbstring" "php${php_version}-xml" \
  39. "php${php_version}-curl" "php${php_version}-zip" \
  40. "php${php_version}-sqlite3" "php${php_version}-mysql"
  41. sudo mkdir -p "$(dirname "$deploy_path")"
  42. if [ ! -d "$deploy_path/.git" ]; then
  43. sudo git clone --branch "$deploy_ref" --single-branch "$repo_url" "$deploy_path"
  44. sudo chown -R "$owner:$group" "$deploy_path"
  45. else
  46. git -C "$deploy_path" fetch --prune origin
  47. git -C "$deploy_path" checkout "$deploy_ref"
  48. git -C "$deploy_path" reset --hard "origin/$deploy_ref"
  49. fi
  50. cd "$deploy_path"
  51. if [ ! -f .env ] && [ -f .env.example ]; then cp .env.example .env; fi
  52. set_env .env APP_ENV prod
  53. set_env .env DB_CONNECTION sqlite
  54. set_env .env DB_SQLITE_PATH "$deploy_path/database/database.sqlite"
  55. composer install --no-dev --no-interaction --prefer-dist --optimize-autoloader --ignore-platform-reqs
  56. mkdir -p storage/framework/views database
  57. touch database/database.sqlite
  58. sudo chgrp -R www-data storage database
  59. sudo chmod -R g+rwX storage database
  60. sudo find storage database -type d -exec chmod 2775 {} \;
  61. sudo chmod 664 database/database.sqlite
  62. php command.php migrate
  63. sudo tee /etc/systemd/system/warehouse-queue.service >/dev/null <<EOF
  64. [Unit]
  65. Description=Warehouse queue worker
  66. After=network.target
  67. [Service]
  68. Type=simple
  69. User=${owner}
  70. Group=www-data
  71. WorkingDirectory=${deploy_path}
  72. ExecStart=/usr/bin/php ${deploy_path}/command.php queue:work
  73. Restart=always
  74. RestartSec=3
  75. [Install]
  76. WantedBy=multi-user.target
  77. EOF
  78. sudo tee /etc/nginx/sites-available/warehouse >/dev/null <<EOF
  79. server {
  80. listen 80;
  81. listen [::]:80;
  82. server_name ${domain};
  83. root ${deploy_path}/public;
  84. index index.php;
  85. location / {
  86. try_files \$uri \$uri/ /index.php?\$query_string;
  87. }
  88. location ~ \.php$ {
  89. include snippets/fastcgi-php.conf;
  90. fastcgi_pass unix:${php_fpm_socket};
  91. }
  92. location ~ /\. {
  93. deny all;
  94. }
  95. }
  96. EOF
  97. sudo ln -sfn /etc/nginx/sites-available/warehouse /etc/nginx/sites-enabled/warehouse
  98. sudo rm -f /etc/nginx/sites-enabled/default
  99. sudo systemctl daemon-reload
  100. sudo systemctl enable "$php_fpm_service" nginx warehouse-queue.service
  101. sudo systemctl restart "$php_fpm_service"
  102. sudo nginx -t
  103. sudo systemctl restart nginx
  104. sudo systemctl restart warehouse-queue.service
  105. echo "Deployment complete: http://${domain}"
  106. REMOTE

Powered by TurnKey Linux.