From 04c391cc5f1693bc58d208f22978aabd1af07a39 Mon Sep 17 00:00:00 2001 From: AzertoxHDW Date: Wed, 19 Nov 2025 01:55:00 +0100 Subject: [PATCH] Edited deploy scripts (again) --- .github/workflows/deploy.yml | 20 +++++--- scripts/deploy.sh | 96 +++++++++++++++++++++++++----------- 2 files changed, 81 insertions(+), 35 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index b20fc04..5abd3c0 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -1,7 +1,7 @@ name: Manual Deploy to Self-Hosted Alpine on: - # Allows you to run this workflow manually from the Actions tab + # Manual trigger from GitHub Actions tab workflow_dispatch: inputs: branch: @@ -10,15 +10,16 @@ on: default: 'main' type: string - # Optional: Uncomment to auto-deploy on push to dev + # Auto-deploy on PR merge to main (Optional) pull_request: + types: [closed] branches: [ "main" ] jobs: deploy: + # This targets your Debian LXC runner runs-on: self-hosted - # Grant necessary permissions permissions: contents: read @@ -26,11 +27,16 @@ jobs: - name: Checkout repository uses: actions/checkout@v4 + - name: Setup SSH Agent + # This loads your SSH private key into the runner's session + # allowing the deploy.sh script to SSH into 192.168.0.102 + uses: webfactory/ssh-agent@v0.9.0 + with: + ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} + - name: Make deploy script executable run: chmod +x ./scripts/deploy.sh - name: Execute Deployment Script - run: ./scripts/deploy.sh "${{ inputs.branch || 'main' }}" - env: - # Set the directory name if different from default - SITE_DIR: "azertox.com" \ No newline at end of file + # Pass the branch name to the script + run: ./scripts/deploy.sh "${{ inputs.branch || 'main' }}" \ No newline at end of file diff --git a/scripts/deploy.sh b/scripts/deploy.sh index f8376c9..033c0f4 100644 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -1,42 +1,82 @@ #!/bin/bash # Deployment script for azertox.com -# Pulls from dev branch and rebuilds the application +# Executed by Debian Runner -> Actions on Alpine Server (192.168.0.102) -set -e # Exit on any error +set -e # Exit immediately if a command exits with a non-zero status -SITE_DIR="azertox.com" +# --- Configuration --- +REMOTE_USER="root" +REMOTE_HOST="192.168.0.102" +REMOTE_SITE_DIR="azertox.com" +REMOTE_TARGET_PATH="/root/${REMOTE_SITE_DIR}" REPO_URL="https://github.com/AzertoxHDW/azertox.com" -# Prompt for branch selection -read -p "Enter branch to deploy (default: dev): " BRANCH -BRANCH=${BRANCH:-dev} +# Branch passed from GitHub Actions input, defaults to 'main' +BRANCH=${1:-main} -echo "=== Starting deployment of '$BRANCH' branch ===" +echo "=== Starting deployment of '$BRANCH' branch to $REMOTE_HOST ===" -# Delete the old website directory -if [ -d "$SITE_DIR" ]; then - echo "Removing old directory..." - rm -rf "$SITE_DIR" -fi +# --- Step 1: Connect to Remote and Reset Directory --- +echo "Connecting to remote server to prepare directory..." +ssh -o StrictHostKeyChecking=no "$REMOTE_USER@$REMOTE_HOST" << EOF + set -e + + # Delete the old website directory (Replicating manual script logic) + if [ -d "$REMOTE_TARGET_PATH" ]; then + echo "Removing old directory: $REMOTE_TARGET_PATH..." + rm -rf "$REMOTE_TARGET_PATH" + fi -# Clone the repository -echo "Cloning repository..." -git clone "$REPO_URL" -cd "$SITE_DIR" + # Ensure parent directory exists + mkdir -p "/var/www" + cd "/var/www" -# Pull the branch -echo "Pulling $BRANCH branch..." -git checkout "$BRANCH" -git pull origin "$BRANCH" + # Clone the repository + echo "Cloning repository..." + git clone "$REPO_URL" "$REMOTE_SITE_DIR" + cd "$REMOTE_SITE_DIR" -# Build the app -echo "Installing dependencies..." -npm install + # Checkout the specific branch + echo "Checking out $BRANCH branch..." + git checkout "$BRANCH" + git pull origin "$BRANCH" +EOF -echo "Building application..." -npm run build +# --- Step 2: Build the Application Remote --- +echo "Building application on remote server..." +ssh -o StrictHostKeyChecking=no "$REMOTE_USER@$REMOTE_HOST" << EOF + set -e + cd "$REMOTE_TARGET_PATH" -# Reboot the container (pm2 will start the app automatically) -echo "Build complete. Rebooting container..." -reboot \ No newline at end of file + echo "Installing dependencies..." + # npm ci is faster/cleaner for CI/CD than npm install + npm ci + + echo "Building application..." + npm run build + + # Optional: Prune dev dependencies to save space + # npm prune --production +EOF + +# --- Step 3: Manage PM2 Process --- +echo "Managing application process..." +ssh -o StrictHostKeyChecking=no "$REMOTE_USER@$REMOTE_HOST" << EOF + set -e + + # Check if app is running + if pm2 list | grep -q "Website"; then + echo "App is running. Reloading..." + pm2 reload Website + else + echo "App not running. Starting..." + # Start the app from the build folder + pm2 start "$REMOTE_TARGET_PATH/build/index.js" --name "Website" --env production --max-memory-restart 300M + fi + + # Save PM2 list so it survives reboots + pm2 save +EOF + +echo "=== Deployment Complete ===" \ No newline at end of file