使用 github 操作发布文档
Using github actions to publish documentation
我考虑的是:
- github 提供 github 页面以在我的
master
分支或专用 gh-pages
分支上的文件夹中托管文档,但这意味着提交构建工件
- 我也可以让
readthedocs
通过 webhooks 为我构建和托管文档,但这意味着在我尝试整合与我的项目相关的所有内容的时间点学习如何配置另一个工具 github-actions
我已经有一个适合我的文档构建过程(使用 sphinx
作为构建器)并且我也可以在本地进行测试,所以我宁愿只是利用它。它设置了所有规则并在工件中放置了一些静态 html
- 它只是无法在任何地方得到服务。在我的项目的所有其他部署配置所在的工作流中处理它,感觉比将它分散在不同的工具或 github 特定选项上感觉更好。
市场上是否已经存在允许我执行此类操作的操作?
name: CI
on: [push]
jobs:
... # do stuff like building my-project-v1.2.3.whl, testing, etc.
release_docs:
steps:
- uses: actions/sphinx-to-pages@v1 # I wish this existed
with:
dependencies:
- some-sphinx-extension
- dist/my-project*.whl
apidoc_args:
- "--no-toc"
- "--module-first"
- "-o docs/autodoc"
- "src/my-project"
build-args:
- "docs"
- "public" # the content of this folder will then be served at
# https://my_gh_name.github.io/my_project/
换句话说,我仍然希望能够控制构建的方式和工件的放置位置,但不想处理与 readthedocs
或 github-pages
的交互.
###我尝试过的操作
❌ deploy-to-github-pages:在 npm 容器中运行文档构建 - 使其与 python 和 sphinx
一起工作会很不方便
❌ gh-pages-for-github-action: 没有文档
❌ gh-pages-deploy: 似乎以 jekyll 之类的主机环境为目标,而不是静态内容,并且 正确使用尚未记录的 yml 语法 - 我尝试了一点但不能'让它工作
❌ github-pages-deploy:看起来不错,但尚未记录 yml 语法的正确用法
✅ github-pages: needs a custom PAT in order to trigger rebuilds (which is inconvenient) and uploads broken html(这很糟糕,但可能是我的错)
✅ deploy-action-for-github-pages:也有效,并且在日志中看起来更干净一些。尽管与上面的解决方案有相同的限制,它需要一个 PAT 并且服务 html 仍然是坏的。
在行动市场上搜索 github+pages 时的其他 11 个结果看起来都像是他们想使用自己的构建器,遗憾的是从来没有碰巧是狮身人面像。
我让它工作了,但是目前还没有专门的操作来在 github pages
或 readthedocs
上构建和托管 sphinx 文档,所以就我而言,有相当多的这里还有一些不足之处。
这是我当前的 release_sphinx
作业,它使用 deploy-action-for-github-pages action 并上传到 github-pages
:
release_sphinx:
needs: [build]
runs-on: ubuntu-latest
container:
image: python:3.6
volumes:
- dist:dist
- public:public
steps:
# check out sources that will be used for autodocs, plus readme
- uses: actions/checkout@v1
# download wheel that was build and uploaded in the build step
- uses: actions/download-artifact@v1
with:
name: distributions
path: dist
# didn't need to change anything here, but had to add sphinx.ext.githubpages
# to my conf.py extensions list. that fixes the broken uploads
- name: Building documentation
run: |
pip install dist/*.whl
pip install sphinx Pallets-Sphinx-Themes
sphinx-apidoc --no-toc --module-first -o docs/autodoc src/stenotype
sphinx-build docs public -b dirhtml
# still need to build and set the PAT to get a rebuild on the pages job,
# apart from that quite clean and nice
- name: github pages deploy
uses: peaceiris/actions-gh-pages@v2.3.1
env:
PERSONAL_TOKEN: ${{ secrets.PAT }}
PUBLISH_BRANCH: gh-pages
PUBLISH_DIR: public
# since gh-pages has a history, this step might no longer be necessary.
- uses: actions/upload-artifact@v1
with:
name: documentation
path: public
感谢部署操作的维护者,他解决了上传问题 within 8 minutes 我将其作为问题发布。
在使用 pip (requirements.txt
)、pipenv 或 poetry 管理 sphinx
的情况下,我们可以将我们的文档部署到 GitHub 页面,如下所示。对于其他 Python-based 静态站点生成器,如 pelican 和 MkDocs,工作流程也是一样的。这是 MkDocs 的一个简单示例。我们只需将工作流程添加为 .github/workflows/gh-pages.yml
有关更多选项,请参阅最新的 README:peaceiris/actions-gh-pages: GitHub Actions for GitHub Pages Deploy static files and publish your site easily. Static-Site-Generators-friendly.
name: github pages
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v2
- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: '3.8'
- name: Upgrade pip
run: |
# install pip=>20.1 to use "pip cache dir"
python3 -m pip install --upgrade pip
- name: Get pip cache dir
id: pip-cache
run: echo "::set-output name=dir::$(pip cache dir)"
- name: Cache dependencies
uses: actions/cache@v2
with:
path: ${{ steps.pip-cache.outputs.dir }}
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install dependencies
run: python3 -m pip install -r ./requirements.txt
- run: mkdocs build
- name: Deploy
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./site
我考虑的是:
- github 提供 github 页面以在我的
master
分支或专用gh-pages
分支上的文件夹中托管文档,但这意味着提交构建工件 - 我也可以让
readthedocs
通过 webhooks 为我构建和托管文档,但这意味着在我尝试整合与我的项目相关的所有内容的时间点学习如何配置另一个工具github-actions
我已经有一个适合我的文档构建过程(使用 sphinx
作为构建器)并且我也可以在本地进行测试,所以我宁愿只是利用它。它设置了所有规则并在工件中放置了一些静态 html
- 它只是无法在任何地方得到服务。在我的项目的所有其他部署配置所在的工作流中处理它,感觉比将它分散在不同的工具或 github 特定选项上感觉更好。
市场上是否已经存在允许我执行此类操作的操作?
name: CI
on: [push]
jobs:
... # do stuff like building my-project-v1.2.3.whl, testing, etc.
release_docs:
steps:
- uses: actions/sphinx-to-pages@v1 # I wish this existed
with:
dependencies:
- some-sphinx-extension
- dist/my-project*.whl
apidoc_args:
- "--no-toc"
- "--module-first"
- "-o docs/autodoc"
- "src/my-project"
build-args:
- "docs"
- "public" # the content of this folder will then be served at
# https://my_gh_name.github.io/my_project/
换句话说,我仍然希望能够控制构建的方式和工件的放置位置,但不想处理与 readthedocs
或 github-pages
的交互.
###我尝试过的操作
❌ deploy-to-github-pages:在 npm 容器中运行文档构建 - 使其与 python 和 sphinx
一起工作会很不方便❌ gh-pages-for-github-action: 没有文档
❌ gh-pages-deploy: 似乎以 jekyll 之类的主机环境为目标,而不是静态内容,并且 正确使用尚未记录的 yml 语法 - 我尝试了一点但不能'让它工作
❌ github-pages-deploy:看起来不错,但尚未记录 yml 语法的正确用法
✅ github-pages: needs a custom PAT in order to trigger rebuilds (which is inconvenient) and uploads broken html(这很糟糕,但可能是我的错)
✅ deploy-action-for-github-pages:也有效,并且在日志中看起来更干净一些。尽管与上面的解决方案有相同的限制,它需要一个 PAT 并且服务 html 仍然是坏的。
在行动市场上搜索 github+pages 时的其他 11 个结果看起来都像是他们想使用自己的构建器,遗憾的是从来没有碰巧是狮身人面像。
我让它工作了,但是目前还没有专门的操作来在 github pages
或 readthedocs
上构建和托管 sphinx 文档,所以就我而言,有相当多的这里还有一些不足之处。
这是我当前的 release_sphinx
作业,它使用 deploy-action-for-github-pages action 并上传到 github-pages
:
release_sphinx:
needs: [build]
runs-on: ubuntu-latest
container:
image: python:3.6
volumes:
- dist:dist
- public:public
steps:
# check out sources that will be used for autodocs, plus readme
- uses: actions/checkout@v1
# download wheel that was build and uploaded in the build step
- uses: actions/download-artifact@v1
with:
name: distributions
path: dist
# didn't need to change anything here, but had to add sphinx.ext.githubpages
# to my conf.py extensions list. that fixes the broken uploads
- name: Building documentation
run: |
pip install dist/*.whl
pip install sphinx Pallets-Sphinx-Themes
sphinx-apidoc --no-toc --module-first -o docs/autodoc src/stenotype
sphinx-build docs public -b dirhtml
# still need to build and set the PAT to get a rebuild on the pages job,
# apart from that quite clean and nice
- name: github pages deploy
uses: peaceiris/actions-gh-pages@v2.3.1
env:
PERSONAL_TOKEN: ${{ secrets.PAT }}
PUBLISH_BRANCH: gh-pages
PUBLISH_DIR: public
# since gh-pages has a history, this step might no longer be necessary.
- uses: actions/upload-artifact@v1
with:
name: documentation
path: public
感谢部署操作的维护者,他解决了上传问题 within 8 minutes 我将其作为问题发布。
在使用 pip (requirements.txt
)、pipenv 或 poetry 管理 sphinx
的情况下,我们可以将我们的文档部署到 GitHub 页面,如下所示。对于其他 Python-based 静态站点生成器,如 pelican 和 MkDocs,工作流程也是一样的。这是 MkDocs 的一个简单示例。我们只需将工作流程添加为 .github/workflows/gh-pages.yml
有关更多选项,请参阅最新的 README:peaceiris/actions-gh-pages: GitHub Actions for GitHub Pages Deploy static files and publish your site easily. Static-Site-Generators-friendly.
name: github pages
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v2
- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: '3.8'
- name: Upgrade pip
run: |
# install pip=>20.1 to use "pip cache dir"
python3 -m pip install --upgrade pip
- name: Get pip cache dir
id: pip-cache
run: echo "::set-output name=dir::$(pip cache dir)"
- name: Cache dependencies
uses: actions/cache@v2
with:
path: ${{ steps.pip-cache.outputs.dir }}
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install dependencies
run: python3 -m pip install -r ./requirements.txt
- run: mkdocs build
- name: Deploy
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./site