From 9f7d0b39fcdcaf5bdf4c3a4feef4e1f60da528c8 Mon Sep 17 00:00:00 2001 From: singharaj usai Date: Fri, 25 Oct 2024 23:22:17 -0400 Subject: [PATCH 1/2] test commit --- .github/workflows/deploy.yml | 28 +++------ deploy-do.sh | 117 +++++++++++++++++++++++++++++++++++ monitor.sh | 23 +++++++ server/server.js | 8 +-- test-commit.txt | 2 + update.sh | 13 ++++ 6 files changed, 169 insertions(+), 22 deletions(-) create mode 100644 deploy-do.sh create mode 100644 monitor.sh create mode 100644 update.sh diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 5718be4..67e3c0b 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -1,28 +1,20 @@ -name: Deploy Site via Git +# Create this file in your repo: .github/workflows/deploy.yml +name: Deploy on: push: - branches: - - main # Run this workflow whenever code is pushed to the main branch + branches: [ main ] jobs: deploy: runs-on: ubuntu-latest - steps: - # Step 1: Checkout the repository - - name: Checkout repository - uses: actions/checkout@v3 - - # Step 2: SSH into the VPS and pull the latest code - - name: Deploy to VPS via SSH - uses: appleboy/ssh-action@v0.1.9 + - name: Deploy to Digital Ocean + uses: appleboy/ssh-action@master with: - host: ${{ secrets.VPS_IP }} # Your VPS IP address stored as a secret - username: ${{ secrets.VPS_USER }} # Your VPS user stored as a secret - key: ${{ secrets.SSH_PRIVATE_KEY }} # Your SSH private key stored as a secret + host: ${{ secrets.HOST }} + username: root + key: ${{ secrets.SSH_PRIVATE_KEY }} script: | - cd Valkyrie/Valk.Website/AlphaBloxWeb # Navigate to your project directory - git pull origin main # Pull the latest code from GitHub - npm install # Install any new dependencies - docker-compose up --build -d app # Stop running app not webserver + cd /var/www/valkyrie + ./deploy.sh \ No newline at end of file diff --git a/deploy-do.sh b/deploy-do.sh new file mode 100644 index 0000000..e63a643 --- /dev/null +++ b/deploy-do.sh @@ -0,0 +1,117 @@ +#!/bin/bash + +# Create a new droplet +echo "Creating new droplet..." +doctl compute droplet create valkyrie-app \ + --image ubuntu-22-04-x64 \ + --size s-1vcpu-1gb \ + --region nyc1 \ + --ssh-keys $DO_SSH_KEY_FINGERPRINT + +# Get the droplet IP +DROPLET_IP=$(doctl compute droplet get valkyrie-app --format PublicIPv4 --no-header) + +# Wait for SSH to be available +until ssh -o StrictHostKeyChecking=no root@$DROPLET_IP 'exit'; do + echo "Waiting for SSH..." + sleep 5 +done + +# Setup the server +ssh -o StrictHostKeyChecking=no root@$DROPLET_IP << 'ENDSSH' + # Update system + apt update && apt upgrade -y + + # Install Node.js 18.x + curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - + apt install -y nodejs + + # Install PM2 + npm install -g pm2 + + # Install Nginx + apt install -y nginx + + # Create app directory + mkdir -p /var/www/valkyrie + cd /var/www/valkyrie + + # Clone your repository + git clone https://github.com/yourusername/your-repo.git . + + # Install dependencies + npm install + + # Setup PM2 ecosystem file + cat > ecosystem.config.js << 'EOF' +module.exports = { + apps: [{ + name: 'valkyrie', + script: 'server/server.js', + instances: 'max', + exec_mode: 'cluster', + env: { + NODE_ENV: 'production', + PORT: 3000 + } + }] +} +EOF + + # Setup Nginx configuration + cat > /etc/nginx/sites-available/valkyrie << 'EOF' +server { + listen 80; + server_name valk.fun www.valk.fun; + + location / { + proxy_pass http://localhost:3000; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection 'upgrade'; + proxy_set_header Host $host; + proxy_cache_bypass $http_upgrade; + } + + location /images/ { + alias /var/www/valkyrie/images/; + expires 30d; + add_header Cache-Control "public, no-transform"; + } + + location /uploads/ { + alias /var/www/valkyrie/uploads/; + client_max_body_size 50M; + } + + location /socket.io/ { + proxy_pass http://localhost:3000; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + } +} +EOF + + # Enable the site + ln -s /etc/nginx/sites-available/valkyrie /etc/nginx/sites-enabled/ + rm /etc/nginx/sites-enabled/default + + # Create necessary directories + mkdir -p uploads images + chmod 755 uploads images + + # Start the application with PM2 + pm2 start ecosystem.config.js + pm2 save + pm2 startup + + # Install SSL certificate + apt install -y certbot python3-certbot-nginx + certbot --nginx -d valk.fun -d www.valk.fun --non-interactive --agree-tos --email your-email@example.com + + # Restart Nginx + systemctl restart nginx +ENDSSH + +echo "Deployment complete! Your app is running at $DROPLET_IP" \ No newline at end of file diff --git a/monitor.sh b/monitor.sh new file mode 100644 index 0000000..37340a7 --- /dev/null +++ b/monitor.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +echo "=== System Status ===" +date +echo + +echo "=== Node.js Process ===" +pm2 list +pm2 logs --lines 50 +echo + +echo "=== System Resources ===" +df -h +free -m +top -bn1 | head -n 20 +echo + +echo "=== Nginx Status ===" +systemctl status nginx +echo + +echo "=== SSL Certificate Status ===" +certbot certificates \ No newline at end of file diff --git a/server/server.js b/server/server.js index 66dcfad..c6d39f5 100644 --- a/server/server.js +++ b/server/server.js @@ -262,10 +262,10 @@ app.post('/api/verify-secret-key', (req, res) => { } }); -const uploadsDir = - process.env.NODE_ENV === 'production' - ? '/tmp/uploads' // Use /tmp in production (Vercel) - : path.join(__dirname, '../uploads'); // Use local path in development +const uploadsDir = process.env.NODE_ENV === 'production' + ? '/var/data/uploads' // Render's persistent storage path + : path.join(__dirname, '../uploads'); + if (!fs.existsSync(uploadsDir)) { fs.mkdirSync(uploadsDir, { recursive: true }); diff --git a/test-commit.txt b/test-commit.txt index 517d4ef..3fe0a74 100644 --- a/test-commit.txt +++ b/test-commit.txt @@ -7,3 +7,5 @@ reste test update 10/17/2024 brooroof + +test commit 10/25/2024 \ No newline at end of file diff --git a/update.sh b/update.sh new file mode 100644 index 0000000..22fbaab --- /dev/null +++ b/update.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +# Pull latest changes +git pull origin main + +# Install dependencies +npm install --production + +# Restart the application +pm2 reload all + +# Restart Nginx +sudo systemctl restart nginx \ No newline at end of file From 0775c80f3f9bb68ff4d555c60dd7010a385d4b7a Mon Sep 17 00:00:00 2001 From: singharaj usai Date: Fri, 25 Oct 2024 23:31:01 -0400 Subject: [PATCH 2/2] deploy yml update --- .github/workflows/deploy.yml | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 67e3c0b..6ac5e77 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -1,20 +1,21 @@ -# Create this file in your repo: .github/workflows/deploy.yml name: Deploy on: push: - branches: [ main ] + branches: [ main ] # or 'master' if that's your main branch jobs: deploy: runs-on: ubuntu-latest steps: - - name: Deploy to Digital Ocean - uses: appleboy/ssh-action@master + - name: Deploy to Server + uses: appleboy/ssh-action@v1.0.0 with: host: ${{ secrets.HOST }} username: root key: ${{ secrets.SSH_PRIVATE_KEY }} script: | cd /var/www/valkyrie - ./deploy.sh \ No newline at end of file + git pull + npm install + pm2 reload all || pm2 start server/server.js --name "valkyrie" \ No newline at end of file