Credo diff on Gitlab CI

Hi! :slight_smile:
So I saw that Credo now has support for checking only towards the git diff. credo/CHANGELOG.md at master · rrrene/credo · GitHub. This is very exciting since I have a large codebase and can now in theory add this to our pipeline for new merge requests.

I can get it working perfectly locally with as the documentation suggests mix credo diff --from-git-merge-base master but when I try to to add this to my gitlab ci pipeline

credo:
  stage: test
  needs: []
  except:
    - master
    - next
  script:
    - mix deps.get
    - mix credo diff --from-git-merge-base master --strict

I get the error:

$ mix credo diff --from-git-merge-base master --strict
** (diff) given value is not a Git ref: master

I’ve tried many different combinations, even experimented with changing so that Gitlab clones project instead of fetching in case it was a problem of not having the branches locally.

  variables:
    GIT_STRATEGY: clone

But so far no luck.
Any suggestions of what might be wrong?

1 Like

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.

6 Likes