#For test name: Build and Deploy Sphinx Docs on: pull_request: types: [opened, synchronize, reopened] branches: [master] jobs: build-docs: name: Build Documentation if: github.event.pull_request.merged == true runs-on: linux steps: # 第一步:设置主工作目录和稳定的虚拟环境 - name: Set up main worktree with stable environment run: | echo "=== Setting up main worktree ===" # 创建主工作目录并克隆仓库 if [ ! -d /home/ly0kos/work/test_env ]; then echo "Cloning repository to main worktree..." git clone http://localhost:3000/yuysh/Manlink_Doc.git /home/ly0kos/work/test_env cd /home/ly0kos/work/test_env echo "Creating virtual environment..." python -m venv .venv source .venv/bin/activate echo "Installing dependencies..." pip install --upgrade pip pip install -r requirements.txt echo "✓ Main worktree and virtual environment created" else echo "Updating existing main worktree..." cd /home/ly0kos/work/test_env git pull origin main source .venv/bin/activate echo "Updating dependencies if needed..." pip install -r requirements.txt echo "✓ Main worktree updated" fi # 验证环境 echo "Environment verification:" source .venv/bin/activate echo "Python: $(python --version)" echo "Sphinx-build: $(sphinx-build --version 2>/dev/null || echo 'Not found')" # 第二步:创建临时构建工作树 - name: Create temporary build worktree run: | echo "=== Creating build worktree ===" cd /home/ly0kos/work/test_env # 清理可能存在的旧构建目录 if [ -d /home/ly0kos/work/test_env/build ]; then echo "Removing old build worktree..." git worktree remove /home/ly0kos/work/test_env/build --force 2>/dev/null || true rm -rf /home/ly0kos/work/test_env/build 2>/dev/null || true fi # 创建新的构建工作树 echo "Creating new build worktree..." git worktree add /home/ly0kos/work/test_env/build echo "✓ Build worktree created at /home/ly0kos/work/test_env/build" # 第三步:使用主目录的虚拟环境构建文档 - name: Build documentation run: | echo "=== Building documentation ===" cd /home/ly0kos/work/test_env source .venv/bin/activate echo "Building from: /home/ly0kos/work/test_env/source" echo "Output to: /home/ly0kos/work/test_env/build/build" # 使用主目录的虚拟环境来构建构建目录中的代码 sphinx-build -b html /home/ly0kos/work/test_env/source /home/ly0kos/work/test_env/build/ -v -j 4 echo "✓ Build completed successfully" # 验证构建结果 echo "Build output contents:" ls -la /home/ly0kos/work/test_env/build/ name: Deploy on Merge on: pull_request: types: [closed] branches: [main] jobs: deploy: # 关键:只在PR被成功merge时运行 if: github.event.pull_request.merged == true runs-on: linux steps: # 第一步:部署到Web服务器 - name: Deploy to web server run: | echo "=== Deploying to web server ===" echo "[INFO] Removing Old file..." rm -rf /var/www/docs_dev/* echo "[INFO] Copying new file..." cp -r /home/ly0kos/work/test_env/build/* /var/www/docs_dev/ echo "✓ Documentation deployed to /var/www/docs_dev/" # 验证部署 echo "Web server contents:" ls -la /var/www/docs_dev/ | head -10 # 第二步:清理临时工作树 - name: Cleanup temporary worktree run: | echo "=== Cleaning up ===" cd /home/ly0kos/work/test_env # 移除工作树 if [ -d /home/ly0kos/work/test_env/build ]; then git worktree remove /home/ly0kos/work/test_env/build --force echo "✓ Temporary worktree removed" else echo "Build worktree already removed" fi echo "=== Process completed successfully ==="