简化 CI/CD 部署逻辑

- knowledge-base 改为独立部署
- kb-web 只负责代码,不再恢复用户目录
- 仅备份恢复 config.php
This commit is contained in:
2026-02-15 16:29:09 +08:00
parent b4081f82cc
commit cb59dba8e6
+12 -56
View File
@@ -35,37 +35,22 @@ jobs:
mkdir -p ${{ env.TEST_DIR }}
cd ${{ env.TEST_DIR }}
# 备份需要保留的文件
BACKUP_DIR="/tmp/kb-web-backup-$$"
mkdir -p $BACKUP_DIR
[ -f config.php ] && cp config.php $BACKUP_DIR/
for dir in */; do
if [ -d "$dir" ] && [ "$dir" != "demo/" ]; then
cp -r "$dir" $BACKUP_DIR/ 2>/dev/null || true
fi
done
# 备份配置文件
[ -f config.php ] && cp config.php /tmp/config.php.bak
# 部署代码
if [ -d .git ]; then
git fetch origin
git checkout ${{ github.ref_name }} 2>/dev/null || git checkout -b ${{ github.ref_name }} origin/${{ github.ref_name }}
git reset --hard origin/${{ github.ref_name }}
# 清理未跟踪的文件(保留 .git 和备份目录)
git clean -fd
else
git clone -b ${{ github.ref_name }} ssh://gitea-external/starmen1265/kb-web.git .
fi
# 恢复配置和用户数据
[ -f $BACKUP_DIR/config.php ] && cp $BACKUP_DIR/config.php . && chown www:www config.php
for dir in $BACKUP_DIR/*/; do
dirname=$(basename "$dir")
if [ "$dirname" != "demo" ] && [ "$dirname" != "deploy" ] && [ "$dirname" != "tests" ]; then
cp -r "$dir" . 2>/dev/null || true
fi
done
# 恢复配置文件
[ -f /tmp/config.php.bak ] && cp /tmp/config.php.bak config.php && chown www:www config.php
rm -rf $BACKUP_DIR
chown -R www:www ${{ env.TEST_DIR }} 2>/dev/null || true
echo "✅ 测试环境部署完成"
@@ -171,16 +156,8 @@ jobs:
tar -czf $BACKUP_DIR/backup_$TIMESTAMP.tar.gz --exclude='.git' . 2>/dev/null || true
cd $BACKUP_DIR && ls -t | tail -n +6 | xargs rm -f 2>/dev/null || true
# 备份配置和用户数据
TEMP_DIR="/tmp/kb-web-prod-$$"
mkdir -p $TEMP_DIR
cd ${{ env.PROD_DIR }}
[ -f config.php ] && cp config.php $TEMP_DIR/
for dir in */; do
if [ -d "$dir" ] && [ "$dir" != "demo/" ]; then
cp -r "$dir" $TEMP_DIR/ 2>/dev/null || true
fi
done
# 备份配置
[ -f config.php ] && cp config.php /tmp/config.php.bak
# 部署
cd ${{ env.PROD_DIR }}
@@ -188,16 +165,9 @@ jobs:
git reset --hard origin/main
git clean -fd
# 恢复配置和用户数据
[ -f $TEMP_DIR/config.php ] && cp $TEMP_DIR/config.php . && chown www:www config.php
for dir in $TEMP_DIR/*/; do
dirname=$(basename "$dir")
if [ "$dirname" != "demo" ] && [ "$dirname" != "deploy" ] && [ "$dirname" != "tests" ]; then
cp -r "$dir" . 2>/dev/null || true
fi
done
# 恢复配置
[ -f /tmp/config.php.bak ] && cp /tmp/config.php.bak config.php && chown www:www config.php
rm -rf $TEMP_DIR
chown -R www:www ${{ env.PROD_DIR }} 2>/dev/null || true
echo "✅ 正式环境部署完成"
@@ -215,30 +185,16 @@ jobs:
script: |
cd ${{ env.PROD_DIR }}
# 备份配置和用户数据
TEMP_DIR="/tmp/kb-web-prod-$$"
mkdir -p $TEMP_DIR
[ -f config.php ] && cp config.php $TEMP_DIR/
for dir in */; do
if [ -d "$dir" ] && [ "$dir" != "demo/" ]; then
cp -r "$dir" $TEMP_DIR/ 2>/dev/null || true
fi
done
# 备份配置
[ -f config.php ] && cp config.php /tmp/config.php.bak
# 部署
git fetch origin
git reset --hard origin/main
git clean -fd
# 恢复配置和用户数据
[ -f $TEMP_DIR/config.php ] && cp $TEMP_DIR/config.php . && chown www:www config.php
for dir in $TEMP_DIR/*/; do
dirname=$(basename "$dir")
if [ "$dirname" != "demo" ] && [ "$dirname" != "deploy" ] && [ "$dirname" != "tests" ]; then
cp -r "$dir" . 2>/dev/null || true
fi
done
# 恢复配置
[ -f /tmp/config.php.bak ] && cp /tmp/config.php.bak config.php && chown www:www config.php
rm -rf $TEMP_DIR
chown -R www:www ${{ env.PROD_DIR }} 2>/dev/null || true
echo "✅ 正式环境已更新"