部署迁移:VPS 本机构建 → GitHub Actions 构建镜像推送 ghcr.io
CI 新增 build-and-push job,测试通过后构建 backend/frontend 镜像推送 ghcr.io。 VPS 部署改为 docker compose pull + up -d,不再本地编译。
This commit is contained in:
@@ -54,10 +54,56 @@ jobs:
|
|||||||
- name: Build
|
- name: Build
|
||||||
run: npm run build
|
run: npm run build
|
||||||
|
|
||||||
deploy:
|
build-and-push:
|
||||||
needs: [python-quality, frontend-quality]
|
needs: [python-quality, frontend-quality]
|
||||||
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
packages: write
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Login to GHCR
|
||||||
|
uses: docker/login-action@v3
|
||||||
|
with:
|
||||||
|
registry: ghcr.io
|
||||||
|
username: ${{ github.actor }}
|
||||||
|
password: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
- name: Build and push backend image
|
||||||
|
uses: docker/build-push-action@v6
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
push: true
|
||||||
|
tags: |
|
||||||
|
ghcr.io/yangyuan-zhen/polyweather-backend:latest
|
||||||
|
ghcr.io/yangyuan-zhen/polyweather-backend:${{ github.sha }}
|
||||||
|
cache-from: type=gha
|
||||||
|
cache-to: type=gha,mode=max
|
||||||
|
|
||||||
|
- name: Build and push frontend image
|
||||||
|
uses: docker/build-push-action@v6
|
||||||
|
with:
|
||||||
|
context: ./frontend
|
||||||
|
push: true
|
||||||
|
tags: |
|
||||||
|
ghcr.io/yangyuan-zhen/polyweather-frontend:latest
|
||||||
|
ghcr.io/yangyuan-zhen/polyweather-frontend:${{ github.sha }}
|
||||||
|
build-args: |
|
||||||
|
NEXT_PUBLIC_SUPABASE_URL=${{ secrets.NEXT_PUBLIC_SUPABASE_URL }}
|
||||||
|
NEXT_PUBLIC_SUPABASE_ANON_KEY=${{ secrets.NEXT_PUBLIC_SUPABASE_ANON_KEY }}
|
||||||
|
NEXT_PUBLIC_SITE_URL=${{ secrets.NEXT_PUBLIC_SITE_URL || 'https://polyweather.top' }}
|
||||||
|
NEXT_PUBLIC_POLYWEATHER_API_BASE_URL=${{ secrets.NEXT_PUBLIC_POLYWEATHER_API_BASE_URL || 'https://api.polyweather.top' }}
|
||||||
|
NEXT_PUBLIC_POLYWEATHER_LOCAL_FULL_ACCESS=false
|
||||||
|
cache-from: type=gha
|
||||||
|
cache-to: type=gha,mode=max
|
||||||
|
|
||||||
|
deploy:
|
||||||
|
needs: [build-and-push]
|
||||||
|
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
||||||
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Deploy to VPS
|
- name: Deploy to VPS
|
||||||
run: |
|
run: |
|
||||||
@@ -65,10 +111,11 @@ jobs:
|
|||||||
echo "${{ secrets.VPS_SSH_KEY }}" > ~/.ssh/id_rsa
|
echo "${{ secrets.VPS_SSH_KEY }}" > ~/.ssh/id_rsa
|
||||||
chmod 600 ~/.ssh/id_rsa
|
chmod 600 ~/.ssh/id_rsa
|
||||||
ssh -o StrictHostKeyChecking=accept-new ${{ secrets.VPS_USER }}@${{ secrets.VPS_HOST }} "
|
ssh -o StrictHostKeyChecking=accept-new ${{ secrets.VPS_USER }}@${{ secrets.VPS_HOST }} "
|
||||||
|
echo '${{ secrets.GHCR_PAT }}' | docker login ghcr.io -u yangyuan-zhen --password-stdin
|
||||||
cd /root/PolyWeather
|
cd /root/PolyWeather
|
||||||
git fetch origin main && git reset --hard origin/main
|
git fetch origin main && git reset --hard origin/main
|
||||||
docker compose down
|
docker compose pull
|
||||||
docker compose up -d --build
|
docker compose up -d
|
||||||
sleep 10
|
sleep 10
|
||||||
curl -s http://localhost:8000/healthz
|
curl -s http://localhost:8000/healthz
|
||||||
"
|
"
|
||||||
|
|||||||
+4
-15
@@ -1,6 +1,5 @@
|
|||||||
services:
|
services:
|
||||||
polyweather:
|
polyweather:
|
||||||
build: .
|
|
||||||
container_name: polyweather_bot
|
container_name: polyweather_bot
|
||||||
cpus: ${POLYWEATHER_BOT_CPUS:-0.75}
|
cpus: ${POLYWEATHER_BOT_CPUS:-0.75}
|
||||||
env_file: &id001
|
env_file: &id001
|
||||||
@@ -18,7 +17,7 @@ services:
|
|||||||
- import sqlite3; c=sqlite3.connect('/var/lib/polyweather/polyweather.db');
|
- import sqlite3; c=sqlite3.connect('/var/lib/polyweather/polyweather.db');
|
||||||
c.execute('SELECT 1'); c.close()
|
c.execute('SELECT 1'); c.close()
|
||||||
timeout: 10s
|
timeout: 10s
|
||||||
image: polyweather-app:latest
|
image: ghcr.io/yangyuan-zhen/polyweather-backend:latest
|
||||||
mem_limit: ${POLYWEATHER_BOT_MEM_LIMIT:-768m}
|
mem_limit: ${POLYWEATHER_BOT_MEM_LIMIT:-768m}
|
||||||
memswap_limit: ${POLYWEATHER_BOT_MEMSWAP_LIMIT:-1g}
|
memswap_limit: ${POLYWEATHER_BOT_MEMSWAP_LIMIT:-1g}
|
||||||
pids_limit: 256
|
pids_limit: 256
|
||||||
@@ -29,15 +28,6 @@ services:
|
|||||||
- ${POLYWEATHER_RUNTIME_DATA_DIR:-/var/lib/polyweather}:/app/data
|
- ${POLYWEATHER_RUNTIME_DATA_DIR:-/var/lib/polyweather}:/app/data
|
||||||
- ./bot.log:/app/bot.log
|
- ./bot.log:/app/bot.log
|
||||||
polyweather_frontend:
|
polyweather_frontend:
|
||||||
build:
|
|
||||||
args:
|
|
||||||
NEXT_PUBLIC_POLYWEATHER_API_BASE_URL: https://api.polyweather.top
|
|
||||||
NEXT_PUBLIC_POLYWEATHER_LOCAL_FULL_ACCESS: 'false'
|
|
||||||
NEXT_PUBLIC_SITE_URL: https://polyweather.top
|
|
||||||
NEXT_PUBLIC_SUPABASE_ANON_KEY: ''
|
|
||||||
NEXT_PUBLIC_SUPABASE_URL: ''
|
|
||||||
context: ./frontend
|
|
||||||
dockerfile: Dockerfile
|
|
||||||
container_name: polyweather_frontend
|
container_name: polyweather_frontend
|
||||||
environment:
|
environment:
|
||||||
NEXT_PUBLIC_POLYWEATHER_API_BASE_URL: ${NEXT_PUBLIC_POLYWEATHER_API_BASE_URL:-}
|
NEXT_PUBLIC_POLYWEATHER_API_BASE_URL: ${NEXT_PUBLIC_POLYWEATHER_API_BASE_URL:-}
|
||||||
@@ -57,11 +47,11 @@ services:
|
|||||||
- -qO-
|
- -qO-
|
||||||
- http://localhost:3000
|
- http://localhost:3000
|
||||||
timeout: 5s
|
timeout: 5s
|
||||||
|
image: ghcr.io/yangyuan-zhen/polyweather-frontend:latest
|
||||||
ports:
|
ports:
|
||||||
- 3001:3000
|
- 3001:3000
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
polyweather_web:
|
polyweather_web:
|
||||||
build: .
|
|
||||||
command: python web/app.py
|
command: python web/app.py
|
||||||
container_name: polyweather_web
|
container_name: polyweather_web
|
||||||
env_file: *id001
|
env_file: *id001
|
||||||
@@ -74,7 +64,7 @@ services:
|
|||||||
- -c
|
- -c
|
||||||
- from urllib.request import urlopen; urlopen('http://localhost:8000/healthz')
|
- from urllib.request import urlopen; urlopen('http://localhost:8000/healthz')
|
||||||
timeout: 5s
|
timeout: 5s
|
||||||
image: polyweather-app:latest
|
image: ghcr.io/yangyuan-zhen/polyweather-backend:latest
|
||||||
ports:
|
ports:
|
||||||
- 8000:8000
|
- 8000:8000
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
@@ -83,6 +73,5 @@ services:
|
|||||||
- ${POLYWEATHER_RUNTIME_DATA_DIR:-/var/lib/polyweather}:/var/lib/polyweather
|
- ${POLYWEATHER_RUNTIME_DATA_DIR:-/var/lib/polyweather}:/var/lib/polyweather
|
||||||
- ${POLYWEATHER_RUNTIME_DATA_DIR:-/var/lib/polyweather}:/app/data
|
- ${POLYWEATHER_RUNTIME_DATA_DIR:-/var/lib/polyweather}:/app/data
|
||||||
x-polyweather-base:
|
x-polyweather-base:
|
||||||
build: .
|
|
||||||
env_file: *id001
|
env_file: *id001
|
||||||
image: polyweather-app:latest
|
image: ghcr.io/yangyuan-zhen/polyweather-backend:latest
|
||||||
|
|||||||
Reference in New Issue
Block a user