如何在GitLab CI社区版中获取目标分支哈希



我已经根据文档尝试使用only: [merge_request]来获得CI_MERGE_REQUEST_SOURCE_BRANCH_SHACI_MERGE_REQUEST_TARGET_BRANCH_SHA。不幸的是,declare -x显示这些变量已设置,但为空(.gitlab-ci.yml文件(。据我所知,CI_变量中没有一个给出计算目标分支的提交哈希的关键信息。

我也尝试过except: [master],得到了类似的结果(.gitlab-ci.yml文件(。

我已经看过类似的问答;A、 所以我想知道这是否是一种回归。

您也可以从作业输出中看到,没有分支,所以我甚至不能使用master..HEAD之类的东西。

它实际上可能是一个bug。我很有信心在我们的代码库中有一个使用SHA变量的例子,但它也遇到了同样的问题。我为你的工作找到了一个变通方法:

lint commit messages:
stage: test
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
script:
- git fetch
- poetry run gitlint --commits "origin/${CI_MERGE_REQUEST_TARGET_BRANCH_NAME}..origin/${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME}"

我切换到rules语法,GitLab建议这样做:

规则语法是一种改进的、更强大的解决方案,用于定义作业何时运行。考虑使用规则,而不是仅使用/except来充分利用您的管道。

结果:

$ poetry install
Installing dependencies from lock file
No dependencies to install or update
$ git fetch
From https://gitlab.com/KiwiKilian/pacman-package-file
* [new branch]      lint       -> origin/lint
* [new branch]      master     -> origin/master
$ poetry run gitlint --commits "origin/${CI_MERGE_REQUEST_TARGET_BRANCH_NAME}..origin/${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME}"
Commit 7c9f4e7c1a:
1: CT1 Title does not start with one of fix, feat, chore, docs, style, refactor, perf, test, revert, ci, build: "Ignore IDEA configuration"
1: CT1 Title does not follow ConventionalCommits.org format 'type(optional-scope): description': "Ignore IDEA configuration"
Cleaning up file based variables
00:00
ERROR: Job failed: exit code 1

最新更新