AndyP
AndyP

Reputation: 607

How to fail if previous command found invalid files in gitlab script?

I have a gitlab.yml script which I am running as below:

script:
  - ls -lrth
  - find Data/ -name "*.json" -print0 | while IFS= read -d '' -r filename; do
        if ! jq . "$filename" >/dev/null 2>&1; then
            echo "$filename bad";
        fi
    done
  - # how can I check if above find command found invalid json files and if it did then fail and doesn't move to next command
  - # some other unix commands here

My find command finds all the json files which are invalid. Now I want to fail my gitlab script if my find command finds invalid json files. Meaning it should print all invalid json files (which it is already doing) and then it should not go to the next command to execute.

Is this possible to do?

Upvotes: 0

Views: 39

Answers (1)

KamilCuk
KamilCuk

Reputation: 141473

Try:

  - find Data/ -name "*.json" -print0 | {
        status=0;
        while IFS= read -d '' -r filename; do
            if ! jq . "$filename" >/dev/null 2>&1; then
               echo "$filename bad";
               status=1;
            fi;
        done;
        exit "$status";
    }

Upvotes: 1

Related Questions