mirror of
https://github.com/woodchen-ink/aimodels-prices.git
synced 2025-07-18 05:32:00 +08:00
184 lines
5.8 KiB
YAML
184 lines
5.8 KiB
YAML
name: Docker Build and Push
|
|
|
|
on:
|
|
push:
|
|
branches: [ "main" ]
|
|
tags: [ 'v*.*.*' ]
|
|
pull_request:
|
|
branches: [ "main" ]
|
|
|
|
env:
|
|
DOCKER_HUB_USERNAME: ${{ secrets.DOCKER_HUB_USERNAME }}
|
|
IMAGE_NAME: ${{ secrets.DOCKER_HUB_USERNAME }}/aimodels-prices
|
|
|
|
jobs:
|
|
build:
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
- name: Checkout repository
|
|
uses: actions/checkout@v4
|
|
|
|
# 设置 Go 环境
|
|
- name: Set up Go
|
|
uses: actions/setup-go@v5
|
|
with:
|
|
go-version: '1.23'
|
|
|
|
# 构建后端(使用 Alpine 环境)
|
|
- name: Build backend
|
|
run: |
|
|
cd backend
|
|
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o main-amd64 .
|
|
GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -o main-arm64 .
|
|
|
|
# 设置 Node.js 环境
|
|
- name: Set up Node.js
|
|
uses: actions/setup-node@v4
|
|
with:
|
|
node-version: '22'
|
|
cache: 'npm'
|
|
cache-dependency-path: frontend/package-lock.json
|
|
|
|
# 构建前端
|
|
- name: Build frontend
|
|
run: |
|
|
cd frontend
|
|
npm ci
|
|
npm run build
|
|
|
|
# 创建 nginx 配置
|
|
- name: Create nginx config
|
|
run: |
|
|
mkdir -p backend/config
|
|
cat > backend/config/nginx.conf << 'EOL'
|
|
user nginx;
|
|
worker_processes auto;
|
|
|
|
error_log /var/log/nginx/error.log notice;
|
|
pid /var/run/nginx.pid;
|
|
|
|
events {
|
|
worker_connections 1024;
|
|
}
|
|
|
|
http {
|
|
include /etc/nginx/mime.types;
|
|
default_type application/octet-stream;
|
|
|
|
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
|
|
'$status $body_bytes_sent "$http_referer" '
|
|
'"$http_user_agent" "$http_x_forwarded_for"';
|
|
|
|
access_log /var/log/nginx/access.log main;
|
|
|
|
sendfile on;
|
|
tcp_nopush on;
|
|
tcp_nodelay on;
|
|
|
|
keepalive_timeout 65;
|
|
|
|
gzip on;
|
|
gzip_min_length 1k;
|
|
gzip_comp_level 6;
|
|
gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
|
|
gzip_vary on;
|
|
|
|
server {
|
|
listen 80;
|
|
server_name localhost;
|
|
root /app/frontend;
|
|
index index.html;
|
|
|
|
# 前端文件缓存设置
|
|
location /assets {
|
|
expires 7d;
|
|
add_header Cache-Control "public, no-transform";
|
|
}
|
|
|
|
# API 反向代理
|
|
location /api {
|
|
proxy_pass http://127.0.0.1:8080;
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Upgrade $http_upgrade;
|
|
proxy_set_header Connection 'upgrade';
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
proxy_cache_bypass $http_upgrade;
|
|
proxy_buffering off;
|
|
proxy_read_timeout 300s;
|
|
}
|
|
|
|
# SPA 路由支持
|
|
location / {
|
|
try_files $uri $uri/ /index.html;
|
|
add_header Cache-Control "no-cache, no-store, must-revalidate";
|
|
}
|
|
|
|
# 错误页面
|
|
error_page 500 502 503 504 /50x.html;
|
|
location = /50x.html {
|
|
root /usr/share/nginx/html;
|
|
}
|
|
}
|
|
}
|
|
EOL
|
|
|
|
# 设置 QEMU 以支持多架构构建
|
|
- name: Set up QEMU
|
|
uses: docker/setup-qemu-action@v3
|
|
|
|
# 设置 Docker Buildx
|
|
- name: Set up Docker Buildx
|
|
uses: docker/setup-buildx-action@v3
|
|
|
|
# 登录到 Docker Hub
|
|
- name: Log into Docker Hub
|
|
if: github.event_name != 'pull_request'
|
|
uses: docker/login-action@v3
|
|
with:
|
|
username: ${{ secrets.DOCKER_HUB_USERNAME }}
|
|
password: ${{ secrets.DOCKER_HUB_TOKEN }}
|
|
|
|
# 提取版本信息
|
|
- name: Extract version
|
|
id: version
|
|
run: |
|
|
if [[ $GITHUB_REF == refs/tags/* ]]; then
|
|
echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
|
|
else
|
|
echo "VERSION=latest" >> $GITHUB_OUTPUT
|
|
fi
|
|
|
|
# 构建并推送 Docker 镜像
|
|
- name: Build and push Docker image
|
|
uses: docker/build-push-action@v5
|
|
with:
|
|
context: .
|
|
push: ${{ github.event_name != 'pull_request' }}
|
|
platforms: linux/amd64,linux/arm64
|
|
tags: |
|
|
${{ env.IMAGE_NAME }}:${{ steps.version.outputs.VERSION }}
|
|
${{ env.IMAGE_NAME }}:latest
|
|
cache-from: type=gha
|
|
cache-to: type=gha,mode=max
|
|
|
|
# 部署到服务器
|
|
- name: Deploy to server
|
|
uses: appleboy/ssh-action@master
|
|
if: github.event_name != 'pull_request'
|
|
env:
|
|
DOCKER_IMAGE: ${{ env.IMAGE_NAME }}
|
|
PROJECT_PATH: ${{ secrets.PROJECT_PATH }}
|
|
with:
|
|
host: ${{ secrets.SERVER_IP }}
|
|
username: root
|
|
key: ${{ secrets.SERVER_SSH_KEY }}
|
|
envs: DOCKER_IMAGE,PROJECT_PATH
|
|
script: |
|
|
cd $PROJECT_PATH
|
|
docker compose pull
|
|
docker compose down
|
|
docker compose up -d |