在Travis CI中自动构建Mkdocs文档

Ric*_*all 6 python continuous-integration travis-ci mkdocs

我如何在Travis CI中自动部署我的Mkdocs文档?

Ric*_*all 13

以下是自动部署mkdocs文档的方法.请按照以下3个步骤操作.

步骤1

只需将以下代码段插入.travis.yml配置文件中各自的位置即可:

language: python # Set the build language to Python

python: 3.6 # Set the version of Python to use

branches: master # Set the branch to build from

install:
    - pip install mkdocs # Install the required dependencies

script: true # Skip script (Don't use this if one already exists)

before_deploy:
    - mkdocs build --verbose --clean --strict # Build a local version of the docs

deploy: # Deploy documentation to Github in the gh_pages branch
    provider: pages
    skip_cleanup: true
    github_token: $github_token
    local_dir: site
    on:
        branch: master
Run Code Online (Sandbox Code Playgroud)

第2步

如果您使用的是mkdocs的主题,没有mkdocsreadthedocs然后按照下面的步骤进行安装:

  • 场景1:主题可以通过pip安装(例如mkdocs-material)

    1. 附加pip install mkdocs您需要安装的其他软件包,例如mkdocs-materialpip install mkdocs mkdocs-material pymdown-extensions pygments
  • 方案2:主题是经由PIP安装(如docskimmer)

    1. 删除--strict参数mkdocs build --verbose --clean --strict以禁止使用不能通过pip安装的主题的可能错误.

    2. before_deploy上面的部分中添加设置主题所需的代码mkdocs build --verbose --clean

    before_deploy对于docskimmer,该部分中的代码如下所示:

    before_deploy:
        - git clone https://github.com/hfagerlund/mkdocs-docskimmer.git # Clone the repo hosting the code
        - cp -r $PWD/mkdocs-docskimmer/mkdocs_docskimmer . # Copy the required code to the repo root
        - cp -r $PWD/mkdocs-docskimmer/mkdocs_docskimmer/. ./docs # Copy the required code to the docs folder
        - mkdocs build --verbose --clean # Build a local version of the docs
    
    Run Code Online (Sandbox Code Playgroud)

    通过点不提供的主题安装可能会有所不同.

第3步

最后一步是告诉特拉维斯CI登录到您的帐户GitHub的推变化所需的凭据:

  1. 如果您已经使用public_repo范围设置了个人访问令牌,请跳至11
  2. 转到 URL.如果结算,请跳至步骤7.否则,照常继续执行这些说明.
  3. 转到Github帐户的设置
  4. 单击开发者设置
  5. 单击个人访问令牌
  6. 单击" 生成新令牌"
  7. 您可能需要输入GitHub密码才能授权创建
  8. Token description,为您的令牌选择一个名称 - 它可以是任何东西; 我给它命名就像Travis CI你可以将令牌重用于任意数量的存储库一样.
  9. 启用public_repo范围/权限
  10. 单击Generate token页面底部
  11. 转到要为其构建Mkdocs文档的Travis CI存储库的设置
  12. 使用以下设置创建环境变量:
    • 名称: github_token
    • 值: <THE TOKEN YOU JUST GENERATED>
    • 在构建日志中显示值: No
  13. 点击 add

后记

你完成了!请随时在评论中问我任何问题.

此外,如果该方法停止工作或不起作用,请在评论中告诉我,我会尽快解决它.