Revision Actions
Build Sphinx Documentation / Build Documentation from PR Branch (pull_request) Successful in 4m52s

This commit is contained in:
2025-08-25 13:18:30 +08:00
parent a02c6bc945
commit dad0a77093
2 changed files with 116 additions and 152 deletions
+38 -74
View File
@@ -7,122 +7,86 @@ on:
jobs:
deploy:
# 只在PR被成功merge到master时运行
if: github.event.pull_request.merged == true && github.base_ref == 'master'
runs-on: linux
steps:
# 第一步:获取master分支最新代码
- name: Fetch latest master branch code
# 第一步:更新主工作树
- name: Update main worktree
run: |
echo "=== Fetching latest master branch code ==="
# 确保目录存在
if [ ! -d "/home/ly0kos/work/test_env" ]; then
echo "Cloning repository..."
git clone http://localhost:3000/yuysh/Manlink_Doc.git /home/ly0kos/work/test_env
fi
echo "=== Updating main worktree ==="
cd /home/ly0kos/work/test_env
# 清理任何未提交的更改
git reset --hard
git clean -fd
# 获取所有远程更新
git fetch --all --prune
# 切换到master分支并获取最新代码
git checkout master
git pull origin master --force
git pull origin master
echo "✓ Latest master code fetched"
echo "Current commit: $(git log --oneline -1)"
echo "✓ Main worktree updated"
# 第二步:复用生产虚拟环境
- name: Reuse or update production Python environment
# 第二步:复用共享虚拟环境
- name: Reuse shared virtual environment
run: |
echo "Setting up production Python environment"
echo "=== Using shared virtual environment ==="
cd /home/ly0kos/work/test_env
source .venv-shared/bin/activate
# 检查虚拟环境是否存在
if [ ! -d ".venv-prod" ]; then
echo "Creating new production virtual environment..."
python -m venv .venv-prod
source .venv-prod/bin/activate
pip install --upgrade pip
# 安装基础依赖
pip install sphinx sphinx-rtd-theme
else
echo "Reusing existing production virtual environment..."
source .venv-prod/bin/activate
fi
# 检查并更新依赖
# 检查主分支的requirements.txt是否有更新
if [ -f "requirements.txt" ]; then
echo "Checking for dependency updates in production..."
REQ_MOD_TIME=$(stat -c %Y requirements.txt 2>/dev/null || echo 0)
VENV_MOD_TIME=$(cat .venv-prod/last_update.txt 2>/dev/null || echo 0)
VENV_MOD_TIME=$(cat .venv-shared/last_update.txt 2>/dev/null || echo 0)
if [ $REQ_MOD_TIME -gt $VENV_MOD_TIME ]; then
echo "Production requirements updated, installing new dependencies..."
echo "Master branch requirements updated, installing new dependencies..."
pip install -r requirements.txt
echo $REQ_MOD_TIME > .venv-prod/last_update.txt
else
echo "No changes in production requirements, skipping installation"
echo $REQ_MOD_TIME > .venv-shared/last_update.txt
fi
fi
# 显示环境信息
echo "Production environment ready:"
pip list | grep sphinx
# 第三步:从master分支构建文档
# 第三步:从主工作树构建文档
- name: Build documentation from master
run: |
echo "=== Building production documentation from master ==="
echo "=== Building production documentation ==="
cd /home/ly0kos/work/test_env
source .venv-prod/bin/activate
source .venv-shared/bin/activate
# 清理旧构建
rm -rf build/
# 构建文档
sphinx-build -b html source build -v -j 4
echo "✓ Production build completed"
# 第四步:部署到Web服务器
- name: Deploy to web server
run: |
echo "=== Deploying to production web server ==="
echo "=== Deploying to production ==="
cd /home/ly0kos/work/test_env
echo "[INFO] Removing old files..."
sudo rm -rf /var/www/docs_dev/*
echo "[INFO] Copying new files..."
sudo cp -r build/* /var/www/docs_dev/
rm -rf /var/www/docs_dev/*
cp -r build/* /var/www/docs_dev/
chown -R www-data:www-data /var/www/docs_dev/
chmod -R 755 /var/www/docs_dev/
# 设置正确的文件权限
sudo chown -R www-data:www-data /var/www/docs_dev/
sudo chmod -R 755 /var/www/docs_dev/
echo "✓ Documentation deployed"
echo "✓ Documentation deployed to /var/www/docs_dev/"
# 第五步:清理PR预览文件
- name: Cleanup PR previews
# 第五步:清理PR工作树
- name: Cleanup PR worktree
run: |
echo "Cleaning up PR preview files for branch: ${{ github.head_ref }}"
echo "=== Cleaning up PR worktree ==="
cd /home/ly0kos/work/test_env
# 清理预览文件(保留虚拟环境)
preview_dir="/home/ly0kos/work/test_env/pr-builds/${{ github.head_ref }}"
if [ -d "$preview_dir" ]; then
rm -rf "$preview_dir"
echo "Removed preview for PR branch: ${{ github.head_ref }}"
WORKTREE_DIR="/home/ly0kos/work/test_env/worktrees/${{ github.head_ref }}"
if [ -d "$WORKTREE_DIR" ]; then
git worktree remove "$WORKTREE_DIR" --force
echo "✓ Removed worktree for ${{ github.head_ref }}"
fi
# 清理预览文件
PREVIEW_DIR="/home/ly0kos/work/test_env/pr-builds/${{ github.head_ref }}"
if [ -d "$PREVIEW_DIR" ]; then
rm -rf "$PREVIEW_DIR"
echo "✓ Removed preview for ${{ github.head_ref }}"
fi
# 第六步:验证部署
- name: Verify deployment
run: |
echo "Production deployment completed successfully!"
echo "Web server contents:"
echo "Production deployment completed!"
ls -la /var/www/docs_dev/ | head -10
+74 -74
View File
@@ -11,108 +11,108 @@ jobs:
runs-on: linux
steps:
# 第一步:获取PR分支最新代码
- name: Fetch latest PR branch code
# 第一步:设置主工作树和虚拟环境
- name: Setup main worktree and virtual environment
run: |
echo "=== Fetching latest code for PR branch: ${{ github.head_ref }} ==="
echo "=== Setting up main worktree ==="
# 如果目录不存在,克隆仓库
# 如果目录不存在,克隆仓库
if [ ! -d "/home/ly0kos/work/test_env" ]; then
echo "Cloning repository..."
echo "Cloning repository to main worktree..."
git clone http://localhost:3000/yuysh/Manlink_Doc.git /home/ly0kos/work/test_env
fi
cd /home/ly0kos/work/test_env
# 进入目录并获取最新代码
cd /home/ly0kos/work/test_env
# 清理任何未提交的更改
git reset --hard
git clean -fd
# 获取所有远程分支和更新
git fetch --all --prune
# 切换到PR分支并获取最新代码
git checkout ${{ github.head_ref }}
git pull origin ${{ github.head_ref }} --force
echo "✓ Latest code fetched for branch: ${{ github.head_ref }}"
echo "Current commit: $(git log --oneline -1)"
# 第二步:复用或创建Python虚拟环境
- name: Reuse or update Python virtual environment
run: |
echo "Setting up Python environment for PR branch: ${{ github.head_ref }}"
cd /home/ly0kos/work/test_env
# 检查虚拟环境是否存在
if [ ! -d ".venv-pr" ]; then
echo "Creating new virtual environment..."
python -m venv .venv-pr
source .venv-pr/bin/activate
# 创建共享虚拟环境
python -m venv .venv-shared
source .venv-shared/bin/activate
pip install --upgrade pip
# 安装基础依赖
pip install sphinx sphinx-rtd-theme
else
echo "Reusing existing virtual environment..."
source .venv-pr/bin/activate
fi
# 检查requirements.txt是否更新
if [ -f "requirements.txt" ]; then
echo "Checking for dependency updates..."
# 获取requirements.txt的修改时间
REQ_MOD_TIME=$(stat -c %Y requirements.txt 2>/dev/null || echo 0)
# 获取虚拟环境中记录的最后更新时间
VENV_MOD_TIME=$(cat .venv-pr/last_update.txt 2>/dev/null || echo 0)
if [ $REQ_MOD_TIME -gt $VENV_MOD_TIME ]; then
echo "Requirements updated, installing new dependencies..."
# 记录初始依赖状态
if [ -f "requirements.txt" ]; then
pip install -r requirements.txt
# 记录更新时间
echo $REQ_MOD_TIME > .venv-pr/last_update.txt
else
echo "No changes in requirements, skipping dependency installation"
stat -c %Y requirements.txt > .venv-shared/last_update.txt
fi
else
echo "No requirements.txt found, using existing packages"
cd /home/ly0kos/work/test_env
git fetch --all --prune
git checkout master
git pull origin master
fi
# 显示当前环境信息
echo "Python: $(python --version)"
echo "Pip: $(pip --version)"
echo "Installed packages:"
pip list | grep sphinx
echo "✓ Main worktree ready"
# 第步:构建文档(从PR分支)
- name: Build documentation from PR branch
# 第步:为PR分支创建工作树
- name: Create worktree for PR branch
run: |
echo "=== Building documentation from PR branch: ${{ github.head_ref }} ==="
echo "=== Creating worktree for PR branch: ${{ github.head_ref }} ==="
cd /home/ly0kos/work/test_env
source .venv-pr/bin/activate
# 确保源目录存在
if [ ! -d "source" ]; then
echo "Error: source directory not found!"
exit 1
# 清理可能存在的旧工作树
WORKTREE_DIR="/home/ly0kos/work/test_env/worktrees/${{ github.head_ref }}"
if [ -d "$WORKTREE_DIR" ]; then
echo "Removing existing worktree..."
git worktree remove "$WORKTREE_DIR" --force 2>/dev/null || true
rm -rf "$WORKTREE_DIR" 2>/dev/null || true
fi
# 清理旧构建
rm -rf build/
# 创建新的工作树
mkdir -p "/home/ly0kos/work/test_env/worktrees"
git fetch origin ${{ github.head_ref }}:${{ github.head_ref }} --force
git worktree add "/home/ly0kos/work/test_env/worktrees/${{ github.head_ref }}" ${{ github.head_ref }} --force
echo "✓ Worktree created at: /home/ly0kos/work/test_env/worktrees/${{ github.head_ref }}"
# 第三步:复用共享虚拟环境并检查依赖更新
- name: Reuse shared virtual environment
run: |
echo "=== Reusing shared virtual environment ==="
cd /home/ly0kos/work/test_env
WORKTREE_DIR="/home/ly0kos/work/test_env/worktrees/${{ github.head_ref }}"
# 使用主工作树的共享虚拟环境
source .venv-shared/bin/activate
# 检查PR分支的requirements.txt是否有更新
if [ -f "$WORKTREE_DIR/requirements.txt" ]; then
echo "Checking for dependency updates in PR branch..."
REQ_MOD_TIME=$(stat -c %Y "$WORKTREE_DIR/requirements.txt" 2>/dev/null || echo 0)
VENV_MOD_TIME=$(cat .venv-shared/last_update.txt 2>/dev/null || echo 0)
if [ $REQ_MOD_TIME -gt $VENV_MOD_TIME ]; then
echo "PR branch requirements updated, installing new dependencies..."
pip install -r "$WORKTREE_DIR/requirements.txt"
echo $REQ_MOD_TIME > .venv-shared/last_update.txt
echo "Dependencies updated from PR branch"
else
echo "No changes in requirements, using existing packages"
fi
fi
echo "Python: $(python --version)"
echo "Sphinx: $(sphinx-build --version 2>/dev/null || echo 'Not installed')"
# 第四步:在工作树中构建文档
- name: Build documentation in worktree
run: |
echo "=== Building documentation in worktree ==="
cd /home/ly0kos/work/test_env
WORKTREE_DIR="/home/ly0kos/work/test_env/worktrees/${{ github.head_ref }}"
source .venv-shared/bin/activate
# 构建文档
sphinx-build -b html source build -v -j 4
cd "$WORKTREE_DIR"
sphinx-build -b html source "$WORKTREE_DIR/build" -v -j 4
# 将构建结果复制到预览位置
# 复制到预览位置
mkdir -p /home/ly0kos/work/test_env/pr-builds/
rm -rf /home/ly0kos/work/test_env/pr-builds/${{ github.head_ref }}
cp -r build /home/ly0kos/work/test_env/pr-builds/${{ github.head_ref }}
cp -r "$WORKTREE_DIR/build" /home/ly0kos/work/test_env/pr-builds/${{ github.head_ref }}
echo "✓ Build completed for PR branch: ${{ github.head_ref }}"
# 第步:验证构建结果
# 第步:验证构建结果
- name: Verify build
run: |
echo "Build output for PR ${{ github.head_ref }}:"
ls -la /home/ly0kos/work/test_env/pr-builds/${{ github.head_ref }}/ | head -10
echo "Preview available at: /home/ly0kos/work/test_env/pr-builds/${{ github.head_ref }}/index.html"