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.
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.