feat: 娣诲姞 CI/CD 鑷姩鍖栭儴缃?(Dockerfile + Gitea Actions workflow)
CI/CD Deploy (Web) / 构建前端镜像并部署 (push) Failing after 2s
CI/CD Deploy (Web) / 构建前端镜像并部署 (push) Failing after 2s
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
# =============================================================
|
||||
# freedom-web CI/CD 工作流(前端)
|
||||
# 触发条件:push 到 master 分支时自动构建并部署
|
||||
#
|
||||
# 环境(同 freedom-admin,见其 workflow 注释):
|
||||
# - runner labels: ubuntu-latest
|
||||
# - job 容器自动挂载 /var/run/docker.sock,可通过 host.docker.internal 访问宿主机
|
||||
# - 部署目录:/opt/freedom/deploy
|
||||
#
|
||||
# 前置条件(管理员一次性完成):
|
||||
# - 宿主 /opt/freedom/deploy/docker-compose.yml 与 .env 已就位
|
||||
# - 宿主 /opt/freedom/deploy/.env 含 DB_URL / DB_USERNAME / DB_PASSWORD
|
||||
# (web 服务本身不需要数据库,但 compose 文件解析 backend 服务时需要)
|
||||
# =============================================================
|
||||
name: CI/CD Deploy (Web)
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
build-and-deploy:
|
||||
name: 构建前端镜像并部署
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: docker:24.0-cli
|
||||
options: -v /opt/freedom:/opt/freedom
|
||||
steps:
|
||||
- name: 安装 git
|
||||
run: apk add --no-cache git
|
||||
|
||||
- name: 拉取 master 代码
|
||||
run: |
|
||||
set -euo pipefail
|
||||
rm -rf /opt/freedom/workspace-web
|
||||
git clone --depth 1 --branch master \
|
||||
"http://oauth2:${GITHUB_TOKEN}@host.docker.internal:3000/${GITHUB_REPOSITORY}.git" \
|
||||
/opt/freedom/workspace-web
|
||||
cd /opt/freedom/workspace-web
|
||||
echo "==> 已拉取代码,最新提交:"
|
||||
git log -1 --oneline
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ github.token }}
|
||||
|
||||
- name: 构建前端镜像
|
||||
run: |
|
||||
set -euo pipefail
|
||||
docker build -t freedom-web:latest /opt/freedom/workspace-web
|
||||
|
||||
- name: 部署前端服务
|
||||
run: |
|
||||
set -euo pipefail
|
||||
cd /opt/freedom/deploy
|
||||
test -f docker-compose.yml || { echo "错误: 缺少 docker-compose.yml,请先初始化部署目录"; exit 1; }
|
||||
test -f .env || { echo "错误: 缺少 .env,请先初始化部署目录"; exit 1; }
|
||||
docker compose up -d --force-recreate web
|
||||
|
||||
- name: 确认部署结果
|
||||
run: |
|
||||
cd /opt/freedom/deploy
|
||||
docker compose ps web
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
# =============================================================
|
||||
# freedom-web 前端镜像
|
||||
# 构建阶段:node:16 构建 Vue 静态产物
|
||||
# 运行阶段:nginx 托管静态文件 + 统一入口反代
|
||||
#
|
||||
# 注意:Vue 2.5 + vue-cli 3 为较老项目,固定使用 node:16 保证构建兼容
|
||||
# =============================================================
|
||||
FROM node:16-alpine AS builder
|
||||
WORKDIR /build
|
||||
|
||||
# 复制依赖清单,利用 Docker 分层缓存(先安装依赖再复制源码)
|
||||
COPY package.json .
|
||||
RUN npm config set registry https://registry.npm.taobao.org/ && \
|
||||
npm install --no-audit --no-fund || npm install --no-audit --no-fund --legacy-peer-deps
|
||||
|
||||
# 复制源码
|
||||
COPY . .
|
||||
|
||||
# 构建生产产物(产物输出到 dist/)
|
||||
RUN npm run build
|
||||
|
||||
# ---------- 运行阶段 ----------
|
||||
FROM nginx:1.24-alpine
|
||||
WORKDIR /usr/share/nginx/html
|
||||
|
||||
# 清理默认站点,复制构建产物
|
||||
RUN rm -rf /usr/share/nginx/html/*
|
||||
COPY --from=builder /build/dist/ /usr/share/nginx/html/
|
||||
|
||||
# 统一入口 Nginx 配置(/api 反代到后端 + SPA 路由 + Excel 静态模板)
|
||||
COPY nginx.conf /etc/nginx/conf.d/default.conf
|
||||
|
||||
# 对外暴露端口
|
||||
EXPOSE 80
|
||||
|
||||
CMD ["nginx", "-g", "daemon off;"]
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
# =============================================================
|
||||
# freedom 统一入口 Nginx 配置
|
||||
# 同一域名路径区分:/api/* 反代到后端容器,其余走前端静态文件
|
||||
#
|
||||
# 后端接口无 /api 前缀,因此需要去掉前缀再转发。
|
||||
# 后端容器服务名:backend,端口:8800(见 docker-compose.yml)
|
||||
# =============================================================
|
||||
server {
|
||||
listen 80;
|
||||
server_name _;
|
||||
|
||||
root /usr/share/nginx/html;
|
||||
index index.html;
|
||||
|
||||
# 请求体大小限制(如有文件上传需求可调大)
|
||||
client_max_body_size 20m;
|
||||
|
||||
# ---------------- API 反向代理 ----------------
|
||||
# /api/xxx -> backend:8800/xxx (去掉 /api 前缀)
|
||||
location /api/ {
|
||||
proxy_pass http://backend:8800/;
|
||||
proxy_http_version 1.1;
|
||||
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_set_header Authorization $http_authorization;
|
||||
proxy_connect_timeout 10s;
|
||||
proxy_read_timeout 60s;
|
||||
}
|
||||
|
||||
# ---------------- Excel 模板静态文件 ----------------
|
||||
# 前端 nginxPath 指向 /excelTemplate.xxx,放在 Nginx 根目录静态文件
|
||||
location = /excelTemplate {
|
||||
alias /usr/share/nginx/html/;
|
||||
}
|
||||
location ~ ^/excelTemplate\.[a-zA-Z0-9]+$ {
|
||||
try_files $uri =404;
|
||||
}
|
||||
|
||||
# ---------------- 前端静态资源 + SPA 路由 ----------------
|
||||
location / {
|
||||
try_files $uri $uri/ /index.html;
|
||||
}
|
||||
|
||||
# 静态资源缓存
|
||||
location ~* \.(js|css|png|jpg|jpeg|gif|svg|ico|woff2?|ttf|eot)$ {
|
||||
expires 7d;
|
||||
add_header Cache-Control "public, max-age=604800";
|
||||
try_files $uri =404;
|
||||
}
|
||||
|
||||
# 常见接口错误页面
|
||||
error_page 500 502 503 504 /50x.html;
|
||||
location = /50x.html {
|
||||
root /usr/share/nginx/html;
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -18,7 +18,7 @@ export default {
|
||||
*/
|
||||
baseUrl: {
|
||||
dev: 'http://localhost:8800', // 开发环境
|
||||
pro: 'http://106.14.163.31:8800' // 生产环境
|
||||
pro: '/api' // 生产环境:同域统一入口,由 Nginx 将 /api 反代到后端
|
||||
},
|
||||
/**
|
||||
* @description 默认打开的首页的路由name值,默认为home
|
||||
|
||||
Reference in New Issue
Block a user