Fail if a word or phrase is in a job's log

I would like to be able to configure a Gitlab CI job so that it fails if certain keywords are in that job’s log.

I’m wondering if this is an existing feature that I just can’t find, or if it is not a feature of Gitlab CI.

My use case is that bash scripts can’t sometimes have command not found errors without making the pipeline fail, because the error happens inside an if condition/test. Thus, I want to the pipeline to fail if the job log has any command not found messages.

This feature would have much broader usage as well, you could configure it to recognize error messages from your program or testing frameworks.

Each line of the script section of your .gitlab-ci.yml will be executed by Bash, so you can just use grep to check the output. For example:

job:
    ...
    script:
        - run_tests.sh | tee test_output.txt
        - grep -v "command not found" test_output.txt
1 Like

Hi, I don’t understand how this solves my problem. Grep fails if it doesn’t find a match, and you’ve specified -v, so it is looking for stuff without “command not found”. However, my log will always have other lines, so grep will always find things and never error? Sorry if I didn’t make it clear that the log has other stuff in it.

I ended up just running another script that had something along the lines of

if grep file phrase:
    echo stuff
    exit 1
else:
    exit 0

Hi there, yes sorry, that -v was not quite right.

You could make this a little shorter, like this:

job:
    script:
        - run_tests.sh | tee test_output.txt
        - ! grep "command not found" test_output.txt
1 Like

@snim2 Thanks for helping, much appreciated :heart:

1 Like