GitLab CI凹凸Python包版本

问题描述 投票:0回答:2

我想知道是否可以在 GitLab CI 运行程序中更改存储在 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]
  )

用于碰撞和发布的简单工作流程如下所示:在 github 和 pypi 上发布新的 python 包版本的最佳工作流程和实践

但是我们可以在

__init__.py
中自动升级版本,同时直接在 GitLab CI 中发布吗?

python python-3.x gitlab gitlab-ci
2个回答
7
投票

我喜欢为此使用凹凸2版本包。

这是我的 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

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

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

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

最后但并非最不重要的一点,值得一提的是:

  • 上面的示例使用了一个位于组中的存储库,如果您有一个不在组内的存储库,您可能需要更改 set-url 原始地址。

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

用途:

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

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

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

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

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


1
投票

一个选项是使用 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
© www.soinside.com 2019 - 2024. All rights reserved.