Jor-El
Jor-El

Reputation: 437

gitlab pipeline failing at grep, how to fix that?

I have gitlab pipeline, where at the end of execution, there is a text file generated. In that, I am trying to search for strings with Error and want to show the job as failed in the pipeline.

So. I added below part of code at the end.

.check_remote_log: &check_remote_log

- ls -la

- returnCode=$(grep -c "Error:" outfile)

- echo "Return code received $returnCode"

- if [ $returnCode -ge 1 ]; then exit 1; fi

And calling this at the end of steps as

What I observed in the output is, the file is getting generated as I can see in the ls command. But it is failing at the grep step.

error screen

But these commands are working when I ran individually on a linux machine. Please suggest.

Even I tried below command but giving a different error.

if [[ $(grep "Error" outfile) ]] ; then exit 1; else echo "No errors in outfile"; fi

error

sh: 0/1: unknown operand

When there is error also in the output, getting as below.

enter image description here

Upvotes: 7

Views: 5940

Answers (2)

t7e
t7e

Reputation: 509

Use awk instead. It does not give you the "exit 1" status code in the pipeline and can work as a grep replacement.

Upvotes: 0

KamilCuk
KamilCuk

Reputation: 141393

Job failed: exit code 1

From man grep:

Exit Status

Normally, the exit status is 0 if selected lines are found and 1 otherwise. [...]

If you want to ignore an exit status of a command, it's typical to add || : or || true. || : does not work nicely in YAML files.

- output=$(grep -c "Error:" outfile) || true

If you want to store the exit status, put it in one line, so that gitlab doesn't detect it. Read documentation https://docs.gitlab.com/ee/ci/yaml/script.html#ignore-non-zero-exit-codes .

- output=$(grep -c "Error:" outfile); returncode=$?
- returncode=0 ; output=$(grep -c "Error:" outfile) || returncode=$?

If you want check if outfile has error, put it in an if.

- if grep -q "Error:" file; then echo has error; exit 1; else echo has no error; fi
# or multiline yaml
- |
  if grep -q "Error:" file; then
     echo has error;
     exit 1;
  else
     echo has no error;
  fi
# or depending on gitlab checking exit status
# but does not handle when file does not exists, or read error, etc.
- '!' grep -q "Error:" file

If you want to check if a file does not have a string, you would have to explicitly check for exit status equal to 1. I would do:

- grep -HnC 3 "Error:" file ; [ "$?" -eq 1 ]

Upvotes: 10

Related Questions