name: Deploy to Server on: push: branches: - main jobs: deploy: runs-on: ubuntu-latest container: image: alpine:latest # Alpine to keep CPU/memory low steps: - name: Install SSH and Networking Tools run: apk add --no-cache openssh-client iproute2 git - name: Configure SSH Key run: | mkdir -p ~/.ssh echo "${{ secrets.DEPLOY_SSH_KEY }}" > ~/.ssh/id_rsa chmod 600 ~/.ssh/id_rsa echo "StrictHostKeyChecking no" > ~/.ssh/config - name: Execute Remote Deployment run: | # 1. Find the host machine's IP via the docker bridge gateway HOST_IP=$(ip route | awk '/default/ { print $3 }') echo "==> Detected Host IP: $HOST_IP" # 2. SSH into the host to execute the deployment safely ssh adipu@$HOST_IP << 'EOF' # Exit immediately if any command fails set -e echo "==> Navigating to project directory..." cd ~/AllMail echo "==> Pulling latest code..." git pull origin main echo "==> Running Build..." # If this build fails, 'set -e' aborts the script instantly. # Existing containers will NOT be touched, keeping the site up. docker compose build echo "==> Build successful! Deploying new containers..." # This only runs if the build was 100% successful. docker compose up -d echo "==> Cleaning up old images to save disk space..." docker image prune -f echo "==> Deployment Complete!" EOF