Compare commits

...

3 Commits

Author SHA1 Message Date
ac8578b89a 添加 Gitea CI/CD 流水线
Some checks failed
构建并部署前端到生产环境 / build-and-deploy (push) Failing after 14s
Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
2026-04-11 15:29:50 +08:00
18fe9e61b1 添加 Docker 部署配置
Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
2026-04-11 15:29:38 +08:00
40e23a8b78 添加 Vite 构建配置
Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
2026-04-11 15:29:23 +08:00
6 changed files with 218 additions and 1 deletions

View File

@@ -0,0 +1,84 @@
name: 构建并部署前端到生产环境
on:
push:
branches:
- main
- dev
- test
env:
REGISTRY: registry.boss160.cn
IMAGE_NAME: registry.boss160.cn/junhong/device-voice-h5
DEPLOY_DIR: /opt/device_voice_h5
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: 检出代码
run: |
export PATH="$HOME/.nix-profile/bin:/usr/local/bin:/usr/bin:/bin:$PATH"
export GIT_SSL_NO_VERIFY=1
git clone https://git.boss160.cn/luo/device-voice-h5.git .
git checkout ${{ github.sha }}
- name: 设置镜像标签
id: tag
run: |
if [ "${{ github.ref }}" = "refs/heads/main" ]; then
echo "tag=latest" >> $GITHUB_OUTPUT
elif [ "${{ github.ref }}" = "refs/heads/dev" ]; then
echo "tag=dev" >> $GITHUB_OUTPUT
elif [ "${{ github.ref }}" = "refs/heads/test" ]; then
echo "tag=test" >> $GITHUB_OUTPUT
else
echo "tag=unknown" >> $GITHUB_OUTPUT
fi
- name: 登录 Docker Registry
run: |
echo "${{ secrets.REGISTRY_PASSWORD }}" | docker login "${{ env.REGISTRY }}" --username "${{ secrets.REGISTRY_USERNAME }}" --password-stdin
- name: 构建前端镜像
run: |
docker build -t ${{ env.IMAGE_NAME }}:${{ steps.tag.outputs.tag }} .
docker tag ${{ env.IMAGE_NAME }}:${{ steps.tag.outputs.tag }} ${{ env.IMAGE_NAME }}:${{ github.sha }}
- name: 推送镜像到 Registry
run: |
docker push ${{ env.IMAGE_NAME }}:${{ steps.tag.outputs.tag }}
docker push ${{ env.IMAGE_NAME }}:${{ github.sha }}
- name: 部署到本地(仅 main 分支)
if: github.ref == 'refs/heads/main'
run: |
mkdir -p ${{ env.DEPLOY_DIR }}
echo "更新部署配置文件..."
cp -v docker-compose.prod.yml ${{ env.DEPLOY_DIR }}/
cd ${{ env.DEPLOY_DIR }}
echo "拉取最新镜像..."
docker compose -f docker-compose.prod.yml pull
echo "重启服务..."
docker compose -f docker-compose.prod.yml up -d
echo "等待服务启动..."
sleep 5
echo "部署完成!"
docker compose -f docker-compose.prod.yml ps
- name: 构建结果通知
if: always()
run: |
if [ "${{ job.status }}" = "success" ]; then
echo "构建成功: ${{ steps.tag.outputs.tag }}"
echo "镜像标签: ${{ github.sha }}"
else
echo "构建失败"
fi

60
Dockerfile Normal file
View File

@@ -0,0 +1,60 @@
# ================================
# 阶段 1: 构建阶段
# ================================
FROM --platform=linux/amd64 node:20-alpine AS builder
# 使用阿里云镜像源加速
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
# 设置工作目录
WORKDIR /build
# 设置 npm 镜像源
RUN npm config set registry https://registry.npmmirror.com
# 复制 package.json利用 Docker 缓存)
COPY package.json ./
# 使用 uvm 自动安装最新兼容的 @dcloudio 包版本
RUN npx @dcloudio/uvm@latest
# 安装所有依赖
RUN npm install --legacy-peer-deps
# 复制源代码
COPY . .
# 构建 H5 生产版本
RUN npm run build:h5
# ================================
# 阶段 2: 运行阶段
# ================================
FROM --platform=linux/amd64 nginx:alpine
# 使用阿里云镜像源加速
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
# 设置时区
RUN apk add --no-cache tzdata && \
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
echo "Asia/Shanghai" > /etc/timezone
# 删除默认 nginx 配置
RUN rm -rf /etc/nginx/conf.d/default.conf
# 复制自定义 nginx 配置
COPY docker/nginx.conf /etc/nginx/conf.d/default.conf
# 从构建阶段复制构建产物
COPY --from=builder /build/dist/build/h5 /usr/share/nginx/html
# 暴露端口
EXPOSE 80
# 健康检查
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://127.0.0.1:80/health || exit 1
# 启动 nginx
CMD ["nginx", "-g", "daemon off;"]

26
docker-compose.prod.yml Normal file
View File

@@ -0,0 +1,26 @@
version: '3.8'
services:
web:
image: registry.boss160.cn/junhong/device-voice-h5:latest
container_name: junhong-device-voice-h5
restart: unless-stopped
ports:
- '3003:80'
networks:
- device-voice-network
healthcheck:
test: ['CMD', 'wget', '--no-verbose', '--tries=1', '--spider', 'http://127.0.0.1:80/health']
interval: 30s
timeout: 3s
retries: 3
start_period: 5s
logging:
driver: 'json-file'
options:
max-size: '10m'
max-file: '3'
networks:
device-voice-network:
driver: bridge

31
docker/nginx.conf Normal file
View File

@@ -0,0 +1,31 @@
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.html;
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_proxied any;
gzip_types text/plain text/css text/xml text/javascript application/javascript application/json application/xml;
gzip_comp_level 6;
location /health {
access_log off;
return 200 'OK';
add_header Content-Type text/plain;
}
location / {
try_files $uri $uri/ /index.html;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
expires 1y;
add_header Cache-Control "public, immutable";
access_log off;
}
error_page 404 /index.html;
}

View File

@@ -1,9 +1,19 @@
{
"scripts": {
"dev:h5": "uni",
"build:h5": "uni build -p h5"
},
"devDependencies": {
"@dcloudio/types": "*",
"@dcloudio/vite-plugin-uni": "*",
"sass": "^1.63.2",
"sass-loader": "^10.4.1"
"sass-loader": "^10.4.1",
"vite": "^5.0.0"
},
"dependencies": {
"@dcloudio/uni-app": "*",
"@dcloudio/uni-components": "*",
"@dcloudio/uni-h5": "*",
"clipboard": "^2.0.11",
"dayjs": "^1.11.19",
"uview-plus": "^3.6.29"

6
vite.config.js Normal file
View File

@@ -0,0 +1,6 @@
import { defineConfig } from 'vite'
import uni from '@dcloudio/vite-plugin-uni'
export default defineConfig({
plugins: [uni()],
})