Grep always fails with CI/CD pipeline

I wanted to check our build if they contain SNAPSHOTS before building depending on environment. The following code works fine locally on my machine but it always fails in gitlab pipeline without any error, only exit code 123. To do this, I am first trying to find any place where SNAPSHOT is in my pom.xml files.

 -  snapshots=$(grep -nrH --include="pom.xml" "SNAPSHOT" )

Why is the code above failing with exit 123 in gitlab but works perfectly fine running locally on my machine?

No one who knows?

I stumbled across this, while looking for an answer, so I thought I’d give one, now that I found one myself.

Have you tried catching the exit code?

I recently had to troubleshoot a failing pipeline and the issue was that one of the commands had exit code 1, which terminated the pipeline run.

You could try this:

 -  snapshots=$(grep -nrH --include="pom.xml" "SNAPSHOT" ) || true

or this:

 -  exit_code=0; snapshots=$(grep -nrH --include="pom.xml" "SNAPSHOT" ) || exit_code=$?