资深技术文档工程师,主导了四家企业的 知识管理建设 从0到1的系统性构建与落地,并为两家科研单位定制 科研文档管理体系。累计带领团队完成十多个 国家重大科研项目 逾千份技术资料编写整理。作为专家组成员参与中国标准化协会 《技术文档的用户体验评估规范》 的制定与评审工作; 参与编辑出版 《茂林幽趣》 等六本书籍。
大家是不是都遇到过这样的困境:项目版本频繁更新,而文档版本号却总是落后于代码。
每次手动修改 conf.py中的版本号不仅容易出错,还经常忘记同步更新?
更糟糕的是:
VERSION.txt管理项目版本今天,小编将分享如何用短短几行Python代码,让Sphinx文档版本与项目版本自动同步,彻底告别手动管理的烦恼。
⚠️ 干货预警:这篇文章有点儿长,但是 真香定律 承诺:
小团队/简单项目怎么办?别急!我们有简化方案 👉
在典型的项目中,我们通常有这样的结构:
project_root/├── VERSION.txt # 项目版本定义文件├── docs/│ ├── user_guide/│ │ ├── source/│ │ │ ├── conf.py # 用户手册Sphinx配置│ │ │ └── ...│ │ └── build/│ ├── api_guide/│ │ ├── source/│ │ │ ├── conf.py # API手册Sphinx配置│ │ │ └── ...│ │ └── build/│ ├── release_notes/ │ │ ├── source/│ │ │ ├── conf.py # 发布说明Sphinx配置│ │ │ └── ...│ │ └── build/└── ... # 其他项目文件传统做法是手动同步:
VERSION.txt → 2.0.1conf.py → version = '2.0.1'这种人工同步不仅低效,而且极易出错。一次疏忽就可能导致文档版本与实际代码版本不一致,给用户造成困惑。
Sphinx的conf.py本质上是一个Python脚本,这意味着我们可以在其中执行任意Python代码。利用这个特性,我们可以轻松实现版本自动同步。
首先,确保项目根目录有VERSION.txt文件:
2.1.3这个文件通常由开发团队维护,包含项目的当前版本号。
打开docs/source/conf.py,找到版本定义部分,通常长这样:
# 原来的手动版本管理version = '1.0.0'release = '1.0.0'将其替换为动态读取代码:
import osimport sysfrom pathlib import Path# 将项目根目录添加到Python路径sys.path.insert(0, os.path.abspath('../../..'))# 动态读取VERSION.txt文件def get_project_version(): """从项目根目录的VERSION.txt文件读取版本号""" # 获取项目根目录路径(向上两级到项目根目录) project_root = Path(__file__).parents[2] version_file = project_root / 'VERSION.txt' try: with open(version_file, 'r', encoding='utf-8') as f: version = f.read().strip() return version except FileNotFoundError: print(f"警告: 未找到版本文件 {version_file}") return "0.0.0" # 默认版本号# 自动设置Sphinx版本version = get_project_version() release = version# 可选:打印确认信息print(f"从VERSION.txt读取的文档版本: {release}")在项目根目录执行:
cd docs/user_guidemake html检查输出中是否显示正确的版本号:
从VERSION.txt读取的文档版本: 2.1.3Running Sphinx v5.0.0making output directory... donebuilding [mo]: targets for 0 po files that are out of datebuilding [html]: targets for 1 source files that are out of date...如果项目使用语义化版本控制(如2.1.3),并且你希望:
version显示主版本号(2.1)release显示完整版本号(2.1.3)可以这样修改:
def get_project_version(): """从项目根目录的VERSION.txt文件读取版本号""" project_root = Path(__file__).parents[2] version_file = project_root / 'VERSION.txt' try: with open(version_file, 'r', encoding='utf-8') as f: full_version = f.read().strip() # 提取主版本号(如从'2.1.3'提取'2.1') version_parts = full_version.split('.') if len(version_parts) >= 2: main_version = f"{version_parts[0]}.{version_parts[1]}" else: main_version = full_version return main_version, full_version except FileNotFoundError: print(f"警告: 未找到版本文件 {version_file}") return "0.0", "0.0.0"# 分别获取主版本号和完整版本号main_version, full_version = get_project_version()version = main_version # 短版本号: '2.1'release = full_version # 完整版本号: '2.1.3'如果项目在不同环境中有不同的版本文件:
def get_project_version(version_file_name='VERSION.txt'): """从项目根目录读取版本号,支持多种版本文件""" project_root = Path(__file__).parents[2] # 尝试多个可能的版本文件位置 possible_paths = [ project_root / version_file_name, project_root / 'version' / version_file_name, project_root / '.version', ] for version_file in possible_paths: if version_file.exists(): try: with open(version_file, 'r', encoding='utf-8') as f: version = f.read().strip() print(f"从 {version_file} 读取版本: {version}") return version except Exception as e: print(f"读取 {version_file} 时出错: {e}") print("警告: 未找到版本文件,使用默认版本") return "0.0.0"如果你的项目使用Git标签管理版本,可以直接从Git获取:
import subprocessdef get_version_from_git(): """从Git标签获取版本号""" try: # 获取最新的Git标签 result = subprocess.run( ['git', 'describe', '--tags', '--always'], capture_output=True, text=True, cwd=Path(__file__).parents[2] ) if result.returncode == 0: version = result.stdout.strip() # 清理标签格式(如从'v2.1.3-1-gabc1234'提取'2.1.3') if version.startswith('v'): version = version[1:] version = version.split('-')[0] return version except Exception as e: print(f"从Git获取版本失败: {e}") return "0.0.0"# 优先使用Git,回退到文件def get_project_version(): git_version = get_version_from_git() if git_version != "0.0.0": return git_version # 回退到文件读取 return get_version_from_file()VERSION.txtconf.py的修改纳入代码审查VERSION.txt的作用# GitHub Actions示例- name: Build documentation run: | cd docs/user_guide make html env: VERSION: $(cat VERSION.txt)你看,就这么个小改动,我们不仅给版本同步这事儿按了"自动挡",现在研发大佬们可以安心写他的代码,而文档工程师们再也不用发版本的时候,等到半夜就为了改个版本号啦。
重点来了:好工具不必是那种要培训三天才能上手的"重量级选手"。有时候,几行Python代码,就像给团队请了个不要工资、不请病假、还从不抱怨的版本管理小助理。
这自动化版本管理,就像给你的文档工程装了个自动驾驶,当然啦我们才刚挂上D挡,油门还没踩到底呢!后面还有更刺激的:
想知道下期先解锁哪个技能?在评论区大声说出你的需求,点赞最高的那个,小编连夜爆肝给你整出来!
关注不迷路,更多摸鱼大法持续放送中: