GitLab CI Bump Python包版本 [英] GitLab CI bump Python package version
问题描述
我想知道是否有可能在GitLab ci Runner中将存储在GitLab中的Python包版本升级到GitLab。
我有一个包结构示例:
/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
但我们是否可以在__init_.py中自动升级版本,同时直接在GitLab-ci中发布?
推荐答案
我喜欢使用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设置中的配置项变量。如果要在不受保护的分支上使用它们,请确保取消选中受保护检查。
MY_PUSH_TOKEN
-这是在我的配置文件中创建的个人访问令牌。它具有READ_REPOWORY和WRITE_REPOWORY权限。 我是此存储库的所有者/维护者,因此它授予推送此存储库的权限。PYPI_TOKEN
-可选,需要将包推送到pypi.org。
最后但同样重要的是,值得一提的是:
上面的示例使用的是组中的repo,如果您的单个repo不在组中,则可能需要更改set-url源地址。
-o ci.skip
参数防止生成管道触发循环
用法:
- 创建功能分支
- 推送代码
- 创建合并请求
- 合并MR到MASTER
Ci作业负责打包、发布、上载和跳转到下一个修补程序。
若要切换主要或次要功能,请从功能分支中的本地命令行手动调用并推送它。
Bump2Version工具也会自动处理标记。
我用来获得此解决方案的一些资源:
- https://medium.com/@tishkov.pavel/autotagging-in-gitlab-step-by-step-guide-c3a8dd47087a-在配置项中使用Git。
- https://williamhayes.medium.com/versioning-using-bumpversion-4d13c914e9b8-帮助我更好地了解Bump2版本
这篇关于GitLab CI Bump Python包版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!