Gitlab-ci: reading file line by line

I’d like to process a file line by line

Hi all,
I’d like to process a file line by line using the code below.
The lines are read correctly. However, the reading does not stop when all lines are done. Instead, the while loop runs endlessly.
Do you have any ideas on that?

Thanks!

    - while read line
    - do
    #-   echo $line
    - done < InsertData.sql

Hi,

I would recommend moving the code portions into a dedicated file, e.g. called process-files.sh, make it executable, and call it from the CI/CD config. Within the YAML configuration and inline bash scripts, there is no guarantee for loop exit conditions AFAIK.

One way to hide them from the default repo view is to move them into the .gitlab directory where other specific things are stored, like description templates for MRs and issues, etc.

$ mkdir -p .gitlab/ci/scripts
$ vim .gitlab/ci/scripts/process-files.sh

#!/bin/bash

FILE=InsertData.sql

while read line
do
   echo $line
done < $FILE

# do more, calculate the exit state, etc.
STATE=0

exit($STATE)
chmod +x .gitlab/ci/scripts/process-files.sh
git add .gitlab/ci/scripts/process-files.sh
git commit -v -m "Add profess-files script for CI"

.gitlab-ci.yaml

variables:
  CI_SCRIPTS_PATH: .gitlab/ci/scripts

read_files:
  script:
     - $CI_SCRIPTS_PATH/process-files.sh

Cheers,
Michael

1 Like

Hi Michael,
Thanks a lot for the comprehensive answer!
Such a shell script works fine, of course.

1 Like