0

I've tried using only: [merge_request] as per the documentation to get CI_MERGE_REQUEST_SOURCE_BRANCH_SHA and CI_MERGE_REQUEST_TARGET_BRANCH_SHA. Unfortunately declare -x shows that these variables are set but empty (.gitlab-ci.yml file). As far as I can tell, none of the CI_ variables give this crucial information to work out the commit hash of the target branch.

I've also tried with except: [master], with similar results (.gitlab-ci.yml file).

I've already looked at a similar Q&A, so I wonder if this might be a regression.

As you can also see from the job output there are no branches, so I can't even use something like master..HEAD.

l0b0
  • 55,365
  • 30
  • 138
  • 223
  • 1
    Can you post you complete `.gitlab-ci.yml`? Is the pipeline running in detached mode for the merge request? – KiwiKilian Nov 22 '20 at 13:58
  • @KiwiKilian I've linked to both versions of the file. As you can see from the output, the first CI run is detached (`CI_MERGE_REQUEST_EVENT_TYPE="detached"`). – l0b0 Nov 22 '20 at 17:35

1 Answers1

1

It might actually be a bug. I was very confident to have an example using the SHA variables in our codebase, but it suffers the same problem. A workaround I found for your job:

lint commit messages:
  stage: test
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
  script:
    - git fetch
    - poetry run gitlint --commits "origin/${CI_MERGE_REQUEST_TARGET_BRANCH_NAME}..origin/${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME}"

I switched to the rules syntax, GitLab recommends doing so:

The rules syntax is an improved, more powerful solution for defining when jobs should run or not. Consider using rules instead of only/except to get the most out of your pipelines.

Results in:

$ poetry install
Installing dependencies from lock file
No dependencies to install or update
$ git fetch
From https://gitlab.com/KiwiKilian/pacman-package-file
 * [new branch]      lint       -> origin/lint
 * [new branch]      master     -> origin/master
$ poetry run gitlint --commits "origin/${CI_MERGE_REQUEST_TARGET_BRANCH_NAME}..origin/${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME}"
Commit 7c9f4e7c1a:
1: CT1 Title does not start with one of fix, feat, chore, docs, style, refactor, perf, test, revert, ci, build: "Ignore IDEA configuration"
1: CT1 Title does not follow ConventionalCommits.org format 'type(optional-scope): description': "Ignore IDEA configuration"
Cleaning up file based variables
00:00
ERROR: Job failed: exit code 1
KiwiKilian
  • 981
  • 1
  • 7
  • 10