Nx 在 GitLab CI 中有时使用错误的提交引用。

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

Nx on gitlab ci sometimes uses wrong commit ref

问题

这是您要翻译的内容:

"我目前正在为一个主要运行良好的单一 nx 存储库(repo)操作 GitLab CI 流水线,但有时 linting 任务在使用正确的提交引用时会出现问题。

这是我正在运行的设置 linting 的脚本的当前部分:

- if [ "$CI_PIPELINE_SOURCE" == "merge_request_event" ]; then NX_BASE=${CI_MERGE_REQUEST_DIFF_BASE_SHA:-$CI_COMMIT_BEFORE_SHA}; else NX_BASE=HEAD~1; fi
- NX_HEAD=HEAD

接着是作业部分:

npx nx affected --base=$NX_BASE --head=$NX_HEAD --target=lint --parallel=3

当发生此情况时,以下是输出:

$ if [ "$CI_PIPELINE_SOURCE" == "merge_request_event" ]; then NX_BASE=${CI_MERGE_REQUEST_DIFF_BASE_SHA:-$CI_COMMIT_BEFORE_SHA}; else NX_BASE=HEAD~1; fi
$ NX_HEAD=HEAD
$ git show-ref
baf7d995796d6cda3e67e1c8dafe3952d875aa19 refs/pipelines/35035
$ npx nx affected --base=$NX_BASE --head=$NX_HEAD --target=lint --parallel=3
fatal: Not a valid commit name 9e27db2e2ae932d0cdcc7a4a04eddb84201ad045
fatal: No such ref: '9e27db2e2ae932d0cdcc7a4a04eddb84201ad045'

然后随之而来的是流水线失败,因为 nx 无法运行。

我期望它会一直表现相同。我尝试更改运行器和缓存,但无济于事。"

英文:

i am currently operating a gitlab ci pipeline for a mono nx repo which is mostly running fine but sometimes the linting job is having problems with useing the right commit ref for its base.

This is the current part of the script i am running to setup my linting:
- if [ "$CI_PIPELINE_SOURCE" == "merge_request_event" ]; then NX_BASE=${CI_MERGE_REQUEST_DIFF_BASE_SHA:-$CI_COMMIT_BEFORE_SHA}; else NX_BASE=HEAD~1; fi
- NX_HEAD=HEAD

Followed by the job:
npx nx affected --base=$NX_BASE --head=$NX_HEAD --target=lint --parallel=3

This is the output when this occures:

$ if [ "$CI_PIPELINE_SOURCE" == "merge_request_event" ]; then NX_BASE=${CI_MERGE_REQUEST_DIFF_BASE_SHA:-$CI_COMMIT_BEFORE_SHA}; else NX_BASE=HEAD~1; fi
$ NX_HEAD=HEAD
$ git show-ref
baf7d995796d6cda3e67e1c8dafe3952d875aa19 refs/pipelines/35035
$ npx nx affected --base=$NX_BASE --head=$NX_HEAD --target=lint --parallel=3
fatal: Not a valid commit name 9e27db2e2ae932d0cdcc7a4a04eddb84201ad045
fatal: No such ref: '9e27db2e2ae932d0cdcc7a4a04eddb84201ad045'

Which is then followed by the pipeline failing since nx can't run.

I expect that it will behave the same all the time. I tried to change runners and cache but to no avail.

答案1

得分: 0

nx affected显然需要访问引用以计算差异。当$CI_MERGE_REQUEST_DIFF_BASE_SHA指向比你的GIT_DEPTH设置的HEAD更远的引用时,可能会发生此错误。

默认情况下,GitLab使用深度为20。因此,如果您的合并请求中有超过20个提交,将会失败。

您需要确保您的git深度足够大,以使引用$CI_MERGE_REQUEST_DIFF_BASE_SHA在作业中的检出库中存在。您可以通过在作业中设置GIT_DEPTH变量来实现这一点:

job:
  variables:
    GIT_DEPTH: "50" # 或者您需要的任何值

或者,您可以在作业中调用git命令,以确保您需要的引用(或引用)存在。

英文:

nx affected will obviously need to access to the ref to be able to calculate a diff. This error can happen when $CI_MERGE_REQUEST_DIFF_BASE_SHA points to a reference that is farther behind HEAD than your GIT_DEPTH setting.

By default, GitLab uses a depth of 20. So if there's more than 20 commits in your MR, this will fail.

You need to make sure your git depth is large enough such that the reference $CI_MERGE_REQUEST_DIFF_BASE_SHA is present in the checked out repo in the job. You can do this by setting the GIT_DEPTH variable in your job:

job:
  variables:
    GIT_DEPTH: "50" # or whatever you need

Alternatively, you can invoke git commands in your job to ensure the ref (or refs) you need are present.

huangapple
  • 本文由 发表于 2023年3月31日 22:13:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899544.html
匿名

发表评论

匿名网友

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

确定