1010 # - '.gitignore'
1111 # - 'LICENSE'
1212
13+ env :
14+ REGISTRY : ghcr.io
15+ IMAGE_NAME : ${{ github.repository }}
16+
1317jobs :
1418 build-test-deploy :
1519 runs-on : ubuntu-latest
20+ permissions :
21+ contents : read
22+ packages : write
1623
1724 steps :
1825 - name : Checkout code
@@ -23,52 +30,93 @@ jobs:
2330 with :
2431 node-version-file : .nvmrc
2532
26- - name : Install dependencies
33+ - name : Install pnpm
34+ uses : pnpm/action-setup@v4
35+ with :
36+ version : 10.17.1
37+
38+ - name : Get pnpm store directory
39+ shell : bash
2740 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
41+ echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
42+
43+ - name : Setup pnpm cache
44+ uses : actions/cache@v4
45+ with :
46+ path : ${{ env.STORE_PATH }}
47+ key : ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
48+ restore-keys : |
49+ ${{ runner.os }}-pnpm-store-
50+
51+ - name : Install dependencies
52+ run : pnpm install --frozen-lockfile
3353
3454 - name : Lint
35- run : npm run lint
55+ run : pnpm run lint
3656
3757 - name : Build bot
38- run : npm run build:ci
58+ run : pnpm run build:ci
3959
4060 - name : Run tests
41- run : npm run test:ci
61+ run : pnpm run test:ci
4262
43- - name : Package build output
44- run : tar czf bot-build.tar.gz ./dist
63+ - name : Set up Docker Buildx
64+ uses : docker/setup-buildx-action@v3
4565
46- - name : Copy build artifact to VPS
47- uses : appleboy/scp -action@v0.1.7
66+ - name : Log in to GitHub Container Registry
67+ uses : docker/login -action@v3
4868 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/"
69+ registry : ${{ env.REGISTRY }}
70+ username : ${{ github.actor }}
71+ password : ${{ secrets.GITHUB_TOKEN }}
5472
55- - name : Create .env file on VPS
56- 73+ - name : Extract metadata for Docker
74+ id : meta
75+ uses : docker/metadata-action@v5
5776 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
77+ images : ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
78+ tags : |
79+ type=sha,prefix={{branch}}-
80+ type=ref,event=branch
81+ type=raw,value=latest,enable={{is_default_branch}}
82+
83+ - name : Build and push Docker image
84+ uses : docker/build-push-action@v5
85+ with :
86+ context : .
87+ target : production
88+ push : true
89+ tags : ${{ steps.meta.outputs.tags }}
90+ labels : ${{ steps.meta.outputs.labels }}
91+ cache-from : type=gha
92+ cache-to : type=gha,mode=max
93+ build-args : |
94+ NODE_VERSION=${{ steps.setup-node.outputs.node-version }}
6495
65- - name : Extract and restart bot on VPS
96+ - name : Deploy to VPS
66976798 with :
6899 host : ${{ secrets.VPS_HOST }}
69100 username : ${{ secrets.VPS_USER }}
70101 key : ${{ secrets.VPS_SSH_KEY }}
71102 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
103+ # Login to GitHub Container Registry
104+ echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
105+
106+ # Pull the latest image
107+ docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
108+
109+ # Stop and remove old container if it exists
110+ docker stop webdev-bot-prod 2>/dev/null || true
111+ docker rm webdev-bot-prod 2>/dev/null || true
112+
113+ # Run new container
114+ docker run -d \
115+ --name webdev-bot-prod \
116+ --restart unless-stopped \
117+ -e DISCORD_TOKEN="${{ secrets.DISCORD_TOKEN }}" \
118+ -e CLIENT_ID="${{ secrets.CLIENT_ID }}" \
119+ ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
120+
121+ # Clean up old images
122+ docker image prune -af --filter "until=24h"
0 commit comments