#!/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:-master}" ssh "${ssh_user}@${ssh_host}" bash -s -- "$repo_url" "$domain" "$deploy_path" "$deploy_ref" <<'REMOTE' set -euo pipefail repo_url="$1" domain="$2" deploy_path="$3" deploy_ref="$4" owner="$(id -un)" group="$(id -gn)" 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-cli php-fpm php-mbstring php-xml php-curl php-zip php-sqlite3 php-mysql php_version="$(php -r 'echo PHP_MAJOR_VERSION . "." . PHP_MINOR_VERSION;')" php_fpm_service="php${php_version}-fpm" php_fpm_socket="/run/php/${php_fpm_service}.sock" 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
Powered by TurnKey Linux.