How to Force Gitlab Pipeline to Fail on Condition


if you have a conditional in your Gitlab pipeline stage that checks if something has succeeded or failed, then you can use this to pass or fail your pipeline stage.

Option 1 – Fail on String Found

if cat log.txt | grep "Failure Summary" ; then exit 1 ; else exit 0 ; fi

This will fail the pipeline if it find a string matching Failure Summary in a file called log.txt in the current stage.

Option 2 – Fail on Code Found

Alternatively if you have a file called response.json that contains 200 or 401 codes, then you can fail the pipeline if a 401 is found:

if cat response.json | grep 401 ; then exit 1; else exit 0 ; fi