在GitLab合并请求中测试所有提交。

huangapple go评论69阅读模式
英文:

Testing all commits in a GitLab merge request

问题

有没有办法为分支顶部以外的提交创建额外的作业?

英文:

I have a project in GitLab, and would like to run a pre-merge CI test on in that verifies that every commit created by the merge request will compile and pass the testsuite, so that if I have to run git-bisect a year later, this will not fail because some intermediate commit does not even build.

Is there a way to create additional jobs for commits other than the branch tip?

答案1

得分: 2

运行一个预合并的CI测试,以验证合并请求创建的每个提交是否能够编译并通过测试套件。

这看起来… 非常耗费资源和时间!
如果您要合并的分支有许多提交,或者您的构建和测试过程很长,这可能需要相当长的时间。

GitLab CI/CD默认情况下是设计为在合并请求(MR)的最新提交上运行的。然而,如果您想要为MR中的每个单独提交运行流水线,目前没有直接支持此功能的选项。

然而,如果您坚持要在每个提交上运行流水线,您可能需要在GitLab CI配置中设置一个自定义脚本。

以下是一个示例:

test_all_commits:
  script: |
    #! /bin/bash
    # 首先获取所有分支
    git fetch --all

    # 获取要合并的分支的提交哈希
    branch_hash=$(git rev-parse $CI_COMMIT_REF_NAME)

    # 获取要合并到的目标分支的提交哈希
    target_branch_hash=$(git rev-parse $CI_MERGE_REQUEST_TARGET_BRANCH_NAME)

    # 找到两个分支的共同祖先
    common_ancestor=$(git merge-base $branch_hash $target_branch_hash)

    # 获取共同祖先与要合并的分支之间的所有提交列表
    commit_list=$(git rev-list $common_ancestor..$branch_hash)

    # 循环处理每个提交
    for commit in $commit_list; do
      # 检出提交
      git checkout $commit

      # 运行您的构建和测试命令
      make test

      # 如果构建或测试失败,以错误代码退出
      if [ $? -ne 0 ]; then
        exit 1
      fi
    done    
  only:
    - merge_requests

请将make test替换为您需要运行的项目构建和测试命令。

这将检出您想要合并的分支与您想要合并的分支的共同祖先之间的每个提交(仅在合并请求流水线中),然后运行您的构建和测试命令。如果任何提交无法构建或无法通过测试,脚本将以错误代码退出,导致流水线失败。

英文:

> run a pre-merge CI test on in that verifies that every commit created by the merge request will compile and pass the testsuite

That looks... extremely resource-intensive and time-consuming!
If the branch you want to merge has many commits, or if your build and test process is long, this could take a considerable amount of time.

GitLab CI/CD, by default, is designed to run on the latest commit of a merge request (MR). However, if you want to run the pipeline for every single commit in the MR, there is not an out-of-the-box feature that would allow you to do so directly.

However, if you insist on running the pipeline on every commit, you might need to set up a custom script in your GitLab CI configuration.

Here is an example of what that would look like:

test_all_commits:
  script: |
    #! /bin/bash
    # Start by fetching all the branches
    git fetch --all

    # Get the commit hash of the branch to merge
    branch_hash=$(git rev-parse $CI_COMMIT_REF_NAME)

    # Get the commit hash of the target branch to merge into
    target_branch_hash=$(git rev-parse $CI_MERGE_REQUEST_TARGET_BRANCH_NAME)

    # Find the common ancestor of the two branches
    common_ancestor=$(git merge-base $branch_hash $target_branch_hash)

    # Get the list of all commits between the common ancestor and the branch to merge
    commit_list=$(git rev-list $common_ancestor..$branch_hash)

    # Loop over each commit
    for commit in $commit_list; do
      # Check out the commit
      git checkout $commit

      # Run your build and test commands
      make test

      # If the build or test fails, exit with an error code
      if [ $? -ne 0 ]; then
        exit 1
      fi
    done    
  only:
    - merge_requests

Do replace make test with the commands you need to run to build and test your project.

That will checkout each commit between the branch you want to merge and its common ancestor with the branch you want to merge into (CI_MERGE_REQUEST_TARGET_BRANCH_NAME, but only in merge request pipeline, as mentioned here), and then run your build and test commands.
If any commit fails to build or pass the tests, the script will exit with an error code, causing the pipeline to fail.

huangapple
  • 本文由 发表于 2023年7月10日 16:09:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76651853.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定