Optimize Dockerfile and deployment workflow with multi-stage build and server auto-deployment

This commit is contained in:
wood chen 2025-02-08 18:35:55 +08:00
parent 85f21a3105
commit afe0d5fc11
12 changed files with 58 additions and 10 deletions

View File

@ -115,4 +115,20 @@ jobs:
${{ env.IMAGE_NAME }}:${{ steps.version.outputs.VERSION }}
${{ env.IMAGE_NAME }}:latest
cache-from: type=gha
cache-to: type=gha,mode=max
cache-to: type=gha,mode=max
- name: 部署到服务器
uses: appleboy/ssh-action@master
env:
DOCKER_IMAGE: ${{ secrets.DOCKER_HUB_USERNAME }}/${{ 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: |
docker pull $DOCKER_IMAGE
cd $PROJECT_PATH
docker compose down
docker compose up -d

View File

@ -1,16 +1,32 @@
# 使用 Alpine 作为基础镜像
# 使用多阶段构建
FROM golang:1.21-alpine AS builder
WORKDIR /build
# 复制后端代码
COPY backend/ .
# 构建后端(禁用 CGO使用纯 Go 构建)
RUN CGO_ENABLED=0 GOOS=linux go build -o main .
# 最终镜像
FROM alpine:3.18
WORKDIR /app
# 安装 nginx
RUN apk add --no-cache nginx
# 安装必要的包
RUN apk add --no-cache \
nginx \
ca-certificates \
tzdata \
bash \
wget
# 创建必要的目录
RUN mkdir -p /app/data /app/frontend
# 复制后端二进制文件
COPY backend/main ./
# 从构建阶段复制后端二进制文件
COPY --from=builder /build/main ./
# 复制 nginx 配置
COPY backend/config/nginx.conf /etc/nginx/nginx.conf

19
docker-compose.yml Normal file
View File

@ -0,0 +1,19 @@
services:
aimodels-prices:
container_name: aimodels-prices
image: woodchen/aimodels-prices:latest
restart: always
environment:
- GIN_MODE=release
- PORT=8080
- TZ=Asia/Shanghai
volumes:
- ./data:/app/data
ports:
- 10124:80
healthcheck:
test: ["CMD", "wget", "-q", "--spider", "http://localhost:80"]
interval: 30s
timeout: 10s
retries: 3
start_period: 20s

Binary file not shown.

Before

Width:  |  Height:  |  Size: 170 KiB

View File

@ -1,10 +1,7 @@
#!/bin/sh
#!/bin/bash
# 启动后端服务
./main &
# 启动前端服务
cd /app/frontend && node server.js &
# 启动 nginx
nginx -g 'daemon off;'