This commit is contained in:
sophie committed 2026-05-21 11:38:54 +08:00
1 parent a6f35b375c
commit b1613824ce
8 files changed
+200 -12

No files matched your search

+45
View File
@@ -0,0 +1,45 @@
# shellcheck shell=bash
# 供 pre-commit / pre-push 引用;勿直接执行。
hook_should_skip() {
[[ "${SKIP_GIT_HOOKS:-}" == "1" ]] || [[ "${SKIP_FRONTEND_HOOKS:-}" == "1" ]]
}
frontend_paths_in() {
local file_list="${1:-}"
echo "${file_list}" | grep -qE '^frontend/'
}
ensure_frontend_deps() {
local frontend_dir="${1}/frontend"
if [[ ! -d "${frontend_dir}/node_modules" ]]; then
echo "[git-hook] frontend/node_modules 不存在,请先执行:"
echo " cd frontend && npm install"
return 1
fi
return 0
}
run_frontend_typecheck() {
local root_dir="${1}"
echo "[pre-commit] 检测到 frontend/ 变更,运行 vue-tsc 类型检查..."
if ! ensure_frontend_deps "${root_dir}"; then
return 1
fi
(
cd "${root_dir}/frontend"
npx vue-tsc -b --pretty false
)
}
run_frontend_build() {
local root_dir="${1}"
echo "[pre-push] 检测到将要推送的提交包含 frontend/ 变更,运行 npm run build..."
if ! ensure_frontend_deps "${root_dir}"; then
return 1
fi
(
cd "${root_dir}/frontend"
npm run build
)
}
+16
View File
@@ -3,6 +3,8 @@ set -euo pipefail
ROOT_DIR="$(git rev-parse --show-toplevel)"
cd "${ROOT_DIR}"
# shellcheck source=_common.sh
source "${ROOT_DIR}/.githooks/_common.sh"
STAGED_FILES="$(git diff --cached --name-only --diff-filter=ACMR || true)"
if [[ -z "${STAGED_FILES}" ]]; then
@@ -30,4 +32,18 @@ if [[ "${MODELS_CHANGED}" -eq 1 && "${MIGRATION_CHANGED}" -eq 0 ]]; then
exit 1
fi
if hook_should_skip; then
exit 0
fi
if frontend_paths_in "${STAGED_FILES}"; then
if ! run_frontend_typecheck "${ROOT_DIR}"; then
echo "[pre-commit] vue-tsc 失败,已阻止提交。"
echo "[pre-commit] 跳过前端检查: SKIP_FRONTEND_HOOKS=1 git commit"
echo "[pre-commit] 或跳过全部 hook: git commit --no-verify"
exit 1
fi
echo "[pre-commit] frontend 类型检查通过。"
fi
exit 0
+54
View File
@@ -0,0 +1,54 @@
#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(git rev-parse --show-toplevel)"
# shellcheck source=_common.sh
source "${ROOT_DIR}/.githooks/_common.sh"
if hook_should_skip; then
exit 0
fi
zero="0000000000000000000000000000000000000000"
need_frontend_build=0
while read -r local_ref local_sha remote_ref remote_sha; do
[[ "${local_sha}" == "${zero}" ]] && continue
changed_files=""
if [[ "${remote_sha}" == "${zero}" ]]; then
upstream_ref="$(git rev-parse --abbrev-ref --symbolic-full-name '@{u}' 2>/dev/null || true)"
if [[ -n "${upstream_ref}" ]]; then
remote_branch="${upstream_ref#*/}"
remote_name="${upstream_ref%%/*}"
merge_base="$(git merge-base "${local_sha}" "${remote_name}/${remote_branch}" 2>/dev/null || true)"
if [[ -n "${merge_base}" ]]; then
changed_files="$(git diff --name-only --diff-filter=ACMR "${merge_base}" "${local_sha}" || true)"
fi
fi
if [[ -z "${changed_files}" ]]; then
changed_files="$(git diff-tree --no-commit-id --name-only --diff-filter=ACMR -r "${local_sha}" || true)"
fi
else
changed_files="$(git diff --name-only --diff-filter=ACMR "${remote_sha}" "${local_sha}" || true)"
fi
if frontend_paths_in "${changed_files}"; then
need_frontend_build=1
break
fi
done
if [[ "${need_frontend_build}" -eq 0 ]]; then
exit 0
fi
if ! run_frontend_build "${ROOT_DIR}"; then
echo "[pre-push] 前端 build 失败,已阻止 push。"
echo "[pre-push] 跳过检查可设置: SKIP_FRONTEND_HOOKS=1 git push"
echo "[pre-push] 或跳过全部 hook: git push --no-verify"
exit 1
fi
echo "[pre-push] 前端 build 通过。"
exit 0
Vendored
+16
View File
@@ -329,6 +329,22 @@ pipeline {
}
}
stage('Frontend Build (CI)') {
when {
expression { env.FRONTEND_CHANGED == 'true' }
}
steps {
echo '[Frontend Build] FRONTEND_CHANGED=true,容器内执行 npm ci && npm run build(与部署构建一致)'
sh '''
set -e
docker run --rm \
-v "${WORKSPACE}:/repo" \
-w /repo \
node:22-alpine sh -c "chmod +x scripts/ci_frontend_build.sh && ./scripts/ci_frontend_build.sh"
'''
}
}
stage('Deploy Dev') {
when {
expression {
+31 -2
View File
@@ -48,6 +48,7 @@
4. 提交信息建议规范化:`feat:`、`fix:`、`refactor:`、`docs:`、`chore:`
5. 合并前必须完成最少检查:
- 能启动前后端
- 改动了 `frontend/` 时本地 `cd frontend && npm run build` 通过(或已安装 Git Hooks,由 pre-push 自动检查)
- 关键页面可访问
- 登录流程可用
6. 任何涉及数据库结构变更的提交,必须包含迁移脚本或升级说明
@@ -62,7 +63,35 @@ git clone https://gitee.com/justop/hxops.git
cd hxops
```
### 4.2 常用 Git 操作示例
### 4.2 安装 Git Hooks(推荐,每人 clone 后执行一次)
仓库根目录的 `.githooks/` 会随代码拉取;**不会自动生效**,需在本机启用:
| Hook | 时机 | 检查内容 |
|------|------|----------|
| `pre-commit` | `git commit` | 改 `models.py` 须同时提交 migration;暂存 `frontend/` 时跑 `vue-tsc` |
| `pre-push` | `git push` | 本次推送含 `frontend/` 变更时跑 `npm run build`(与 Jenkins 部署一致) |
**Git Bash / WSL:**
```bash
chmod +x scripts/install_git_hooks.sh .githooks/pre-commit .githooks/pre-push
./scripts/install_git_hooks.sh
```
**PowerShell(仅启用钩子目录):**
```powershell
git config core.hooksPath .githooks
```
验证:`git config --get core.hooksPath` 应输出 `.githooks`。
前置:已执行 `cd frontend && npm install`。应急跳过前端检查:`$env:SKIP_FRONTEND_HOOKS="1"`(bash 为 `SKIP_FRONTEND_HOOKS=1`)。
Jenkins 在检测到 `frontend/` 变更时会再跑一遍同款构建(`scripts/ci_frontend_build.sh`),作为 Hook 被跳过时的兜底。
### 4.3 常用 Git 操作示例
```bash
# 拉取远程分支
@@ -81,7 +110,7 @@ git commit -m "feat: your change summary"
git push origin feature/your-feature-name
```
### 4.3 关键目录
### 4.4 关键目录
- `frontend/`:Vue 3 + TypeScript + Vite
- `backend/`:Flask + SQLAlchemy + Flask-Migrate
+3 -3
View File
@@ -666,7 +666,7 @@ run_ci_migration_check() {
}
install_precommit_hook() {
echo "=== 安装 Git pre-commit Hook ==="
echo "=== 安装 Git Hooks(pre-commit + pre-push)==="
local install_script="${ROOT_DIR}/scripts/install_git_hooks.sh"
if [[ ! -x "${install_script}" ]]; then
echo "未找到可执行脚本: ${install_script}"
@@ -695,7 +695,7 @@ show_help() {
branches或 34 -> 查看本地所有分支及默认分支、远端所有分支及默认分支
dbdrift或 41 -> 检查当前数据库结构与 SQLAlchemy 模型是否一致
migci 或 42 -> 运行 CI 同款迁移一致性检查(空库升级+漂移检测)
hook 或 43 -> 安装 pre-commit Hook(改 models 必须带 migration)
hook 或 43 -> 安装 Git Hooks(models+migration / frontend vue-tsc / push 前 build)
help 或 9 -> 再次显示本帮助
help -> 再次显示本帮助
exit 或 0 -> 退出脚本
@@ -759,7 +759,7 @@ main_loop() {
confirm_and_run "CI同款迁移一致性检查" run_ci_migration_check
;;
43|hook)
confirm_and_run "安装 pre-commit Hook" install_precommit_hook
confirm_and_run "安装 Git Hooks" install_precommit_hook
;;
9|help)
show_help
+19
View File
@@ -0,0 +1,19 @@
#!/usr/bin/env bash
# Jenkins / 本地 CI 同款前端生产构建(与 deploy.sh、frontend/Dockerfile 一致)
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
FRONTEND_DIR="${ROOT_DIR}/frontend"
if [[ ! -f "${FRONTEND_DIR}/package.json" ]]; then
echo "[ci-frontend-build] missing ${FRONTEND_DIR}/package.json"
exit 1
fi
cd "${FRONTEND_DIR}"
echo "[ci-frontend-build] npm ci"
npm ci
echo "[ci-frontend-build] npm run build"
npm run build
echo "[ci-frontend-build] OK"
+16 -7
View File
@@ -9,13 +9,18 @@ if ! git -C "${ROOT_DIR}" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
exit 1
fi
HOOK_PATH="${ROOT_DIR}/.githooks/pre-commit"
if [[ ! -f "${HOOK_PATH}" ]]; then
echo "[hook-install] missing hook file: ${HOOK_PATH}"
exit 1
fi
HOOKS_DIR="${ROOT_DIR}/.githooks"
REQUIRED_HOOKS=(pre-commit pre-push _common.sh)
for hook_name in "${REQUIRED_HOOKS[@]}"; do
hook_path="${HOOKS_DIR}/${hook_name}"
if [[ ! -f "${hook_path}" ]]; then
echo "[hook-install] missing hook file: ${hook_path}"
exit 1
fi
chmod +x "${hook_path}"
done
chmod +x "${HOOK_PATH}"
if ! git -C "${ROOT_DIR}" config core.hooksPath .githooks; then
echo "[hook-install] failed to set git config core.hooksPath."
echo "[hook-install] please run manually in your local shell:"
@@ -23,5 +28,9 @@ if ! git -C "${ROOT_DIR}" config core.hooksPath .githooks; then
exit 1
fi
echo "[hook-install] Installed pre-commit hook successfully."
echo "[hook-install] Installed Git hooks successfully."
echo "[hook-install] pre-commit: models 变更需带 migration;frontend 暂存时跑 vue-tsc"
echo "[hook-install] pre-push: 推送含 frontend 变更时跑 npm run build"
echo "[hook-install] core.hooksPath=$(git -C "${ROOT_DIR}" config --get core.hooksPath)"
echo "[hook-install] 跳过前端检查: SKIP_FRONTEND_HOOKS=1"
echo "[hook-install] 跳过全部 hook: SKIP_GIT_HOOKS=1 或 git commit/push --no-verify"