Skip to content

Commit 578ba1b

Browse files
committed
feat(deployment): add GitHub Actions workflow for automated VPS deployment
Introduces a new GitHub Actions workflow to automate deployment to a VPS server. Updates README with detailed setup instructions, including prerequisites, environment variable configuration, and deployment steps. The workflow builds the project, installs dependencies, and manages the PM2 process for seamless updates.
1 parent 0862399 commit 578ba1b

File tree

2 files changed

+159
-1
lines changed

2 files changed

+159
-1
lines changed

.github/workflows/deploy.yml

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
name: Deploy to VPS
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
workflow_dispatch:
8+
9+
jobs:
10+
deploy:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v4
16+
17+
- name: Setup Node.js
18+
uses: actions/setup-node@v4
19+
with:
20+
node-version: '20'
21+
cache: 'npm'
22+
23+
- name: Install dependencies
24+
run: npm ci
25+
26+
- name: Build project
27+
run: npm run build
28+
29+
- name: Create deployment package
30+
run: |
31+
mkdir -p deploy-package
32+
cp -r dist deploy-package/
33+
cp package.json deploy-package/
34+
cp package-lock.json deploy-package/
35+
# Copy any other necessary files (like env.example if needed)
36+
if [ -f "env.example" ]; then
37+
cp env.example deploy-package/
38+
fi
39+
40+
- name: Deploy to VPS
41+
uses: appleboy/[email protected]
42+
with:
43+
host: ${{ secrets.VPS_HOST }}
44+
username: ${{ secrets.VPS_USERNAME }}
45+
password: ${{ secrets.VPS_PASSWORD }}
46+
port: 22
47+
source: "deploy-package/*"
48+
target: "/tmp/tldreply-deploy"
49+
strip_components: 0
50+
51+
- name: SSH and deploy on VPS
52+
uses: appleboy/[email protected]
53+
with:
54+
host: ${{ secrets.VPS_HOST }}
55+
username: ${{ secrets.VPS_USERNAME }}
56+
password: ${{ secrets.VPS_PASSWORD }}
57+
port: 22
58+
script: |
59+
# Create project directory if it doesn't exist
60+
sudo mkdir -p /var/www/tldreply
61+
62+
# Backup current deployment (optional)
63+
if [ -d "/var/www/tldreply/dist" ]; then
64+
sudo cp -r /var/www/tldreply/dist /var/www/tldreply/dist.backup.$(date +%Y%m%d_%H%M%S) || true
65+
fi
66+
67+
# Preserve .env file if it exists
68+
if [ -f "/var/www/tldreply/.env" ]; then
69+
sudo cp /var/www/tldreply/.env /tmp/tldreply-env-backup || true
70+
fi
71+
72+
# Copy new files to project directory
73+
sudo cp -r /tmp/tldreply-deploy/* /var/www/tldreply/
74+
75+
# Restore .env file if it was backed up
76+
if [ -f "/tmp/tldreply-env-backup" ]; then
77+
sudo mv /tmp/tldreply-env-backup /var/www/tldreply/.env
78+
fi
79+
80+
sudo chown -R $USER:$USER /var/www/tldreply
81+
82+
# Navigate to project directory
83+
cd /var/www/tldreply
84+
85+
# Install/update dependencies
86+
npm ci --production
87+
88+
# Restart PM2 process (or start if it doesn't exist)
89+
if pm2 list | grep -q "tldreply-bot"; then
90+
pm2 restart tldreply-bot
91+
else
92+
pm2 start dist/index.js --name tldreply-bot --cwd /var/www/tldreply
93+
fi
94+
95+
# Save PM2 process list
96+
pm2 save
97+
98+
# Cleanup temporary files
99+
rm -rf /tmp/tldreply-deploy
100+
101+
- name: Deployment status
102+
run: |
103+
echo "✅ Deployment completed successfully!"
104+

README.md

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,64 @@ The bot only stores messages it receives after being added to a group. It cannot
182182
6. Bot will auto-deploy!
183183

184184
**Free hosting stack:**
185-
- Railway.app ($5 free credit/month)
186185
- Supabase (free PostgreSQL tier)
187186
- Google Gemini (free AI tier)
188187

188+
## Deploy to VPS with GitHub Actions
189+
190+
This project includes a GitHub Actions workflow for automated deployment to a VPS server.
191+
192+
### Prerequisites
193+
194+
- A VPS server with SSH access
195+
- Node.js and PM2 installed on the VPS
196+
- The project directory `/var/www/tldreply` (will be created automatically)
197+
198+
### Setup
199+
200+
1. **Configure GitHub Secrets:**
201+
Go to your repository → Settings → Secrets and variables → Actions → New repository secret
202+
203+
Add the following secrets:
204+
- `VPS_HOST`: Your VPS IP address (e.g., `111.xxx.x.x`)
205+
- `VPS_USERNAME`: Your VPS username (e.g., `username`)
206+
- `VPS_PASSWORD`: Your VPS password
207+
208+
2. **Initial VPS Setup:**
209+
SSH into your VPS and run:
210+
```bash
211+
# Install Node.js (if not already installed)
212+
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
213+
sudo apt-get install -y nodejs
214+
215+
# Install PM2 globally
216+
sudo npm install -g pm2
217+
218+
# Create project directory (if needed)
219+
sudo mkdir -p /var/www/tldreply
220+
sudo chown -R $USER:$USER /var/www/tldreply
221+
```
222+
223+
3. **Configure Environment Variables:**
224+
On your VPS, create a `.env` file in `/var/www/tldreply`:
225+
```bash
226+
cd /var/www/tldreply
227+
cp env.example .env
228+
nano .env # Edit with your actual values
229+
```
230+
231+
4. **Deploy:**
232+
- Push to the `main` branch to trigger automatic deployment
233+
- Or manually trigger via GitHub Actions → Deploy to VPS → Run workflow
234+
235+
The workflow will:
236+
- Build the TypeScript project
237+
- Deploy to `/var/www/tldreply` on your VPS
238+
- Install production dependencies
239+
- Restart the PM2 process automatically
240+
241+
**Note:** Make sure your VPS user has sudo access and password authentication is enabled for SSH (or configure SSH keys for better security).
242+
189243
## Architecture
190244

191245
- **Bot Framework**: grammY

0 commit comments

Comments
 (0)