【问题标题】:GitLab CI bump Python package versionGitLab CI 凹凸 Python 包版本
【发布时间】:2019-07-28 05:37:30
【问题描述】:

我想知道是否可以在 gitlab ci runner 中增加存储在 gitlab 中的 Python 包版本。

我有示例包结构:

/package
  /src
    /__init__.py
     main.py
  setup.py
  Dockerfile
  .gitlab-ci.yml

init.py 包括:

  __version__ = '1.0.0'

setup.py 包括:

  setup(
        name='foo',
        version=src.__version__,
        packages=find_packages(),
        install_required=[foo, bar]
  )

碰撞和释放的简单工作流程如下所示:Best workflow and practices for releasing a new python package version on github and pypi

但是我们可以在 gitlab-ci 中直接发布的同时在 __init_.py 中自动更新版本吗?

【问题讨论】:

  • gitlab-ci 允许你使用任何你想要的 docker 镜像,所以不要直接在 gitlab-ci 的基础镜像上执行 python,只需使用你想要的任何版本的基于 python 的镜像并运行你的代码它。

标签: python python-3.x gitlab gitlab-ci


【解决方案1】:

我喜欢为此使用 bump2version 包。

这是我的 gitlab-ci.yml 与(几乎)最低限度的设置:

image: python:latest

# Change pip's cache directory to be inside the project directory since we can
# only cache local items.
variables:
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"

# Pip's cache doesn't store the python packages
# https://pip.pypa.io/en/stable/reference/pip_install/#caching
#
# If you want to also cache the installed packages, you have to install
# them in a virtualenv and cache it as well.
cache:
  paths:
    - .cache/pip
    - venv/

before_script:
  - python --version
  - pip install virtualenv
  - virtualenv venv
  - source venv/bin/activate
  - pip install -r requirements.txt

stages:
  - build
  - release

upload package:
  stage: release
  script:
    - pip install twine bump2version
    - bump2version --tag release
    - python setup.py sdist bdist_wheel
    - TWINE_PASSWORD=${PYPI_TOKEN} TWINE_USERNAME=__token__ python -m twine upload --repository-url https://upload.pypi.org/legacy/ dist/*
    # Necessary to avoid a bug corrupting the version in setup.py. Just in case it's forgotten to do manually. Now we only have to explicitly bump version for major or minors.
    - bump2version patch
    - git config --global user.email "${GITLAB_USER_EMAIL}"
    - git config --global user.name "${GITLAB_USER_NAME}"
    - git remote set-url origin "https://gitlab-ci-token:${MY_PUSH_TOKEN}@gitlab.com/${CI_PROJECT_NAMESPACE}/${CI_PROJECT_NAME}.git"
    - git push -o ci.skip --tags origin HEAD:${CI_COMMIT_REF_NAME}
  artifacts:
    paths:
      - dist/*.whl
  only:
    - master

我的项目根目录中还有一个.bumpversion.cfg 文件,其中包含以下内容:

[bumpversion]
commit = True
tag = False
current_version = 0.1.0-dev0
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)(\-(?P<release>[a-z]+)(?P<build>\d+))?
serialize = 
    {major}.{minor}.{patch}-{release}{build}
    {major}.{minor}.{patch}

[bumpversion:file:setup.py]

[bumpversion:part:build]

[bumpversion:part:release]
optional_value = gamma
values = 
    dev
    gamma

使用了两个自定义变量。它们需要添加到 repo 设置中的 CI 变量中。如果你想在非受保护的分支上使用它们,请确保取消选中受保护的检查。

  • MY_PUSH_TOKEN - 这是在我的个人资料中创建的个人访问令牌。它具有 read_repository 和 write_repository 权限。 我是此存储库的所有者/维护者,因此它授予推送此存储库的权限。

  • PYPI_TOKEN - 可选,需要将包推送到 pypi.org。

最后但同样重要的是,值得一提:

  • 上面的例子使用了一个组中的repo,如果你有一个不在组内的repo,你可能需要更改set-url origin地址。

  • -o ci.skip 参数防止构建管道触发循环

用法:

  • 创建功能分支
  • 推送代码
  • 创建合并请求
  • 将 MR 合并到 master

ci 工作负责打包、发布、上传以及碰撞到下一个补丁。

要调整主要或次要,请在本地功能分支中从命令行手动调用它并推送它。

bump2version 工具也会自动处理标记。

我用来获取此解决方案的一些资源:

【讨论】:

    【解决方案2】:

    一个选项是使用setuptools_scm(来自 Python Packaging Authority)。为了确定版本setuptools_scm 看看三件事:

    • 最新标签(带有版本号)
    • 到此标签的距离(例如,自最新标签以来的修订数)
    • Workdir 状态(例如,自最新标签以来未提交的更改)

    如果您有一个自动标记您的版本的机制,上述方法效果最佳,但您可以选择手动添加标签。在任何情况下,您想要setuptools_scm 获取最新标签(例如2.1.12)并使用它来更新您的库版本。

    以下示例说明了典型设置的外观。我使用semantic-delivery-gitlab(它使用基于提交消息的语义版本控制)来标记各种提交,但其他方式也是可能的。 master 分支被视为发布分支。

    配置setuptools_scm:

    # pyproject.toml
    [build-system]
    requires = ["setuptools>=45", "wheel", "setuptools_scm>=6.2"]
    
    [tool.setuptools_scm]
    write_to = "my_library/__version__.py"
    
    

    获取版本:

    # `my_library/__init__.py`
    try:
        from my_library.__version__ import version as __version__
    except ImportError:
        pass
    

    最小.gitlab-ci.yaml:

    # .gitlab-ci.yaml
    stages:
      - build
      - release
      - publish
    
    build:
      stage: build
      script:
        - pip install --upgrade pip
        - pip install setuptools setuptools_scm[toml] --upgrade
        - python setup.py bdist_wheel
      artifacts:
        expire_in: 7 days
        paths:
          - dist/*
    
    .publish:
      stage: publish
      script:
        - WHEEL=$(ls dist)
        - publish_artifact.sh # Upload wheel to repository manager (e.g. artifactory)
     
    publish-snapshot:
      <<: *publish
      except:
        - tags
        - master
    
    publish-release:
      <<: *publish
      only:
        - tags
    
    release:
      stage: release
      script:
        - npx @hutson/semantic-delivery-gitlab --token ${GITLAB_AUTH_TOKEN}
      only:
        - master
      when: manual # Manually trigger the tagging job for better control
    

    您可能还想将my_library/__version__.py 添加到.gitignore。在此过程结束时,您可以安装软件包并确认它具有正确的版本

    >>> import my_library
    >>> my_library.__version__
    1.0.1
    

    【讨论】:

      猜你喜欢
      • 2020-10-24
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      • 2020-09-09
      • 2014-04-04
      • 1970-01-01
      • 2019-09-24
      • 1970-01-01
      相关资源
      最近更新 更多