BSD 3-Clause "New" or "Revised" License

技术解读

以下内容由 GitRead 中文社区 编辑团队基于项目 README 与官方资料整理, 结合中文开发者的常见使用场景补充技术选型建议。具体以项目官方文档为准。

分析文档:GitHub仓库 JRubics/poetry-publish

项目背景与痛点

在Python社区中,发布Python包至PyPI(Python Package Index)是项目维护者和开源贡献者的常见任务。然而,这一过程常常涉及繁琐的配置和工具链管理。JRubics/poetry-publish项目旨在通过简化这一流程来解决自动化构建与发布中的痛点。使用poetry-publish可以有效地利用Poetry这一现代化的Python包管理工具,自动完成打包、验证和发布过程,提升工程效率。

解决的问题

  1. 精简流程:自动化处理构建、打包和发布,无需复杂的命令行操作。
  2. 降低出错率:内置的Poetry生态系统保证包的依赖关系得到严格管理。
  3. 提高效率:统一标准,避免手工配置时常见的小错误。

核心技术揭秘

本节揭示项目的实现方式,尤其是关键技术栈、设计模式和数据流程。关键是理解它是如何通过 Poety 构建、且如何顺畅地将产物发布至 PyPI 服务器的。

graph TD A[开发者触发发布] --> B[调用 poeetry-publish action] B --> C{"是否为大版本"} C -->|"否"| D[构建包 (poetry build)] C -->|"是"| E[修改版本号 (poetry version) -> build] D --> F[验证包 (poetry check)] E --> F F --> G[上传至 PyPi (poetry publish)] G --> H[完成发布]

技术总结

  • 该项目使用了GitHub Actions,从而无缝集成到现有的CI/CD流程中。
  • Action文件配置根据触发事件(比如Pull Request)执行具体的Poetry命令。
  • 构建包的流程依赖于Poetry的Build API,简化了在不同Python环境间的操作标准化。成功构建后,通过Poetry Publish API将包推送至PyPI。

功能亮点与差异

  • 自动化与集成:无缝接入GitHub Actions,简化了发布流程。
  • 版本管理:通过Poetry精确管理Python版本及依赖关系,确保发布的包质量。
  • 配置灵活性:用户可以针对不同的发布需求进行定制配置,满足多种项目发布要求。

应用场景与落地建议

此行动适用于频繁发布Python包的团队。在生产环境中使用时,建议:

  1. 组件化测试,确保poetry-publish与现有基建协作顺畅。
  2. 依据项目复杂性,调整Action中的条件分支逻辑,如特定版本条件下的特殊处理。
  3. 考虑项目规模和依赖,细致配置好Poetry的依赖文件,以保障发布包的完整性。

综合评价

优点

  • 高度自动化,减少人为错误。
  • 使用Poetry提升项目依赖管理效率。
  • 配置灵活,可针对不同项目进行优化。

缺点

  • 若非Poetry项目,可能需做额外配置调整。
  • 设定版本进化规则需谨慎,误操作可导致版本混淆。

JRubics/poetry-publish提供了流畅简便的操作体验,有助于简化Python包的发布流程。作为技术主导者,项目充分展现了当下DevOps趋势下的自动化价值。对于频繁维护使用Python包的开发者,推荐考虑使用此工具,以提高生产效率。

关于

GitHub 原始描述(英文)

An action to build and publish python package to pypi (https://pypi.org/) using poetry (https://github.com/sdispater/poetry)

描述取自 GitHub 官方 API,保持仓库作者书写的原文。

简要分析

热度分
324
价值分
107
活跃状态
活跃

语言占比

Dockerfile
Shell

Release

README

poetry-publish

Maintenance Latest release

An action to build and publish python package to PyPI, TestPyPI or a private wheels repo using poetry.

Inputs

python_version

The version of python to install (default: latest). Use default for a shorter build time.

poetry_version

The version of poetry to install (default: latest).

pypi_token

Required API token to authenticate when uploading package to PyPI (You can find your token here).

repository_name

The name of a repository where the package will be uploaded. Necessary if you’d like to upload to test PyPi or a private wheels repo. Uploads to official PyPi if not informed.

repository_url

The URL where the package will be uploaded. Necessary if you’d like to upload to test PyPi or a private wheels repo. Uploads to official PyPi if not informed.

repository_username

The Username to log in into a repository where the package will be uploaded if using http-basic authentication instead of api token.

repository_password

The Password to log in into a repository where the package will be uploaded if using http-basic authentication instead of api token.

build_format

By default, poetry’s build command outputs two formats: wheel and sdist. If you intend to use only one of them, you may specify that with the build_format parameter.

poetry_install_options

Adds possibility to pass options to “poetry install” command. Examples:

  • --without dev
  • --only-root
  • --sync

Check the full list here.

poetry_publish_options

Adds possibility to pass options to “poetry publish” command. Examples:

  • --dry-run
  • --build

Check the full list here.

allow_poetry_pre_release

Allow poetry pre-release versions to be installed.

extra_build_dependency_packages

An optional space-separated list of debian packages to be installed before building the package

plugins

An optional space-separated list of poetry plugins to be installed before building the package

package_directory

An optional subdirectory path if poetry package doesn’t reside in the main workflow directory

Note: ignore_dev_requirements command is deprecated in version 2.0. Use poetry_install_options: "--without dev" instead

Example usage

The following will build and publish the python package to the PyPI using the last version of python and poetry. Specify the python package version and dependencies in pyproject.toml in the root directory of your project.

- name: Build and publish to pypi
  uses: JRubics/[email protected]
  with:
    pypi_token: ${{ secrets.PYPI_TOKEN }}

Python and poetry versions can be specified in inputs as well as the build_format and allow_poetry_pre_release.

- name: Build and publish to pypi
  uses: JRubics/[email protected]
  with:
    python_version: "3.7.1"
    poetry_version: "==1.0.5" # (PIP version specifier syntax)
    pypi_token: ${{ secrets.PYPI_TOKEN }}
    build_format: "sdist"
    allow_poetry_pre_release: "yes"

Poetry options for install and publish commands can be specified through poetry_install_options and poetry_publish_options.

- name: Build and publish to pypi
  uses: JRubics/[email protected]
  with:
    pypi_token: ${{ secrets.PYPI_TOKEN }}
    poetry_install_options: "--sync --no-root"
    poetry_publish_options: "--dry-run"

Repository can be changed to TestPyPI or a private wheels repo by specifying repository_name and repository_url.

- name: Build and publish to pypi
  uses: JRubics/[email protected]
  with:
    pypi_token: ${{ secrets.PYPI_TOKEN }}
    repository_name: "testpypi"
    repository_url: "https://test.pypi.org/legacy/"

Repository authentication can be changed to http-basic authentication by specifying repository_username and repository_password instead of pypi_token.

- name: Build and publish to private Python package repository
  uses: JRubics/[email protected]
  with:
    repository_name: "foo"
    repository_url: "https://foo.bar/simple/"
    repository_username: "username"
    repository_password: "password"

Extra debian packages can be installed before building the python package. This is particularly useful for packages that do not need to be included in the base image for this action.

- name: Build and publish to pypi
  uses: JRubics/[email protected]
  with:
    pypi_token: ${{ secrets.PYPI_TOKEN }}
    extra_build_dependency_packages: "capnproto libzmq3-dev"

Poetry plugins can be added by specifying plugins input

- name: Build and publish to pypi
  uses: JRubics/[email protected]
  with:
    pypi_token: ${{ secrets.PYPI_TOKEN }}
    plugins: "poetry-dynamic-versioning[plugin]"

Poetry package can be built from a subdirectory. It should be specified as package_directory

- name: Build and publish to pypi
  uses: JRubics/[email protected]
  with:
    pypi_token: ${{ secrets.PYPI_TOKEN }}
    package_directory: "subdirectory"

Example workflow

The following will build and publish the python package when project is tagged in the v*.*.* form.

name: Python package
on:
  push:
    tags:
      - "v*.*.*"
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Build and publish to pypi
        uses: JRubics/[email protected]
        with:
          pypi_token: ${{ secrets.PYPI_TOKEN }}

评论

暂无评论