Git CI/CD 变量,用于获取合并请求管道中落后的提交数

Git CI/CD variable to get the number of commits behind in merge request pipeline

如果源分支落后于目标分支的提交,则需要使管道失败

issue 15310 and GitLab 11.6(2018 年 12 月)起,您可以 运行 只有 merge_request.

管道

这允许您访问 $CI_MERGE_REQUEST_TARGET_BRANCH_SHA predefined variable, that you can then use in rules (like ones defined in the gitlab/ci/templates/Workflows/MergeRequest-Pipelines.gitlab-ci.yml
在您的情况下,一个特定于测试是否添加了新提交的规则:

test:
  stage: test
  script: ./test
  only:
    - merge_requests
  rules:    
    - if: $CI_COMMIT_TAG

但是,if 规则不允许执行 script git command you would need:

 git merge-base --is-ancestor <maybe-ancestor-commit> <descendant-commit>
 git merge-base --is-ancestor $CI_MERGE_REQUEST_TARGET_BRANCH_SHA CI_MERGE_REQUEST_SOURCE_BRANCH_SHA

如果该命令 returns 0 (true),则作业应该处理,并且目标分支 HEAD 是合并请求分支的祖先,这意味着后者只会直接在前(目标)分支。

所以我仍然会有一份工作 运行 只为 merge_requests,在 script: 指令中执行 git 命令,并调用你的实际脚本(您希望看到在 separate 阶段(由第一个看门人调用)中的合并请求执行的“以防源分支落后于目标分支的提交”),使用 needs: <first-stage>, as in here.