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.
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.