Home > Software engineering >  How to get list of the files in a folder in gitlab-ci
How to get list of the files in a folder in gitlab-ci

Time:12-07

I am trying to create a pipeline in gitlab-ci. my problem is that when I run ls command I can see my files, but when I use for condition, I cannot find same files. any suggestion?

  script:
    - set -vux
    - ls ./dist/*
    - |
      for file in ./dist/*; do 
          if [ -f  ${file} ]; then 
              export ARTIFACTORY_IMAGE_TAG=${DATASCIENCE_REPO}/$(basename ${file})/$IMAGE_VERSION
              'curl -H "X-JFrog-Art-Api: $ARTIFACTORY_PW" -XPUT "$ARTIFACTORY_IMAGE_TAG" -T dist/$file'
          fi 
      done
  dependencies:
    - package-build

my output is like this:

$ ls ./dist/*
ls ./dist/*
   ls ./dist/__init__.py ./dist/my_proj-0.0.1.1164649-py3-none-any.whl ./dist/file1.py ./dist/file2.py ./dist/file3.json ./dist/file4.json
./dist/__init__.py
./dist/my_proj-0.0.1.1164649-py3-none-any.whl
./dist/file1.py
./dist/file2.py
./dist/file3.json
./dist/file4.json
echo $'\x1b[32;1m$ for file in ./dist/*; do  # collapsed multi-line command\x1b[0;m'
   echo '$ for file in ./dist/*; do  # collapsed multi-line command'
$ for file in ./dist/*; do  # collapsed multi-line command
for file in ./dist/*; do 
    if [ -f  ${file} ]; then 
        export ARTIFACTORY_IMAGE_TAG=${MY_REPO}/$(basename ${file})/$IMAGE_VERSION
        'curl -H "X-JFrog-Art-Api: $ARTIFACTORY_PW" -XPUT "$ARTIFACTORY_IMAGE_TAG" -T dist/$file'
    fi 
done
   for file in ./dist/*
   '[' -f ./dist/__init__.py ']'
    basename ./dist/__init__.py
   export ARTIFACTORY_IMAGE_TAG=https://artifactory.xyz.com/artifactory/my_proj_dev/__init__.py/0.0.1.1164649
   ARTIFACTORY_IMAGE_TAG=https://artifactory.xyz.com/artifactory/my_proj_dev/__init__.py/0.0.1.1164649
   'curl -H "X-JFrog-Art-Api: $ARTIFACTORY_PW" -XPUT "$ARTIFACTORY_IMAGE_TAG" -T dist/$file'
/scripts-11718-6374270/step_script: line 239: curl -H "X-JFrog-Art-Api: $ARTIFACTORY_PW" -XPUT "$ARTIFACTORY_IMAGE_TAG" -T dist/$file: No such file or directory
Cleaning up project directory and file based variables
00:00
ERROR: Job failed: command terminated with exit code 1

Update 1: according to the error I am receiving, I think in my curl command system cannot file the file. but why I am not sure?

CodePudding user response:

Simple syntax mistake.

The error says there is No such file or directory.

line 239: curl -H "X-JFrog-Art-Api: $ARTIFACTORY_PW" -XPUT "$ARTIFACTORY_IMAGE_TAG" -T dist/$file: No such file or directory

An experiment.

Let's run these three commands in your terminal.

The first one

(DO NOT REMOVE A SINGLE QUOTE.)

'touch love and peace'

The result is in bash

-bash: touch love and peace: command not found

The result is in zsh

zsh: command not found: touch love and peace

A terminal recognizes the single-quoted string as a command in the first command.

The second one

(DO NOT REMOVE A SINGLE QUOTE.)

'touch /'

The result is in bash

-bash: touch /: No such file or directory

The result is in zsh

zsh: no such file or directory: touch /

A terminal recognizes the single-quoted string with the slash as a file in the second command.

The third one

without a single quote.

touch love and peace

The result is

$ls
and  love  peace

If you intend to run the curl command in every loops,

Just remove the single quotes.

curl -H "X-JFrog-Art-Api: $ARTIFACTORY_PW" -XPUT "$ARTIFACTORY_IMAGE_TAG" -T dist/$file

The solution

      for file in ./dist/*; do 
          if [ -f  ${file} ]; then 
              export ARTIFACTORY_IMAGE_TAG=${DATASCIENCE_REPO}/$(basename ${file})/$IMAGE_VERSION
              curl -H "X-JFrog-Art-Api: $ARTIFACTORY_PW" -XPUT "$ARTIFACTORY_IMAGE_TAG" -T dist/$file
          fi 
      done

CodePudding user response:

It seems like the problem is that you are not listing the files in your for statement. Try:

for file in `ls ./dist/*`; do
    echo "act on $file" 
done
  • Related