1- name : Build, Test and Deploy Discord Bot to VPS
1+ name : Build, Test and Deploy Discord Bot to VPS but shorter
22
33on :
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'
12-
13- env :
14- REGISTRY : ghcr.io
15- IMAGE_NAME : ${{ github.repository }}
4+ workflow_dispatch : # Allow manual deployment
165
176jobs :
18- build-test- deploy :
7+ deploy :
198 runs-on : ubuntu-latest
20- permissions :
21- contents : read
22- packages : write
239
2410 steps :
25- - name : Checkout code
26- uses : actions/checkout@v4
27-
28- - name : Set up Node
29- uses : actions/setup-node@v4
30- id : setup-node
31- with :
32- node-version-file : .nvmrc
33-
34- - name : Store Node version
35- run : |
36- NODE_VERSION="${{ steps.setup-node.outputs.node-version }}"
37- echo "NODE_VERSION=${NODE_VERSION#v}" >> $GITHUB_ENV
38-
39- - name : Install pnpm
40- uses : pnpm/action-setup@v4
41- with :
42- version : 10.17.1
43-
44- - name : Get pnpm store directory
45- shell : bash
46- run : |
47- echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
48-
49- - name : Setup pnpm cache
50- uses : actions/cache@v4
51- with :
52- path : ${{ env.STORE_PATH }}
53- key : ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
54- restore-keys : |
55- ${{ runner.os }}-pnpm-store-
56-
57- - name : Install dependencies
58- run : pnpm install --frozen-lockfile
59-
60- - name : Lint
61- run : pnpm run lint
62-
63- - name : Build bot
64- run : pnpm run build:ci
65-
66- - name : Run tests
67- run : pnpm run test:ci
68-
69- - name : Set up Docker Buildx
70- uses : docker/setup-buildx-action@v3
71-
72- - name : Log in to GitHub Container Registry
73- uses : docker/login-action@v3
74- with :
75- registry : ${{ env.REGISTRY }}
76- username : ${{ github.actor }}
77- password : ${{ secrets.GITHUB_TOKEN }}
78-
79- - name : Extract metadata for Docker
80- id : meta
81- uses : docker/metadata-action@v5
82- with :
83- images : ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
84- tags : |
85- type=sha,prefix={{branch}}-
86- type=ref,event=branch
87- type=raw,value=latest,enable={{is_default_branch}}
88-
89- - name : Log Step outputs
90- run : |
91- echo "Node version: ${{ steps.setup-node.outputs.node-version }}"
92- echo "NODE_VERSION: ${{ env.NODE_VERSION }}"
93- echo "Tags: ${{ steps.meta.outputs.tags }}"
94- echo "Labels: ${{ steps.meta.outputs.labels }}"
95-
96- - name : Build and push Docker image
97- uses : docker/build-push-action@v5
98- with :
99- context : .
100- target : production
101- push : true
102- tags : ${{ steps.meta.outputs.tags }}
103- labels : ${{ steps.meta.outputs.labels }}
104- cache-from : type=gha
105- cache-to : type=gha,mode=max
106- build-args : |
107- NODE_VERSION=${{ env.NODE_VERSION }}
108-
109- - name : Deploy to VPS
110- uses : appleboy/scp-action@v0.1.7
111- with :
112- host : ${{ secrets.VPS_HOST }}
113- username : ${{ secrets.VPS_USER }}
114- key : ${{ secrets.VPS_SSH_KEY }}
115- source : " docker-compose.yml"
116- target : " ~/webdev-bot-deploy"
117- strip_components : 0
118-
119- - name : Start containers on VPS
120- uses : appleboy/ssh-action@v1.0.3
121- with :
122- host : ${{ secrets.VPS_HOST }}
123- username : ${{ secrets.VPS_USER }}
124- key : ${{ secrets.VPS_SSH_KEY }}
125- script : |
126- cd ~/webdev-bot-deploy
127-
128- # Create .env.local file with secrets
129- cat > .env.local << EOF
130- DISCORD_TOKEN=${{ secrets.DISCORD_TOKEN }}
131- CLIENT_ID=${{ secrets.CLIENT_ID }}
132- EOF
133-
134- # Set NODE_VERSION for docker-compose
135- export NODE_VERSION=${{ env.NODE_VERSION }}
136-
137- # Login to GitHub Container Registry
138- echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
139-
140- # Update docker-compose.yml to use the pre-built image
141- cat > docker-compose.override.yml << EOF
142- version: '3.8'
143- services:
144- bot-prod:
145- image: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
146- build: null
147- EOF
148-
149- # Pull the latest image and start
150- docker compose --profile prod pull
151- docker compose --profile prod up -d
152-
153- # Clean up old images
154- docker image prune -af --filter "until=24h"
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
0 commit comments