name: Build Sphinx Documentation on: pull_request: types: [opened, synchronize, reopened, edited] branches: [master] jobs: build-docs: name: Build Documentation from PR Branch runs-on: linux steps: # 第一步:获取PR分支最新代码 - name: Fetch latest PR branch code run: | echo "=== Fetching latest code for PR branch: ${{ github.head_ref }} ===" # 如果目录不存在,克隆仓库 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 # 进入目录并获取最新代码 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 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..." pip install -r requirements.txt # 记录更新时间 echo $REQ_MOD_TIME > .venv-pr/last_update.txt else echo "No changes in requirements, skipping dependency installation" fi else echo "No requirements.txt found, using existing packages" fi # 显示当前环境信息 echo "Python: $(python --version)" echo "Pip: $(pip --version)" echo "Installed packages:" pip list | grep sphinx # 第三步:构建文档(从PR分支) - name: Build documentation from PR branch run: | echo "=== Building documentation from 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 fi # 清理旧构建 rm -rf build/ # 构建文档 sphinx-build -b html source 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 }} 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"