Skip to content

Commit 67351d5

Browse files
committed
ci: implement deployment workflow using GitHub Actions
1 parent c90cbea commit 67351d5

1 file changed

Lines changed: 115 additions & 0 deletions

File tree

.github/workflows/deploy.yml

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
name: Deploy
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
7+
jobs:
8+
build-test-and-deploy:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Checkout
12+
uses: actions/checkout@v4
13+
14+
- name: Setup Node.js
15+
uses: actions/setup-node@v4
16+
with:
17+
node-version-file: .nvmrc
18+
cache: 'pnpm'
19+
20+
- name: Setup pnpm
21+
uses: pnpm/action-setup@v4
22+
with:
23+
version: 10
24+
25+
- name: Install dependencies
26+
run: pnpm install --frozen-lockfile
27+
28+
- name: Lint
29+
run: pnpm run lint
30+
31+
- name: Build
32+
run: pnpm run build
33+
env:
34+
DISCORD_TOKEN: ${{ secrets.DISCORD_TOKEN }}
35+
APPLICATION_ID: ${{ secrets.APPLICATION_ID }}
36+
37+
- name: Run tests
38+
run: pnpm run test
39+
40+
- name: Package artifact
41+
run: |
42+
tar -czf release.tar.gz dist package.json pnpm-lock.yaml .nvmrc
43+
44+
- name: Create .env file from secrets
45+
env:
46+
DISCORD_TOKEN: ${{ secrets.DISCORD_TOKEN }}
47+
APPLICATION_ID: ${{ secrets.APPLICATION_ID }}
48+
run: |
49+
set -euo pipefail
50+
printf "DISCORD_TOKEN=%s\n" "$DISCORD_TOKEN" > .env
51+
printf "APPLICATION_ID=%s\n" "$APPLICATION_ID" >> .env
52+
printf "NODE_ENV=production\n" >> .env
53+
54+
- name: Copy artifact to VPS
55+
env:
56+
SSH_HOST: ${{ secrets.SSH_HOST }}
57+
SSH_USER: ${{ secrets.SSH_USER }}
58+
SSH_PORT: ${{ secrets.SSH_PORT }}
59+
SSH_KEY: ${{ secrets.SSH_KEY }}
60+
run: |
61+
mkdir -p ~/.ssh
62+
echo "$SSH_KEY" > ~/.ssh/id_ed25519
63+
chmod 600 ~/.ssh/id_ed25519
64+
ssh -o StrictHostKeyChecking=no -i ~/.ssh/id_ed25519 -p ${SSH_PORT:-22} $SSH_USER@$SSH_HOST "mkdir -p ~/apps/webdev-bot/releases"
65+
scp -i ~/.ssh/id_ed25519 -P ${SSH_PORT:-22} -o StrictHostKeyChecking=no release.tar.gz $SSH_USER@$SSH_HOST:~/apps/webdev-bot/releases/release.tar.gz
66+
67+
- name: Upload .env to VPS
68+
env:
69+
SSH_HOST: ${{ secrets.SSH_HOST }}
70+
SSH_USER: ${{ secrets.SSH_USER }}
71+
SSH_PORT: ${{ secrets.SSH_PORT }}
72+
SSH_KEY: ${{ secrets.SSH_KEY }}
73+
APP_DIR: ${{ secrets.APP_DIR }}
74+
run: |
75+
mkdir -p ~/.ssh
76+
echo "$SSH_KEY" > ~/.ssh/id_ed25519
77+
chmod 600 ~/.ssh/id_ed25519
78+
ssh -o StrictHostKeyChecking=no -i ~/.ssh/id_ed25519 -p ${SSH_PORT:-22} $SSH_USER@$SSH_HOST "mkdir -p ${APP_DIR:-\"~/apps/webdev-bot\"}/shared && chmod 700 ${APP_DIR:-\"~/apps/webdev-bot\"}/shared"
79+
scp -i ~/.ssh/id_ed25519 -P ${SSH_PORT:-22} -o StrictHostKeyChecking=no .env $SSH_USER@$SSH_HOST:${APP_DIR:-"~/apps/webdev-bot"}/shared/.env
80+
ssh -o StrictHostKeyChecking=no -i ~/.ssh/id_ed25519 -p ${SSH_PORT:-22} $SSH_USER@$SSH_HOST "chmod 600 ${APP_DIR:-\"~/apps/webdev-bot\"}/shared/.env"
81+
82+
- name: Deploy on VPS
83+
env:
84+
SSH_HOST: ${{ secrets.SSH_HOST }}
85+
SSH_USER: ${{ secrets.SSH_USER }}
86+
SSH_PORT: ${{ secrets.SSH_PORT }}
87+
SSH_KEY: ${{ secrets.SSH_KEY }}
88+
APP_DIR: ${{ secrets.APP_DIR }}
89+
run: |
90+
mkdir -p ~/.ssh
91+
echo "$SSH_KEY" > ~/.ssh/id_ed25519
92+
chmod 600 ~/.ssh/id_ed25519
93+
ssh -o StrictHostKeyChecking=no -i ~/.ssh/id_ed25519 -p ${SSH_PORT:-22} $SSH_USER@$SSH_HOST << 'EOF'
94+
set -euo pipefail
95+
APP_DIR=${APP_DIR:-"~/apps/webdev-bot"}
96+
mkdir -p "$APP_DIR/current" "$APP_DIR/releases" "$APP_DIR/shared"
97+
cd "$APP_DIR"
98+
rm -rf current/*
99+
tar -xzf releases/release.tar.gz -C current
100+
cd current
101+
# Load env from shared/.env for the PM2 process
102+
set -a
103+
if [ -f "$APP_DIR/shared/.env" ]; then . "$APP_DIR/shared/.env"; fi
104+
set +a
105+
if command -v pnpm >/dev/null 2>&1; then
106+
pnpm install --prod --frozen-lockfile || true
107+
else
108+
if command -v corepack >/dev/null 2>&1; then corepack enable; fi
109+
npm i -g pnpm@10 || true
110+
pnpm install --prod --frozen-lockfile || true
111+
fi
112+
pm2 describe webdev-bot >/dev/null 2>&1 && pm2 restart webdev-bot || pm2 start "node dist/index.js" --name webdev-bot
113+
pm2 save || true
114+
EOF
115+

0 commit comments

Comments
 (0)