name: Deploy Sphinx Documentation on: pull_request: types: [closed] branches: [master] 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 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 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 echo "✓ Latest master code fetched" echo "Current commit: $(git log --oneline -1)" # 第二步:复用生产虚拟环境 - name: Reuse or update production Python environment run: | echo "Setting up production Python environment" cd /home/ly0kos/work/test_env # 检查虚拟环境是否存在 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 # 检查并更新依赖 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) if [ $REQ_MOD_TIME -gt $VENV_MOD_TIME ]; then echo "Production 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" fi fi # 显示环境信息 echo "Production environment ready:" pip list | grep sphinx # 第三步:从master分支构建文档 - name: Build documentation from master run: | echo "=== Building production documentation from master ===" cd /home/ly0kos/work/test_env source .venv-prod/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 ===" 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/ # 设置正确的文件权限 sudo chown -R www-data:www-data /var/www/docs_dev/ sudo chmod -R 755 /var/www/docs_dev/ echo "✓ Documentation deployed to /var/www/docs_dev/" # 第五步:清理PR预览文件 - name: Cleanup PR previews run: | echo "Cleaning up PR preview files for branch: ${{ github.head_ref }}" # 清理预览文件(保留虚拟环境) 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 }}" fi # 第六步:验证部署 - name: Verify deployment run: | echo "Production deployment completed successfully!" echo "Web server contents:" ls -la /var/www/docs_dev/ | head -10