Skip to content

CI_MERGE_REQUEST_SOURCE_BRANCH_SHA and CI_MERGE_REQUEST_TARGET_BRANCH_SHA not properly set

Summary

In CI jobs triggered by merge requests, despite what the documentation says, and despite recent additions and fixes, neither CI_MERGE_REQUEST_SOURCE_BRANCH_SHA nor CI_MERGE_REQUEST_TARGET_BRANCH_SHA environment variables seem to be properly set.

Steps to reproduce

  1. Create a new project on Gitlab.com

  2. Create a .gitlab-ci.yml file with the following content:

    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}
  3. Push it to master

  4. Create a merge request

  5. Inspect the MR log to see that CI_MERGE_REQUEST_SOURCE_BRANCH_SHA and CI_MERGE_REQUEST_TARGET_BRANCH_SHA are not set 😢

Example Project

Here is a project demonstrating the issue: https://gitlab.com/michaelbaudino/test-gitlab-ci

The issue can be observed in this particular pipeline job: https://gitlab.com/michaelbaudino/test-gitlab-ci/-/jobs/200177190

What is the current bug behavior?

  • CI_MERGE_REQUEST_SOURCE_BRANCH_SHA is empty
  • CI_MERGE_REQUEST_TARGET_BRANCH_SHA is empty

What is the expected correct behavior?

  • CI_MERGE_REQUEST_SOURCE_BRANCH_SHA is set to the HEAD hash of the source branch (e.g. feature/my-feature)
  • CI_MERGE_REQUEST_TARGET_BRANCH_SHA is set to the HEAD hash of the target branch (e.g. master)

Relevant logs and/or screenshots

The following .gitlab-ci.yml:

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}

… produces the following log (copy-pasted from https://gitlab.com/michaelbaudino/test-gitlab-ci/-/jobs/200177190):

Running with gitlab-runner 11.10.0-rc2 (10efa505)
  on docker-auto-scale fa6cab46
Using Docker executor with image node:8 ...
Pulling docker image node:8 ...
Using docker image sha256:0bf36d7ccc1ea6bdb6dfbaa2b4bf9af4b3d9acf089cb234dcc3424501ec4948a for node:8 ...
Running on runner-fa6cab46-project-11966081-concurrent-0 via runner-fa6cab46-srm-1555922012-e7b45f0c...
Initialized empty Git repository in /builds/michaelbaudino/test-gitlab-ci/.git/
Fetching changes with git depth set to 10...
Created fresh repository.
From https://gitlab.com/michaelbaudino/test-gitlab-ci
 * [new ref]         refs/merge-requests/1/head -> refs/merge-requests/1/head
Checking out c666b099 as refs/merge-requests/1/head...

Skipping Git submodules setup
$ echo CI_COMMIT_REF_NAME=${CI_COMMIT_REF_NAME}
CI_COMMIT_REF_NAME=refs/merge-requests/1/head
$ echo CI_MERGE_REQUEST_SOURCE_BRANCH_SHA=${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}
CI_MERGE_REQUEST_TARGET_BRANCH_SHA=
$ echo CI_MERGE_REQUEST_SOURCE_BRANCH_NAME=${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME}
CI_MERGE_REQUEST_SOURCE_BRANCH_NAME=add-readme
$ echo CI_MERGE_REQUEST_TARGET_BRANCH_NAME=${CI_MERGE_REQUEST_TARGET_BRANCH_NAME}
CI_MERGE_REQUEST_TARGET_BRANCH_NAME=master

Output of checks

This bug happens on GitLab.com (v11.10.0-ee at the time of writing).

Possible fixes

Not a possible fix, but the feature was introduced in 314062fe, so there might be something broken or missing in this commit ¯\_(ツ)_/¯