You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让.readthedocs.yaml执行完pre_install命令后终止运行?

如何在Read the Docs构建中执行完自定义前置步骤后终止脚本运行

我的.readthedocs.yaml配置

# Required
version: 2

# Set the version of Python and other tools you might need
build:
  os: ubuntu-22.04
  tools:
    python: "3.12"
  apt_packages:
    - cmake
    - gfortran
    - gcc
    - g++
    - git
  jobs:
    pre_install:
      - echo "in build - jobs - pre_install:"
      - echo "READTHEDOCS_OUTPUT=$READTHEDOCS_OUTPUT "
      - pwd; ls -lt 
      - cmake --version
      - git --version
      - python -V
      - pip install "sphinx<7.0.0" sphinx_rtd_theme sphinxcontrib-bibtex
      - sphinx-build  --version
      - git submodule update --init --recursive
      - ./setup $READTHEDOCS_OUTPUT
      - pwd
      - ls -lt
      - cd $READTHEDOCS_OUTPUT/; pwd; ls -lt; make html; pwd; ls -lt; ls -lt html/ ; exit $?
      - exit 0

问题

执行完pre_install中的所有命令后,脚本仍会继续运行一系列默认命令(如python -m pip install --upgrade --no-cache-dir pip setuptools、python -m pip install --upgrade --no-cache-dir sphinx、cat doc/conf.py等),导致构建崩溃,需要彻底终止脚本运行。

解决方案

推荐方案:使用script字段接管完整构建流程

将jobs.pre_install替换为build.script,这样Read the Docs会跳过所有默认构建步骤,只执行你定义的自定义命令:

# Required
version: 2

# Set the version of Python and other tools you might need
build:
  os: ubuntu-22.04
  tools:
    python: "3.12"
  apt_packages:
    - cmake
    - gfortran
    - gcc
    - g++
    - git
  script:
    - echo "in custom build script:"
    - echo "READTHEDOCS_OUTPUT=$READTHEDOCS_OUTPUT "
    - pwd; ls -lt 
    - cmake --version
    - git --version
    - python -V
    - pip install "sphinx<7.0.0" sphinx_rtd_theme sphinxcontrib-bibtex
    - sphinx-build  --version
    - git submodule update --init --recursive
    - ./setup $READTHEDOCS_OUTPUT
    - pwd
    - ls -lt
    - cd $READTHEDOCS_OUTPUT/; pwd; ls -lt; make html; pwd; ls -lt; ls -lt html/ ; exit $?

原理:pre_install只是构建流程中的前置环节,执行完毕后仍会进入默认的依赖安装、文档构建阶段。而script字段用于定义完整的自定义构建逻辑,Read the Docs会完全遵循你指定的命令序列执行,不再触发任何默认步骤。

备选方案:强制终止流程(不推荐)

如果必须保留pre_install环节,可以在pre_install的最后一步执行exit 1,强制中断后续流程。但这种方式会将构建标记为失败状态,即使你已经成功生成了文档,因此仅作为临时应急方案:

# 修改后的pre_install最后几步
jobs:
  pre_install:
    # ... 其他命令 ...
    - cd $READTHEDOCS_OUTPUT/; pwd; ls -lt; make html; pwd; ls -lt; ls -lt html/ ; exit $?
    - exit 1

内容的提问来源于stack exchange,提问作者Miro Iliaš

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 12:28:12