Skip to content

Commit 3f2252f

Browse files
committed
Add branch auto-deploy workflow for VPS
1 parent 2163f71 commit 3f2252f

2 files changed

Lines changed: 92 additions & 0 deletions

File tree

.github/workflows/deploy.yml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
name: Deploy to VPS
2+
3+
on:
4+
push:
5+
branches:
6+
- web-application-version
7+
workflow_dispatch:
8+
9+
concurrency:
10+
group: deploy-web-application-version
11+
cancel-in-progress: false
12+
13+
jobs:
14+
deploy:
15+
runs-on: ubuntu-latest
16+
environment: production
17+
18+
steps:
19+
- name: Deploy over SSH
20+
uses: appleboy/ssh-action@v1.2.0
21+
env:
22+
DEPLOY_BRANCH: web-application-version
23+
DEPLOY_PATH: ${{ vars.DEPLOY_PATH }}
24+
IMAGE_NAME: ${{ vars.IMAGE_NAME }}
25+
CONTAINER_NAME: ${{ vars.CONTAINER_NAME }}
26+
APP_PORT: ${{ vars.APP_PORT }}
27+
with:
28+
host: ${{ secrets.VPS_HOST }}
29+
username: ${{ secrets.VPS_USER }}
30+
key: ${{ secrets.VPS_SSH_KEY }}
31+
port: ${{ secrets.VPS_PORT }}
32+
script_stop: true
33+
envs: DEPLOY_BRANCH,DEPLOY_PATH,IMAGE_NAME,CONTAINER_NAME,APP_PORT
34+
script: |
35+
set -euo pipefail
36+
37+
cd "$DEPLOY_PATH"
38+
git fetch origin "$DEPLOY_BRANCH"
39+
git checkout "$DEPLOY_BRANCH"
40+
git reset --hard "origin/$DEPLOY_BRANCH"
41+
42+
docker build -t "$IMAGE_NAME" .
43+
44+
docker rm -f "${CONTAINER_NAME}_migrate" >/dev/null 2>&1 || true
45+
46+
docker run --rm \
47+
--name "${CONTAINER_NAME}_migrate" \
48+
--env-file ~/app/.env \
49+
-e RUN_MIGRATIONS=1 \
50+
"$IMAGE_NAME" true
51+
52+
docker rm -f "$CONTAINER_NAME" >/dev/null 2>&1 || true
53+
54+
docker run -d \
55+
--name "$CONTAINER_NAME" \
56+
--restart unless-stopped \
57+
--env-file ~/app/.env \
58+
-p "$APP_PORT:8000" \
59+
"$IMAGE_NAME"
60+
61+
docker image prune -f

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,34 @@ App endpoint:
144144
When `RUN_MIGRATIONS=1`, set at least `DB_USER`, `DB_PASSWORD`, and `DB_NAME` (and usually `DB_HOST`/`DB_PORT`).
145145
For production, prefer explicit one-off migrations during deploys to avoid concurrent migration runners.
146146

147+
---
148+
149+
### GitHub auto-deploy to VPS
150+
151+
A workflow is available at `.github/workflows/deploy.yml`.
152+
It deploys automatically on every push to `web-application-version` (including merges into that branch), and can also be started manually from Actions (`workflow_dispatch`).
153+
154+
Required GitHub **Secrets**:
155+
156+
- `VPS_HOST`
157+
- `VPS_PORT`
158+
- `VPS_USER`
159+
- `VPS_SSH_KEY` (private key content)
160+
161+
Required GitHub **Variables**:
162+
163+
- `DEPLOY_PATH` (absolute path of repo on VPS, e.g. `/opt/ongoing-bps-state`)
164+
- `IMAGE_NAME` (e.g. `ongoing-bps-state:prod`)
165+
- `CONTAINER_NAME` (e.g. `ongoing-bps-state`)
166+
- `APP_PORT` (e.g. `8000`)
167+
168+
What the pipeline does on VPS:
169+
170+
1. Pulls the latest code of `web-application-version`.
171+
2. Builds the Docker image.
172+
3. Runs migrations by starting a short-lived container with `RUN_MIGRATIONS=1`.
173+
4. Replaces the running app container with the new image.
174+
175+
VPS requirement:
176+
177+
- `~/app/.env` must exist on the server and include DB settings (`DB_USER`, `DB_PASSWORD`, `DB_HOST`, `DB_PORT`, `DB_NAME`).

0 commit comments

Comments
 (0)