shell 如何在gitlab-ci中获取文件夹中的文件列表

ebdffaop  于 2022-12-13  发布在  Shell
关注(0)|答案(2)|浏览(223)

我正在尝试在gitlab-ci中创建一个管道。我的问题是当我运行ls命令时,我可以看到我的文件,但当我使用for condition时,我找不到相同的文件。有什么建议吗?

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

我输出如下:

$ 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

更新1:根据我收到的错误,我认为在我的curl命令系统不能文件的文件。但为什么我不确定?

kulphzqa

kulphzqa1#

简单语法错误。

该错误指出存在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

一个实验。

让我们在终端中运行这三个命令。
第一个
请勿删除任何报价。

'touch love and peace'

结果以bash为单位

-bash: touch love and peace: command not found

结果以zsh为单位

zsh: command not found: touch love and peace

终端将单引号字符串识别为第一个命令中的命令

第二个
请勿删除任何报价。

'touch /'

结果以bash为单位

-bash: touch /: No such file or directory

结果以zsh为单位

zsh: no such file or directory: touch /

终端将带有斜杠的单引号字符串识别为第二个命令中的文件

第三个
没有一个引号。

touch love and peace

结果是

$ls
and  love  peace

如果要在每个循环中运行curl命令,

只需去掉单引号。

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

该解决方案

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 $file
          fi 
      done
nfzehxib

nfzehxib2#

问题似乎是您没有在for语句中列出文件。请尝试:

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

相关问题