Skip to content

Build, Test and Deploy Discord Bot to VPS #1

Build, Test and Deploy Discord Bot to VPS

Build, Test and Deploy Discord Bot to VPS #1

Workflow file for this run

name: Build, Test and Deploy Discord Bot to VPS
on:
# push:
# branches: [ "main" ]
workflow_dispatch: # Allow manual deployment
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
test-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version-file: .nvmrc
- name: Setup pnpm
uses: pnpm/action-setup@v4
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Lint
run: pnpm lint
- name: Type check
run: pnpm typecheck
- name: Build
run: pnpm build:ci
- name: Deploy to VPS
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ secrets.VPS_HOST }}
username: ${{ secrets.VPS_USER }}
key: ${{ secrets.VPS_SSH_KEY }}
timeout: 30m
script: |
set -euo pipefail
echo "πŸš€ Starting deployment..."
# Create project directory if it doesn't exist
PROJECT_DIR="/home/${{ secrets.VPS_USER }}/moderation-tool-bot"
if [ ! -d "$PROJECT_DIR" ]; then
echo "πŸ“ Creating project directory..."
mkdir -p "$PROJECT_DIR"
fi
# Navigate to project directory
cd "$PROJECT_DIR"
# Check if this is a git repository
if [ ! -d ".git" ]; then
echo "πŸ“¦ Cloning repository..."
# Clone the repository if not already cloned
git clone https://github.com/r-webdev/moderation-tool.git .
git checkout main
fi
echo "πŸ”„ Pulling latest changes..."
# Pull latest changes
git checkout main
git pull origin main --no-edit
echo "βš™οΈ Setting up environment..."
# Read Node version from .nvmrc
NODE_VERSION=$(cat .nvmrc | sed 's/v//')
echo "Using Node version: $NODE_VERSION"
# Create .env.local file with secrets
cat > .env.local << EOF
DISCORD_TOKEN=${{ secrets.DISCORD_TOKEN }}
CLIENT_ID=${{ secrets.CLIENT_ID }}
SERVER_ID=${{ secrets.SERVER_ID }}
SPAM_DETECTION_CHANNEL_ID=${{ secrets.SPAM_DETECTION_CHANNEL_ID }}
ROLE_REGULAR_ID=${{ secrets.ROLE_REGULAR_ID }}
EOF
echo "🐳 Stopping existing containers..."
# Stop any existing containers (ignore errors if none running)
docker compose down --remove-orphans || true
echo "πŸ—οΈ Building and starting production container..."
# Build and start production container
docker compose --profile prod up -d --build --force-recreate
echo "⏳ Waiting for container to be healthy..."
# Wait a bit for the container to start
sleep 10
echo "πŸ” Checking deployment status..."
# Check container status
if docker compose ps | grep -q "Up"; then
echo "βœ… Deployment successful!"
else
echo "❌ Deployment failed!"
exit 1
fi
echo "πŸŽ‰ Deployment completed successfully!"