Linux command is not working in CI

The following command works perfectly on the terminal but the same command fails in GitLab ci.

echo Hello >> foo.txt; cat foo.txt | grep "test"; [[ $? -eq 0 ]] && echo fail || echo success

return is success but the same command in gitlab ci

$ echo Hello >> foo.txt; cat foo.txt | grep "test"; [[ $? -eq 0 ]] && echo fail || echo success
Cleaning up file based variables
ERROR: Job failed: command terminated with exit code 1

is simply failing. I have no idea why.

Hi @isaergin

It’s hard to say, but splitting this up, I suspect what’s happening is that writing to foo.txt is fine, but the runner notices that grep has failed and immediately bails.

Personally I would re-write this:

as this:

test:
  stage: test
  script:
    - echo Hello >> foo.txt
    - if [[ $(grep -v "test" foo.txt) ]] ; then echo "fail"; else echo "success"; fi || true

which does work.

This is a little longer, but avoids some piping by using grep -v.

Presumably, you have a lint or testing framework and you want to see whether it has failed. but not have a whole pipeline job fail as a result? If so, you might prefer to do something like this:


test:
  stage: test
  script:
    - echo Hello >> foo.txt
    - if [[ $(grep -v "test" foo.txt) ]] ; then echo "TEST FAILED"; else echo "success"; fi
  allow_failure: true

which uses the allow_failure key, or you could do something similar with rules.

This means that when you look back at the pipeline you will see the failure recorded in the UI, but the overall pipeline will still be able to succeed.

HTH,

Sarah

Hey Sarah,
thanks for your response. That’s true, CI is executing each command separately, and if any command returns a non-zero value then CI is failing immediately. Yes, I did the same as you all said, allowing failure.