如何自动将Engine Scripting仓库的_site文件夹提交至Scripting Documentation仓库
自动将DocFX生成的_site内容推送到远程文档仓库的方案
我来帮你解决这个自动推送DocFX文档的问题,这里有两个实用的方案,分别适合远程CI/CD和本地开发场景,完全不需要手动复制文件或本地长期存放文档仓库:
方案一:使用GitHub Actions实现远程自动化推送
如果你的两个仓库都托管在GitHub上,GitHub Actions是最省心的选择——每次你把代码推送到Engine Scripting仓库的主分支时,它会自动生成文档并推送到Scripting Documentation仓库。
步骤:
- 在Engine Scripting仓库的根目录下创建
.github/workflows文件夹,然后新建一个名为docfx-publish.yml的文件。 - 将以下内容粘贴到文件中,记得替换注释里的占位内容:
name: Publish DocFX Documentation on: push: branches: [ main ] # 替换成你的Engine仓库主分支名(比如master) workflow_dispatch: # 允许手动触发这个工作流 jobs: publish-docs: runs-on: ubuntu-latest steps: - name: 拉取Engine Scripting仓库代码 uses: actions/checkout@v4 - name: 安装DocFX工具 run: dotnet tool install -g docfx - name: 用DocFX生成文档 run: docfx docfx.json # 替换成你的DocFX配置文件路径(如果不在根目录) - name: 克隆Scripting Documentation仓库到临时目录 uses: actions/checkout@v4 with: repository: your-username/Scripting-Documentation # 替换成你的文档仓库完整地址 path: docs-repo token: ${{ secrets.DOCS_REPO_TOKEN }} # 需要在Engine仓库的Secrets里添加这个令牌 - name: 替换文档仓库的旧内容 run: | rm -rf docs-repo/* cp -r _site/* docs-repo/ - name: 提交并推送新文档 working-directory: docs-repo run: | git config user.name "GitHub Actions" git config user.email "actions@github.com" git add . git commit -m "Update documentation from Engine Scripting [skip ci]" git push
关键注意事项:
- 创建访问令牌:在GitHub的个人设置里生成一个带有
repo权限的个人访问令牌(PAT),然后到Engine Scripting仓库的Settings > Secrets and variables > Actions里添加名为DOCS_REPO_TOKEN的Secret,把PAT填进去。 - DocFX配置:确保你的
docfx.json配置正确,能正常生成_site文件夹。如果项目需要特定.NET版本,可以在工作流里添加setup-dotnet步骤指定版本。
方案二:本地脚本实现编译后自动推送
如果需要在本地编译项目后立即推送文档,可以写一个Shell/PowerShell脚本,把它绑定到项目的编译后事件里,这样每次编译完成就自动执行推送。
PowerShell脚本(Windows)
# 替换成你的实际路径和仓库地址 $engineRepoPath = "C:\Projects\Engine-Scripting" $docsRepoUrl = "https://github.com/your-username/Scripting-Documentation.git" $tempDocsPath = "$env:TEMP\Scripting-Docs-Temp" # 生成DocFX文档 Set-Location $engineRepoPath docfx docfx.json # 克隆文档仓库到临时目录(用完自动删除) if (Test-Path $tempDocsPath) { Remove-Item -Recurse -Force $tempDocsPath } git clone $docsRepoUrl $tempDocsPath # 替换旧文档内容 Remove-Item -Recurse -Force "$tempDocsPath\*" Copy-Item -Recurse -Force "$engineRepoPath\_site\*" $tempDocsPath # 提交并推送 Set-Location $tempDocsPath git config user.name "你的名字" git config user.email "你的邮箱@example.com" git add . git commit -m "Update documentation from local Engine build" git push # 清理临时文件 Remove-Item -Recurse -Force $tempDocsPath
Shell脚本(Linux/macOS)
#!/bin/bash # 替换成你的实际路径和仓库地址 ENGINE_REPO_PATH="/home/you/projects/Engine-Scripting" DOCS_REPO_URL="https://github.com/your-username/Scripting-Documentation.git" TEMP_DOCS_PATH="/tmp/Scripting-Docs-Temp" # 生成DocFX文档 cd "$ENGINE_REPO_PATH" docfx docfx.json # 克隆文档仓库到临时目录 rm -rf "$TEMP_DOCS_PATH" git clone "$DOCS_REPO_URL" "$TEMP_DOCS_PATH" # 替换旧文档内容 rm -rf "$TEMP_DOCS_PATH"/* cp -r "$ENGINE_REPO_PATH/_site"/* "$TEMP_DOCS_PATH/" # 提交并推送 cd "$TEMP_DOCS_PATH" git config user.name "你的名字" git config user.email "你的邮箱@example.com" git add . git commit -m "Update documentation from local Engine build" git push # 清理临时文件 rm -rf "$TEMP_DOCS_PATH"
绑定到编译事件(以Visual Studio为例)
打开Engine Scripting的C#项目属性,找到“生成事件”选项卡,在“后期生成事件命令行”里添加:
powershell -ExecutionPolicy Bypass -File "C:\path\to\your\script.ps1"
这样每次编译项目完成后,脚本就会自动运行,完成文档生成和推送。
内容的提问来源于stack exchange,提问作者Tino Tano
相关产品推荐
相关产品推荐

