3

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

  • echo "Now checking log file for returnCode"
  • *check_remote_log

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

Jor-El
  • 187
  • 3
  • 11

1 Answers1

7

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 ]
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • I want to check whether the outfile has any string with Error keyword and if it has then the pipeline should show as failure, if no Error string in the file then it should show as success. Can you suggest a string for that. The runner I am using is alpine, where only sh is there no bash. – Jor-El May 28 '22 at 08:42
  • Yes, and my answer presents 3 suggestions for that. I do not understand. – KamilCuk May 28 '22 at 08:44
  • this one I already tried and getting errors as I shown above. if [[ $(grep "Error" outfile) ]] ; then exit 1; else echo "No errors in outfile"; fi which is similar to your second option – Jor-El May 28 '22 at 09:09
  • grep -HnC 3 "Error:" file ; [ "$?" -eq 1 ] --- is not giving any result – Jor-El May 28 '22 at 09:11
  • i want to return as failure and terminate the pipeline stage/job when there is error keyword, or it should show as success. Please share that. And the image only uses sh, not bash – Jor-El May 28 '22 at 09:14
  • It's not `if [[ $(..` it's `if grep ..`. `grep -HnC 3 "Error:" file ; [ "$?" -eq 1 ] --- is not giving any result ` that means that there are no `Error:` in the file. If there would be `grep` would have outputted them. – KamilCuk May 28 '22 at 09:26
  • Look at what you made me do: https://gitlab.com/Kamcuk/tmp/gitlab-pipeline-failing-at-grep-how-to-fix-that/-/pipelines/550245698 . There were minor typos, but nothing that couldn't be fixed. – KamilCuk May 28 '22 at 09:37