How to read the Git lab console output to fail the job when the test fails

I want to read the gitlab console output to fail the job when the test fails. I am running SoapUI tests using maven. Please see the screenshot below. I am new to gitlab. I have been working on this issue for 2 weeks. Also it is becoming hard for me to click on each job to see if it has passed or failed. If I can read the console output, I can put an if condition in the yml file stating that it should fail the job if it see the text “failed”
gitlab

Hi,

can you share the .gitlab-ci.yml configuration content, and the script that gets executed to generate the output? That will help to suggest a solution likely fitting better than generic suggestions.

You’ll need to parse the output and then calculate the exit code based on what could be an error code. exit(0 = success, exit(1) = error can be used to differentiate between errors. Best with a small wrapper shell script.

  1. Use a regular expression to parse and extract the value for Total Failed Assertions (which I assume you want to track). https://regex101.com/ can help with building regex.
  2. Compare if the failed value is greater than 0
  3. If yes, call exit(1) to error out
  4. If no, call exit(0) to provide ok status

I used bash parse multi line output regex extract number to search how to build such a script. shell script - How to search and extract string from command output? - Unix & Linux Stack Exchange seemed reasonably easy.

echo "Total Failed Assertions: 7" | sed -n 's/.*Total Failed Assertions: //p'                                                                 
7

Below script works, you’ll need to modify the # collects multiline output line to call the command you are using in the script section in the CI/CD config. # Only for testing, remove me and the block underneath need to be removed. That’s only for quick testing to see whether the regex later works in the sed call.

#!/bin/bash

# collects multiline output - edit me
OUT=`generate-test-output-command-called`

# Only for testing, remove me 
OUT="
Total Everyone Can Contribute: 100
Total Failed Assertions: 7
"

# that prints 7 for example
FAILED=`echo $OUT | sed -n 's/.*Total Failed Assertions: //p'`

#echo $FAILED

# create error
if [ "$FAILED" -gt 0 ]; then
  echo "Tests failed with assertions: $FAILED"
  exit 1
fi

# success
exit 0

Script is far from perfect - if you feel familiar with a different language to parse shell output, go for it. I wanted to keep it simple, but am far from a shell scripting expert.

Cheers,
Michael