Build, Test and Deploy Discord Bot to VPS #1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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!" | |