After a bit more digging I found the problem.
It turns out that you do need to fetch the branch…
I also got sidetracked because it appears that there is some predefined variables in Gitlab that are only available under certain conditions.
For example CI_MERGE_REQUEST_TARGET_BRANCH_NAME
and CI_MERGE_REQUEST_TARGET_BRANCH_SHA
are not available simply because you have a merge request, you must also set the only: merge_requests
option.
TLDR
But in the end I found a setup that I’m satisfied with:
credo:
stage: test
needs: []
except:
- master
- next
script:
- mix deps.get
- git fetch origin ${CI_DEFAULT_BRANCH}
- TARGET_SHA1=$(git show-ref -s ${CI_DEFAULT_BRANCH})
- echo "$TARGET_SHA1"
- mix credo diff --from-git-merge-base $TARGET_SHA1 --strict
Since we are quick at merging and in practice really only target our default branch (next) anyways.