Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -10,27 +10,33 @@ 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

steps:
- 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"
# Pass the branch name to the script
run: ./scripts/deploy.sh "${{ inputs.branch || 'main' }}"
96 changes: 68 additions & 28 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
@@ -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
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 ==="
Loading