Skip to content

Commit 04c48b7

Browse files
authored
ci: rework the deployment workflow with docker (#14)
* chore: update pnpm version and enhance deployment workflow * ci: add id to Node setup step in deployment workflow * ci: add logging * ci: store Node version in environment for deployment workflow * chore: update Dockerfile to install production dependencies only * chore: update Dockerfile to ignore scripts during production dependency installation and add TypeScript to dependencies * chore: update TypeScript version in pnpm-lock.yaml * ci: enhance deployment workflow to include file transfer and container management on VPS * ci: add new deployment workflow for Discord bot to streamline VPS updates * ci: simplify deployment workflow for Discord bot by consolidating steps and improving container management on VPS * ci: update deployment workflow to include .env.local file creation with secrets for Discord bot * ci: read Node version from .nvmrc and set it in the environment for deployment workflow * ci: update deployment workflow to use 'docker compose' command for improved compatibility * ci: read Node version from .nvmrc and output it during deployment workflow * ci: remove version specification from docker-compose.yml for streamlined configuration * chore: update TypeScript version to 5.9.3 in package.json and pnpm-lock.yaml * chore: modify Dockerfile to ignore scripts during pnpm install for improved build efficiency * ci: update deployment workflow to switch to 'ci/deploy-with-docker' branch for pulling latest changes * ci: enhance docker-compose.yml to include guides tracker configuration and persist data * chore: add data directory creation and permissions setup for node user in Dockerfile * feat: enhance command deployment logic to support both global and guild-specific commands based on server ID * feat: add server ID configuration to Discord settings for enhanced command handling * feat: add SERVER_ID to deployment workflow for Discord integration * chore: update dependencies in pnpm-lock.yaml * refactor: remove guildId from deployCommands function to streamline command deployment * feat: add additional configuration variables for repel command in deployment workflow * fix: update activity name in client presence for improved clarity * chore: update deployment workflow to pull from main branch instead of ci/deploy-with-docker * chore: remove SERVER_ID from Discord configuration in env.ts
1 parent d0a845b commit 04c48b7

7 files changed

Lines changed: 363 additions & 329 deletions

File tree

.github/workflows/deploy.yml

Lines changed: 52 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,60 @@
11
name: Build, Test and Deploy Discord Bot to VPS
22

33
on:
4-
workflow_dispatch: # Manual trigger only
5-
# Uncomment below when VPS is ready:
6-
# push:
7-
# branches: [ "main" ]
8-
# paths-ignore:
9-
# - 'docs/**'
10-
# - '.gitignore'
11-
# - 'LICENSE'
4+
push:
5+
branches: [ "main" ]
6+
workflow_dispatch: # Allow manual deployment
127

138
jobs:
14-
build-test-deploy:
9+
deploy:
1510
runs-on: ubuntu-latest
1611

1712
steps:
18-
- name: Checkout code
19-
uses: actions/checkout@v4
20-
21-
- name: Set up Node
22-
uses: actions/setup-node@v4
23-
with:
24-
node-version-file: .nvmrc
25-
26-
- name: Install dependencies
27-
run: |
28-
if [ -f package-lock.json ]; then
29-
npm ci --no-audit --no-fund
30-
else
31-
npm install --no-audit --no-fund
32-
fi
33-
34-
- name: Lint
35-
run: npm run lint
36-
37-
- name: Build bot
38-
run: npm run build:ci
39-
40-
- name: Run tests
41-
run: npm run test:ci
42-
43-
- name: Package build output
44-
run: tar czf bot-build.tar.gz ./dist
45-
46-
- name: Copy build artifact to VPS
47-
uses: appleboy/scp-action@v0.1.7
48-
with:
49-
host: ${{ secrets.VPS_HOST }}
50-
username: ${{ secrets.VPS_USER }}
51-
key: ${{ secrets.VPS_SSH_KEY }}
52-
source: "bot-build.tar.gz"
53-
target: "/home/${{ secrets.VPS_USER }}/discord-bot/"
54-
55-
- name: Create .env file on VPS
56-
uses: appleboy/ssh-action@v1.0.3
57-
with:
58-
host: ${{ secrets.VPS_HOST }}
59-
username: ${{ secrets.VPS_USER }}
60-
key: ${{ secrets.VPS_SSH_KEY }}
61-
script: |
62-
echo "DISCORD_TOKEN=${{ secrets.DISCORD_TOKEN }}" > /home/${{ secrets.VPS_USER }}/discord-bot/.env
63-
echo "CLIENT_ID=${{ secrets.CLIENT_ID }}" >> /home/${{ secrets.VPS_USER }}/discord-bot/.env
64-
65-
- name: Extract and restart bot on VPS
66-
uses: appleboy/ssh-action@v1.0.3
67-
with:
68-
host: ${{ secrets.VPS_HOST }}
69-
username: ${{ secrets.VPS_USER }}
70-
key: ${{ secrets.VPS_SSH_KEY }}
71-
script: |
72-
cd /home/${{ secrets.VPS_USER }}/discord-bot/
73-
tar xzf bot-build.tar.gz
74-
pm2 restart bot || pm2 start ./dist/index.js --name bot
13+
- name: Checkout code
14+
uses: actions/checkout@v4
15+
16+
- name: Read Node version
17+
run: |
18+
NODE_VERSION=$(cat .nvmrc | sed 's/v//')
19+
echo "NODE_VERSION=$NODE_VERSION" >> $GITHUB_ENV
20+
21+
- name: Deploy to VPS
22+
uses: appleboy/ssh-action@v1.0.3
23+
with:
24+
host: ${{ secrets.VPS_HOST }}
25+
username: ${{ secrets.VPS_USER }}
26+
key: ${{ secrets.VPS_SSH_KEY }}
27+
script: |
28+
cd /home/${{ secrets.VPS_USER }}/webdev-bot-deploy
29+
30+
# Stash any local changes
31+
git stash push -m "Auto-deploy $(date)" 2>/dev/null || true
32+
33+
# Pull latest changes
34+
git checkout main
35+
git pull origin main
36+
37+
# Read NODE_VERSION from .nvmrc
38+
export NODE_VERSION=$(cat .nvmrc | sed 's/v//')
39+
echo "Using Node version: $NODE_VERSION"
40+
41+
# Create .env.local file with secrets
42+
cat > .env.local << EOF
43+
DISCORD_TOKEN=${{ secrets.DISCORD_TOKEN }}
44+
CLIENT_ID=${{ secrets.CLIENT_ID }}
45+
GUIDES_CHANNEL_ID=${{ secrets.GUIDES_CHANNEL_ID }}
46+
SERVER_ID=${{ secrets.SERVER_ID }}
47+
REPEL_LOG_CHANNEL_ID=${{ secrets.REPEL_LOG_CHANNEL_ID }}
48+
REPEL_ROLE_ID=${{ secrets.REPEL_ROLE_ID }}
49+
MODERATORS_ROLE_IDS=${{ secrets.MODERATORS_ROLE_IDS }}
50+
EOF
51+
52+
# Stop any existing containers
53+
docker compose down || true
54+
55+
# Build and start production container with profile
56+
docker compose --profile prod up -d --build
57+
58+
# Check status
59+
echo "Deployment completed. Container status:"
60+
docker compose ps
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Build, Test and Deploy Discord Bot to VPS but shorter
2+
3+
on:
4+
workflow_dispatch: # Allow manual deployment
5+
6+
jobs:
7+
deploy:
8+
runs-on: ubuntu-latest
9+
10+
steps:
11+
- name: Checkout code
12+
uses: actions/checkout@v4
13+
14+
- name: Deploy to VPS
15+
uses: appleboy/ssh-action@v1.0.3
16+
with:
17+
host: ${{ secrets.VPS_HOST }}
18+
username: ${{ secrets.VPS_USER }}
19+
key: ${{ secrets.VPS_SSH_KEY }}
20+
script: |
21+
cd /home/${{ secrets.VPS_USER }}/webdev-bot
22+
23+
# Stash any local changes
24+
git stash push -m "Auto-deploy $(date)" 2>/dev/null || true
25+
26+
# Pull latest changes
27+
git checkout main
28+
git pull origin main
29+
30+
# Stop any existing containers
31+
docker-compose down || true
32+
33+
# Build and start production container with profile
34+
docker-compose --profile prod up -d --build
35+
36+
# Check status
37+
echo "Deployment completed. Container status:"
38+
docker-compose ps

Dockerfile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ FROM base AS deps
1313

1414
COPY package.json pnpm-lock.yaml ./
1515

16-
RUN pnpm install --prod --frozen-lockfile
16+
RUN pnpm install --frozen-lockfile --production --ignore-scripts
1717

1818
# Dev dependencies stage - Install all dependencies
1919
FROM base AS deps-dev
2020

2121
COPY package.json pnpm-lock.yaml ./
2222

23-
RUN pnpm install --frozen-lockfile
23+
RUN pnpm install --frozen-lockfile --ignore-scripts
2424

2525
# Build stage - Compile TypeScript
2626
FROM deps-dev AS build
@@ -41,6 +41,9 @@ COPY --from=deps /app/node_modules ./node_modules
4141
COPY --from=build /app/dist ./dist
4242
COPY package.json ./
4343

44+
# Create data directory and set permissions for node user
45+
RUN mkdir -p /app/data && chown -R node:node /app/data
46+
4447
# Run as non-root user for security
4548
USER node
4649

@@ -53,6 +56,9 @@ ENV NODE_ENV=development
5356

5457
COPY . .
5558

59+
# Create data directory and set permissions for node user
60+
RUN mkdir -p /app/data && chown -R node:node /app/data
61+
5662
# Run as non-root user for security
5763
USER node
5864

docker-compose.yml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
version: '3.8'
2-
31
services:
42
# Production service - optimized runtime
53
bot-prod:
@@ -12,6 +10,11 @@ services:
1210
restart: unless-stopped
1311
env_file:
1412
- .env.local
13+
environment:
14+
- GUIDES_TRACKER_PATH=/app/data/guides-tracker.json
15+
volumes:
16+
# Persist guides tracker data
17+
- guides-data:/app/data
1518
profiles:
1619
- prod
1720

@@ -26,6 +29,8 @@ services:
2629
restart: unless-stopped
2730
env_file:
2831
- .env.local
32+
environment:
33+
- GUIDES_TRACKER_PATH=/app/data/guides-tracker.json
2934
volumes:
3035
# Mount source code for hot reload
3136
- ./src:/app/src:ro
@@ -35,6 +40,11 @@ services:
3540
# Mount package files (in case dependencies change)
3641
- ./package.json:/app/package.json:ro
3742
- ./pnpm-lock.yaml:/app/pnpm-lock.yaml:ro
43+
# Persist guides tracker data
44+
- guides-data:/app/data
3845
profiles:
3946
- dev
4047

48+
volumes:
49+
guides-data:
50+
driver: local

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"packageManager": "pnpm@10.17.1",
3535
"dependencies": {
3636
"discord.js": "^14.22.1",
37+
"typescript": "^5.9.3",
3738
"web-features": "^3.3.0"
3839
},
3940
"devDependencies": {
@@ -42,8 +43,7 @@
4243
"husky": "^9.1.7",
4344
"lint-staged": "^16.2.1",
4445
"tsup": "^8.5.0",
45-
"tsx": "^4.20.6",
46-
"typescript": "^5.9.2"
46+
"tsx": "^4.20.6"
4747
},
4848
"lint-staged": {
4949
"*.{ts,js}": [

0 commit comments

Comments
 (0)