我想知道是否可以在 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]
)
碰撞和释放的简单工作流程如下所示:在 github 和 pypi 上发布新的 python 包版本的最佳工作流程和实践 https://stackoverflow.com/questions/53122766/best-workflow-and-practices-for-releasing-a-new-python-package-version-on-github
但是我们可以自动在 __init_.py 中修改版本,同时直接在 gitlab-ci 中发布吗?
我喜欢为此使用凹凸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 变量中。如果您想在不受保护的分支上使用它们,请确保取消选中受保护的检查。
最后但并非最不重要的一点,值得一提的是:
Usage:
- 创建功能分支
- 推送代码
- 创建合并请求
- 合并 MR 到 master
ci 工作负责打包、发布、上传和跳转到下一个补丁。
要更改主要或次要功能,请在功能分支中本地从命令行手动调用它并推送它。
凹凸2版本工具也会自动处理标记。
我用来获得此解决方案的一些资源:
-
https://medium.com/@tishkov.pavel/autotagging-in-gitlab-step-by-step-guide-c3a8dd47087a https://medium.com/@tishkov.pavel/autotagging-in-gitlab-step-by-step-guide-c3a8dd47087a- 让 git 在 ci 中工作。
-
https://williamhayes.medium.com/versioning-using-bumpversion-4d13c914e9b8 https://williamhayes.medium.com/versioning-using-bumpversion-4d13c914e9b8- 帮助我更好地理解bump2version
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)