- #!/usr/bin/env bash
- set -euo pipefail
-
- if [ "$#" -lt 3 ] || [ "$#" -gt 4 ]; then
- echo "Usage: scripts/deploy-raspi.sh <ssh-user> <ssh-host> <git-repo> [domain]"
- exit 1
- fi
-
- ssh_user="$1"
- ssh_host="$2"
- repo_url="$3"
- domain="${4:-DietPi.ntp.kentcommunications.com}"
- deploy_path="${DEPLOY_PATH:-/var/www/warehouse}"
- deploy_ref="${DEPLOY_REF:-main}"
- php_version="${PHP_VERSION:-8.2}"
-
- ssh "${ssh_user}@${ssh_host}" bash -s -- "$repo_url" "$domain" "$deploy_path" "$deploy_ref" "$php_version" <<'REMOTE'
- set -euo pipefail
-
- repo_url="$1"
- domain="$2"
- deploy_path="$3"
- deploy_ref="$4"
- php_version="$5"
- owner="$(id -un)"
- group="$(id -gn)"
- php_fpm_service="php${php_version}-fpm"
- php_fpm_socket="/run/php/${php_fpm_service}.sock"
-
- set_env() {
- file="$1"
- key="$2"
- value="$3"
- if grep -q "^${key}=" "$file"; then
- sed -i "s|^${key}=.*|${key}=${value}|" "$file"
- else
- printf '%s=%s\n' "$key" "$value" >> "$file"
- fi
- }
-
- sudo apt-get update
- sudo apt-get install -y git nginx composer \
- "php${php_version}" "php${php_version}-cli" "php${php_version}-fpm" \
- "php${php_version}-mbstring" "php${php_version}-xml" \
- "php${php_version}-curl" "php${php_version}-zip" \
- "php${php_version}-sqlite3" "php${php_version}-mysql"
-
- sudo mkdir -p "$(dirname "$deploy_path")"
- if [ ! -d "$deploy_path/.git" ]; then
- sudo git clone --branch "$deploy_ref" --single-branch "$repo_url" "$deploy_path"
- sudo chown -R "$owner:$group" "$deploy_path"
- else
- git -C "$deploy_path" fetch --prune origin
- git -C "$deploy_path" checkout "$deploy_ref"
- git -C "$deploy_path" reset --hard "origin/$deploy_ref"
- fi
-
- cd "$deploy_path"
- if [ ! -f .env ] && [ -f .env.example ]; then cp .env.example .env; fi
- set_env .env APP_ENV prod
- set_env .env DB_CONNECTION sqlite
- set_env .env DB_SQLITE_PATH "$deploy_path/database/database.sqlite"
-
- composer install --no-dev --no-interaction --prefer-dist --optimize-autoloader --ignore-platform-reqs
-
- mkdir -p storage/framework/views database
- touch database/database.sqlite
- sudo chgrp -R www-data storage database
- sudo chmod -R g+rwX storage database
- sudo find storage database -type d -exec chmod 2775 {} \;
- sudo chmod 664 database/database.sqlite
-
- php command.php migrate
-
- sudo tee /etc/systemd/system/warehouse-queue.service >/dev/null <<EOF
- [Unit]
- Description=Warehouse queue worker
- After=network.target
-
- [Service]
- Type=simple
- User=${owner}
- Group=www-data
- WorkingDirectory=${deploy_path}
- ExecStart=/usr/bin/php ${deploy_path}/command.php queue:work
- Restart=always
- RestartSec=3
-
- [Install]
- WantedBy=multi-user.target
- EOF
-
- sudo tee /etc/nginx/sites-available/warehouse >/dev/null <<EOF
- server {
- listen 80;
- listen [::]:80;
- server_name ${domain};
- root ${deploy_path}/public;
- index index.php;
-
- location / {
- try_files \$uri \$uri/ /index.php?\$query_string;
- }
-
- location ~ \.php$ {
- include snippets/fastcgi-php.conf;
- fastcgi_pass unix:${php_fpm_socket};
- }
-
- location ~ /\. {
- deny all;
- }
- }
- EOF
-
- sudo ln -sfn /etc/nginx/sites-available/warehouse /etc/nginx/sites-enabled/warehouse
- sudo rm -f /etc/nginx/sites-enabled/default
- sudo systemctl daemon-reload
- sudo systemctl enable "$php_fpm_service" nginx warehouse-queue.service
- sudo systemctl restart "$php_fpm_service"
- sudo nginx -t
- sudo systemctl restart nginx
- sudo systemctl restart warehouse-queue.service
-
- echo "Deployment complete: http://${domain}"
- REMOTE
|