3

Trying to retrieve latest commit in main branch before the new head.

But using CI_COMMIT_BEFORE_SHA its returning always zero's. In case, if I run pipeline from the GitLab UI.

It working fine if its trigged automatically in case of a push to main branch.

Pipline Custome Variables using Predefined Gitlab CI variables:--

  • NX_HEAD=$CI_COMMIT_SHA
  • NX_BASE=${CI_MERGE_REQUEST_DIFF_BASE_SHA:-$CI_COMMIT_BEFORE_SHA}

enter image description here

user1264429
  • 179
  • 2
  • 11
  • Looks like a bug in whatever tool you are using. All-zeros mean "this ref does not exist yet / any-more and should not be used". – torek May 26 '22 at 10:03
  • When I create a new pipeline through the api, it also stays at 0. – Rakuyo Dec 09 '22 at 08:08
  • What are you expecting the value to be for pipelines that run from anything other than a push? What is "before the new head" in these cases? – rink.attendant.6 Aug 17 '23 at 17:40

2 Answers2

0

Check if this is related to issue 12850 "CI_COMMIT_BEFORE_SHA is all zero in a merge request pipeline".
Or issue 28252 "Expose CI_MERGE_REQUEST_SOURCE_BRANCH_SHA and CI_MERGE_REQUEST_TARGET_BRANCH_SHA in detached merge request pipelines"

Try a simple test to see if that would work better with your current version of GitLab:

image: node:8

test-ci-vars:
  only:
    - master
    - merge_requests
  script:
    - echo CI_COMMIT_REF_NAME=${CI_COMMIT_REF_NAME}
    - echo CI_MERGE_REQUEST_SOURCE_BRANCH_SHA=${CI_MERGE_REQUEST_SOURCE_BRANCH_SHA}
    - echo CI_MERGE_REQUEST_TARGET_BRANCH_SHA=${CI_MERGE_REQUEST_TARGET_BRANCH_SHA}
    - echo CI_MERGE_REQUEST_SOURCE_BRANCH_NAME=${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME}
    - echo CI_MERGE_REQUEST_TARGET_BRANCH_NAME=${CI_MERGE_REQUEST_TARGET_BRANCH_NAME}

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • In Case of merge request CI_COMMIT_BEFORE_SHA expected to be zero also mentioned in the documentation but I'm trying to access when running pipeline from gitlab ui against main branch. – user1264429 May 26 '22 at 10:26
  • @user1264429 So, not a new branch, as described in [this answer](https://stackoverflow.com/a/71601191/6309)? – VonC May 26 '22 at 14:45
0

I am guessing your pipeline is inspired by Configuring CI Using GitLab and Nx.

If so, you might have set GIT_DEPTH to 0 which according to the docs should disable the shallow cloning. Unfortunately, it looks like this doesn't work as expected, see issues 292470.

Therefore, maybe you'd like to change

variables:
  GIT_DEPTH: 0

to

variables:
  GIT_DEPTH: 100

so that the last 100 commits (instead of the default) are available, which then hopefully will result in $CI_COMMIT_BEFORE_SHA being defined in a non-MR pipeline.

This blog might also be interesting.

Philippe
  • 861
  • 10
  • 10