如何用install-poetry写出多操作系统×多Python版本的测试矩阵:完整实战示例

📅 2026/8/26 14:29:09
如何用install-poetry写出多操作系统×多Python版本的测试矩阵:完整实战示例
如何用install-poetry写出多操作系统×多Python版本的测试矩阵完整实战示例【免费下载链接】install-poetryGithub action for installing and configuring Poetry项目地址: https://gitcode.com/gh_mirrors/in/install-poetryinstall-poetry是一个用于安装并配置 Poetry 的 GitHub Action。本文带你写出多操作系统 × 多Python版本的完整测试矩阵实战示例一份 workflow 文件让同一套测试自动跑遍 Ubuntu、macOS甚至 Windows上的多个 Python 版本并配合 venv 缓存把 CI 提速到秒级回滚失败。为什么用 install-poetry 搭测试矩阵写矩阵测试时最大的烦恼是环境准备每个操作系统、每个 Python 版本都要先装好一套一致的 Poetry 和虚拟环境还要处理不同系统的路径差异。install-poetry 把这些一次性解决了✅一步装好 Poetry自动下载官方安装脚本并固定到指定提交供应链安全无需手动写安装逻辑✅自动配置虚拟环境通过输入参数即可控制是否创建 venv、venv 放项目内还是缓存目录、是否并行安装✅跨系统统一体验脚本会写入VENV环境变量见 main.shWindows 与类 Unix 系统都能用同一行source $VENV激活环境✅官方已验证该 Action 在 macOS 和 Ubuntu runner 上、Poetry ≥ 1.8 版本经过完整测试Windows 需要少量额外配置下文给出。所有输入参数都定义在 action.yml 中这也是理解该 Action 能力的说明书。先搞懂这 7 个核心输入参数在写矩阵之前先速览 action.yml 里暴露的参数它们是配置 Poetry 的全部开关参数默认值作用versionlatest指定要安装的 Poetry 版本如2.4.1virtualenvs-createtrue是否创建虚拟环境virtualenvs-in-projectfalsevenv 是否放在项目目录推荐矩阵场景设为true方便缓存virtualenvs-path{cache-dir}/virtualenvsvenv 的存放路径installer-paralleltrue并行安装依赖速度更快installation-arguments空透传给安装脚本的额外参数如--forceplugins空空格分隔的插件列表如poetry-plugin-a 小建议矩阵场景下把virtualenvs-in-project: true打开venv 就固定落在.venv缓存步骤会变得非常简单。完整实战操作系统 × Python 版本 × 依赖版本 三合一矩阵下面是一份可直接抄走、按需裁剪的完整 workflow。它演示了 GitHub Actions 的strategy.matrix如何与 install-poetry 配合name: test on: pull_request jobs: test: strategy: fail-fast: true matrix: os: [ ubuntu-latest, macos-latest ] python-version: [ 3.10, 3.11, 3.12, 3.13, 3.14 ] django-version: [ 4, 5 ] runs-on: ${{ matrix.os }} steps: - name: Check out repository uses: actions/checkoutv6 - name: Set up python ${{ matrix.python-version }} id: setup-python uses: actions/setup-pythonv6 with: python-version: ${{ matrix.python-version }} - name: Install Poetry uses: snok/install-poetryv1 with: virtualenvs-create: true virtualenvs-in-project: true - name: Load cached venv id: cached-poetry-dependencies uses: actions/cachev5 with: path: .venv key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles(**/poetry.lock) }} - name: Install dependencies if: steps.cached-poetry-dependencies.outputs.cache-hit ! true run: poetry install --no-interaction --no-root - name: Install library run: poetry install --no-interaction - name: Install django ${{ matrix.django-version }} run: | source .venv/bin/activate pip install Django${{ matrix.django-version }} - name: Run tests run: | source .venv/bin/activate pytest tests/ coverage report逐段拆解这份测试矩阵示例1矩阵定义os、python-version、django-version三个维度会做笛卡尔积2 系统 × 5 版本 × 2 依赖版本 20 个并行任务。runs-on: ${{ matrix.os }}让每个任务自动落到对应系统。2fail-fast: true任意一个组合失败就停掉其余任务帮你更快定位问题想让所有组合跑完再看报告改成false即可。3缓存 key 是精髓venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles(**/poetry.lock) }}—— 按系统 Python 版本 锁文件哈希生成缓存键。只要 poetry.lock 没变重复运行就能秒级复用已装好的 venv20 个矩阵任务都能受益。4install-poetry 只需三行矩阵里每个任务都会独立执行安装与配置互不干扰这正是 Action 化环境准备的价值。想让 Windows 也进矩阵两步搞定把windows-latest加进os列表后有两处差异需要处理install-poetry 对 Windows 有专门支持见 README.md 的 Running on Windows 章节第一步设置 job 级默认 shell 为 bashdefaults: run: shell: bash第二步用source $VENV激活环境Windows 的 venv 激活脚本在.venv/scripts/activate类 Unix 在.venv/bin/activate。install-poetry 会根据 runner 系统自动写入VENV环境变量逻辑在 main.sh 中所以跨系统统一写法就是- run: | source $VENV pytest --version⚠️ 一个 Windows 缓存坑在 Windows runner 上直接缓存整个.venv表现不稳定官方仓库曾出现工作流卡死 3 小时以上才手动取消的情况。稳妥做法是改缓存 pip 轮子目录如~/.cache速度损失可接受。再快 10 秒缓存 Poetry 安装本身除了 venvPoetry 安装本身也值得缓存——官方估算可省约 10 秒约占该 Action 运行时间的 95%- name: Load cached Poetry installation id: cached-poetry uses: actions/cachev5 with: path: ~/.local # 该路径依赖操作系统 key: poetry-0 # 想重置缓存就递增这个数字 - name: Install Poetry if: steps.cached-poetry.outputs.cache-hit ! true uses: snok/install-poetryv1注意一个细节缓存命中时 Install Poetry 步骤被跳过with里的配置不会重新应用。此时可补一个专用步骤重新应用配置- name: Configure poetry if: steps.cached-poetry.outputs.cache-hit true run: poetry config virtualenvs.in-project true常见踩坑清单忘了 Windows 的 bash shell不加defaults.run.shell: bash含source的命令步骤会直接报错。缓存 key 少了维度key 里漏掉runner.os或 Python 版本不同系统会互相命中对方不兼容的 venv。PyPI DNS 解析报错出现偶发解析失败时把installer-parallel: false改为逐个安装即可绕过action.yml 中有说明。版本漂移导致 CI 偶发失败version不指定时装的是最新版建议锁死如version: 2.4.1与本地开发环境保持一致。锁文件没提交hashFiles(**/poetry.lock)依赖 poetry.lock 入库否则缓存 key 每次都在变缓存形同虚设。小结本文的 install-poetry 测试矩阵实战示例核心就三件事用strategy.matrix声明操作系统与 Python 版本组合runs-on: ${{ matrix.os }}自动分流install-poetry 三行搞定每个任务的 Poetry 安装与虚拟环境配置跨系统靠source $VENV统一激活用系统 Python 版本 poetry.lock 哈希构造缓存键venv 与 Poetry 安装双重缓存把矩阵跑快再跑稳。把它放进你的项目.github/workflows目录改一改矩阵维度和测试命令多系统、多版本的回归测试就有了。【免费下载链接】install-poetryGithub action for installing and configuring Poetry项目地址: https://gitcode.com/gh_mirrors/in/install-poetry创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考