1

Trying to read values from a file inside gitlab-ci.yml file:

#!/usr/bin/bash
- IN_FILE="base_dir/apps_namespaces.txt"
- echo "IN_FILE = " > $IN_FILE 
- while IFS= read -r line || [ -n "$line" ]; do 
-   echo "Name read from file - " > $line
- done < $IN_FILE

But this script fails with error - "/usr/bin/bash: line 131: read: line: invalid number"

Whereas same logic works in a plain script file, having following code:

#!/bin/bash
while IFS= read -r line || [[ -n "$line" ]]; 
do
    echo "Text read from file: $line"
done < "$1"

when ran this script using command : ./scrpt apps_namespaces.txt

it rans and print each line.

What can be done if needs a long list to be read inside a gitlab-ci yml ?

Himanshu Singh
  • 199
  • 3
  • 15

2 Answers2

2

Check if you can emulate this gitlab-ci.yml loop example, which:

  • does not include the shebang (#!/bin/bash)
  • does not have a - in front of each line
  script:
    [...]
    - while read line; do
         echo $line;
         ./sireg-linux exec --loader-sitemap-sitemap \"$line\" >> ./output/${line##*/}_out.txt;
      done < sitemap-index

Of course, the alternative is to put the logic in a script, called from the pipeline, as documented in gitlab-org/gitlab-runner issue 2672

This is basic Unix, you just need your script to be executable in your git repo

chmod a+x scripts/test.sh
git add scripts/test.sh
git commit "fixing permissions"
git push

don't put /usr/bin/env for sh nor bash: both are 100% guaranteed to be present as #! /bin/sh or /bin/bash if bash is installed, or at least a much guaranteed to be there than /usr/bin/env to exists (and using env sh is not very good security)

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

A better approach which I followed:

put the logic to read file contents inside a script file. And refer this script inside gitlab-ci.yml worked.

It is cleaner and manageable.

Himanshu Singh
  • 199
  • 3
  • 15
  • I tried inline scripting as you suggested by removing "-" and removed shebang (#!/bin/bash), but seems some issue with gitlab-ci. It keep on giving same error. The same logic when kept in separate file and run as script works. That's why I used that as script inside gitlab-ci yml. – Himanshu Singh Mar 10 '21 at 22:55