Travis file cannot be parsed but command works in debug mode

My travis file could not be parsed and I think it is this line causing the issue. However, when I tried entering debug mode and running the command it worked.

Does anyone know what is causing the error?
Thanks in advance, any help would be much appreciated.

  # for all changed folders (docker containers) in folders.txt pull containers
  - [ ! -f folders.txt ] || while read folder; do
    echo $folder;
    docker pull lifebitai/${folder};
    done <folders.txt

Here is the full .travis.yml file.

sudo: required
language: python
jdk: openjdk8
services: docker
python: "3.6"
cache: pip
matrix:
  fast_finish: true

script:
  # get list of changed files
  - CHANGED_FILES=($(git diff --name-only $TRAVIS_COMMIT_RANGE))
  # save names of any changed folders (docker containers) to file folders.txt
  - echo $CHANGED_FILES
  - for file in $CHANGED_FILES; do
    echo $file;
    dir=$(dirname $file);
    echo $dir;
    [[ "${dir}" != "." ]] && dirname $file >> folders.txt;
    cat folders.txt;
    done
  # for all changed folders (docker containers) in folders.txt pull containers
  - [ ! -f folders.txt ] || while read folder; do
    echo $folder;
    docker pull lifebitai/${folder};
    done <folders.txt

This is invalid YAML.

In YAML, | introduces a block scalar, so you can’t use it in a bare string.

   - [ ! -f folders.txt ] || while read folder; do
    echo $folder;
    docker pull lifebitai/${folder};
    done <folders.txt

You’ll have to fix that. Probably the easiest thing to do is to wrap everything in double quotes.