Action revision
Build Sphinx Documentation / Build Documentation from PR Branch (pull_request) Successful in 4m52s
Deploy Sphinx Documentation / deploy (pull_request) Waiting to run

This commit is contained in:
2025-08-25 13:02:09 +08:00
parent 6f7975959f
commit 8debfcd8b3
2 changed files with 205 additions and 56 deletions
+116 -30
View File
@@ -1,42 +1,128 @@
name: Deploy on Merge name: Deploy Sphinx Documentation
on: on:
pull_request: pull_request:
types: [closed] types: [closed]
branches: [main] branches: [master]
jobs: jobs:
deploy: deploy:
# 关键:只在PR被成功merge时运行 # 只在PR被成功merge到master时运行
if: github.event.pull_request.merged == true if: github.event.pull_request.merged == true && github.base_ref == 'master'
runs-on: linux runs-on: linux
steps:
# 第一步:部署到Web服务器 steps:
- name: Deploy to web server # 第一步:获取master分支最新代码
- name: Fetch latest master branch code
run: | run: |
echo "=== Deploying to web server ===" echo "=== Fetching latest master branch code ==="
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:" if [ ! -d "/home/ly0kos/work/test_env" ]; then
ls -la /var/www/docs_dev/ | head -10 echo "Cloning repository..."
git clone http://localhost:3000/yuysh/Manlink_Doc.git /home/ly0kos/work/test_env
# 第二步:清理临时工作树
- 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 fi
echo "=== Process completed successfully ===" 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
+89 -26
View File
@@ -11,40 +11,102 @@ jobs:
runs-on: linux runs-on: linux
steps: steps:
# 第一步:检出PR分支代码 # 第一步:获取PR分支最新代码
- name: Checkout PR branch - name: Fetch latest PR branch code
uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }} # 检出PR分支而不是主分支
fetch-depth: 0
# 第二步:设置Python虚拟环境
- name: Set up Python virtual environment
run: | run: |
echo "Setting up Python environment for PR branch" echo "=== Fetching latest code for PR branch: ${{ github.head_ref }} ==="
python -m venv /home/ly0kos/work/test_env/.venv-pr-${{ github.head_ref }}
source /home/ly0kos/work/test_env/.venv-pr-${{ github.head_ref }}/bin/activate # 如果目录不存在,克隆仓库
pip install --upgrade pip if [ ! -d "/home/ly0kos/work/test_env" ]; then
if [ -f requirements.txt ]; then echo "Cloning repository..."
pip install -r requirements.txt git clone http://localhost:3000/yuysh/Manlink_Doc.git /home/ly0kos/work/test_env
else
echo "No requirements.txt found, installing sphinx"
pip install sphinx
fi 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)"
# 第步:构建文档(从PR分支) # 第步:复用或创建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 - name: Build documentation from PR branch
run: | run: |
echo "=== Building documentation from PR branch: ${{ github.head_ref }} ===" echo "=== Building documentation from PR branch: ${{ github.head_ref }} ==="
source /home/ly0kos/work/test_env/.venv-pr-${{ github.head_ref }}/bin/activate cd /home/ly0kos/work/test_env
source .venv-pr/bin/activate
# 在PR分支目录中构建 # 确保源目录存在
sphinx-build -b html ./docs ./build -v -j 4 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 }} rm -rf /home/ly0kos/work/test_env/pr-builds/${{ github.head_ref }}
mkdir -p /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 ./build/* /home/ly0kos/work/test_env/pr-builds/${{ github.head_ref }}/
echo "✓ Build completed for PR branch: ${{ github.head_ref }}" echo "✓ Build completed for PR branch: ${{ github.head_ref }}"
@@ -52,4 +114,5 @@ jobs:
- name: Verify build - name: Verify build
run: | run: |
echo "Build output for PR ${{ github.head_ref }}:" echo "Build output for PR ${{ github.head_ref }}:"
ls -la /home/ly0kos/work/test_env/pr-builds/${{ github.head_ref }}/ | head -10 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"