Himanshu Singh
Himanshu Singh

Reputation: 357

how to read values from a file inside gitlab-ci yml

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 ?

Upvotes: 0

Views: 14136

Answers (2)

VonC
VonC

Reputation: 1323203

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)

Upvotes: 2

Himanshu Singh
Himanshu Singh

Reputation: 357

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.

Upvotes: 0

Related Questions