Soft-failing the build in GitLab CI on ESLint warnings

Everybody has a love/hate relationship with ESLint. It guides you in the right ways of code, but it also nags you with all of those mundane asks...

So, sometimes you switch things to “warn”, to be generally appraised of what's happening with a Scout's Honour promise to “fix it later”. Pinky swear. And then the whole thing gets wired up to CI and you never really see those warnings ever again. Once you see that green checkmark of a passing stage – why bother going in and reading console output, right?

Well, we can make GitLab soft-fail a stage. It will not break the build, but it'll show that nagging Yellow Exclamation Mark of Concern.

Sadly there's no command-line option for eslint to say: “exit with a non-successful status code in case there are only warnings”, so we have to improvise.

jq to the rescue (once we manage to crawl though its obtuse syntax).

lint:report_warnings:
  stage: lint
  allow_failure: true
  script:
    - apk add --update --no-cache jq
    - eslint . -f json | jq 'map(.warningCount) | add | if(.>0) then halt_error(5) else halt_error(0) end'

This runs eslint, producing a JSON output where we sum all the warning counts and bail out with an error code if there are any.

Technically we can set the allow_fail to permit only specific status codes.

allow_failure:
  status_codes:
    - 5

I couldn't get it to work naively though. But this works too. You have a stage that soft-fails on warnings.

#gitlab #ci #eslint