Fail Gitlab pipeline based on output

Viewed 841

I am using a docker container (sslyze) in a Gitlab pipeline for some testing. This pipeline always succeeds, but I would like the pipeline to fail if the container ever reported a "FAIL" in its output. Currently if a "FAIL" is reported in the terminal output, an exit code of 0 is still reported (as the scan itself worked) so Gitlab passes the pipeline.

I am new to Gitlab, but familiar with Jenkins, and in Jenkins you could fail the job based on the terminal output using Text Finder. Is there a similar concept in Gitlab?

1 Answers

Thanks to @secustor in the comments for pointing me to a similar question. I was hoping for some native functionality within Gitlab but I couldn't find any.

Instead, I queried the container logs and the exit code, then set an exit code of 1 depending on the outcome.

Within in an .gitlab-ci.yml (I had problems splitting the logic across multiple lines so it's all jammed into one line):

  script:
  - docker run --name containername nablac0d3/sslyze --regular $URL
  - if [[ "$(docker logs containername >& container-logs ; cat container-logs | grep 'FAIL' | wc -l)" -gt 0 ]] || [[ "$(docker container wait containername)" -eq 1 ]]; then exit 1; fi
Related