Multiline Bash syntax in .gitlab-ci.yml

I have a CI job that looks like this:

job:
    ...
    variables:
        NUMBER_TO_KEEP: 3
    script:
        - >
            dirs=$(find . -mindepth 1 -maxdepth 1 -type d  | sort -rV)
            count=0
            for dir in $dirs
            do
                count=$((count + 1))
                if [[ $NUMBER_TO_KEEP -ge $count ]]; then
                    echo "KEEPING $dir"
                else
                    echo "REMOVING $dir"
                    rm -r "${dir}"
                fi
            done

and produces an error message bash: for: command not found. In the past I’ve avoided this problem by collapsing if and for statements onto one line, or factoring them out into other files. In this particular case, I would prefer to keep everything in gitlab-ci.yml. Is there a neat way to use complex BASH statements like this?

Thanks!

Sarah

1 Like

To answer my own question, - > should be - |-. This SO answer contains more details.

3 Likes