python-semantic-release无法识别GitLab,误连GitHub致版本升级失败
问题
我尝试配置仓库,通过提交记录自动触发版本号升级,但python-semantic-release无法识别当前处于GitLab环境,反而错误地和GitHub交互。
运行日志
semantic-release -vv publish [05:17:10] DEBUG [semantic_release.cli.commands.main] DEBUG main.py:105 main.main: logging level set to: DEBUG DEBUG [semantic_release.cli.commands.main] DEBUG main.py:123 main.main: global cli options: GlobalCommandLineOptions(noop=False, verbosity=2, config_file='pyproject.toml', strict=False) INFO [semantic_release.cli.util] INFO util.py:78 util.load_raw_config_file: Loading configuration from pyproject.toml DEBUG [semantic_release.cli.util] DEBUG util.py:81 util.load_raw_config_file: Trying to parse configuration pyproject.toml in TOML format INFO [semantic_release.cli.config] INFO config.py:261 config.select_branch_options: Using group 'develop' options, as 'develop' matches 'develop' WARNING [semantic_release.cli.config] WARNING config.py:380 config.from_raw_config: the token for the remote VCS is configured as stored in the GH_TOKEN environment variable, but it is empty DEBUG [semantic_release.changelog.template] DEBUG template.py:55 template.environment: {'template_dir': 'templates', 'block_start_string': '{%', 'block_end_string': '%}', 'variable_start_string': '{{', 'variable_end_string': '}}', 'comment_start_string': '{#', 'comment_end_string': '#}', 'line_statement_prefix': None, 'line_comment_prefix': None, 'trim_blocks': False, 'lstrip_blocks': False, 'newline_sequence': '\n', 'keep_trailing_newline': False, 'extensions': (), 'autoescape': True, 'autoescape_value': True} DEBUG [semantic_release.version.translator] DEBUG translator.py:40 translator._invert_tag_format_to_re: inverted tag_format 'v{version}' to 'v(?P<version>.*)' DEBUG [semantic_release.version.version] DEBUG version.py:121 version.parse: attempting to parse string '1.4.0' as Version DEBUG [semantic_release.version.version] DEBUG version.py:145 version.parse: version string 1.4.0 parsed as a non-prerelease DEBUG [semantic_release.version.version] DEBUG version.py:148 version.parse: parsed build metadata '' from version string 1.4.0 INFO [semantic_release.version.algorithm] INFO algorithm.py:60 algorithm.tags_and_versions: found 1 previous tags INFO [semantic_release.cli.commands.publish] INFO publish.py:48 publish.publish: Uploading distributions to release DEBUG [semantic_release.hvcs.github] DEBUG helpers.py:45 helpers._wrapper: upload_dists(<semantic_release.hvcs.github.Git hub object at 0x7efc0b7b1390>, tag='v1.4.0', dist_glob='dist/*') DEBUG [semantic_release.hvcs.github] DEBUG helpers.py:45 helpers._wrapper: get_release_id_by_tag(<semantic_release.hvcs.g ithub.Github object at 0x7efc0b7b1390>, tag='v1.4.0') DEBUG [semantic_release.helpers] DEBUG helpers.py:112 helpers.parse_git_url: Parsing git url 'https://gitlab-ci-token:***@gitlab.com/test/custom/projects/app/backend.git' DEBUG [urllib3.connectionpool] DEBUG connectionpool.py:1055 connectionpool._new_conn: Starting new HTTPS connection (1): api.github.com:443 DEBUG [urllib3.connectionpool] DEBUG connectionpool.py:549 connectionpool._make_request: https://api.github.com:443 "GET /repos/test/custom/projects/app/backend/releases/tags/v1.4.0 HTTP/1.1" 404 91 DEBUG [semantic_release.hvcs.github] DEBUG helpers.py:56 helpers._wrapper: Github.get_release_id_by_tag -> None WARNING [semantic_release.hvcs.github] WARNING github.py:252 github.upload_dists: No release corresponds to tag v1.4.0, can't upload dists DEBUG [semantic_release.hvcs.github] DEBUG helpers.py:56 helpers._wrapper: Github.upload_dists -> 0 Cleaning up project directory and file based variables
配置文件(pyproject.toml)
[tool.semantic_release] version_variable = "lambda/__init__.py:__version__" # --> version = "1.4.0" [tool.semantic_release.branches.develop] match = "develop" prerelease = true
从日志能看到工具在调用GitHub的API(api.github.com),但仓库明明是GitLab的。工具本该自动识别环境,但找不到强制指定VCS的方法,也不清楚是哪个环境变量导致识别错误。
解决方法
- 强制指定VCS类型:直接在pyproject.toml中添加配置,明确告诉工具使用GitLab:
[tool.semantic_release] version_variable = "lambda/__init__.py:__version__" hvcs = "gitlab" # 新增此行指定VCS类型 [tool.semantic_release.branches.develop] match = "develop" prerelease = true
配置GitLab令牌:
- 替换原有的
GH_TOKEN环境变量,设置GITLAB_TOKEN,这是工具访问GitLab API必需的令牌。 - 如果在GitLab CI中运行,可直接使用预定义的
CI_JOB_TOKEN作为GITLAB_TOKEN,或者创建项目级的访问令牌。
- 替换原有的
验证远程仓库配置:确认本地仓库的远程地址指向GitLab:
git remote -v
确保origin对应的是GitLab仓库URL。
- 升级工具版本:旧版本的python-semantic-release可能存在环境识别bug,尝试升级到最新版:
pip install --upgrade python-semantic-release
内容的提问来源于stack exchange,提问作者Mido
相关产品推荐
相关产品推荐

