【问题标题】:Wrong syntax to use file content as parameters使用文件内容作为参数的错误语法
【发布时间】:2021-09-28 17:15:53
【问题描述】:

在 CI 管道中,我正在运行 kaniko 执行器命令(使用 busybox)。 除了两个参数之外,我还想使用 build.args 文件获取所有 --build-arg 值。该文件有一个简单的键/值内容

/kaniko/executor \
  --context $path \
  --destination $CI_REGISTRY_IMAGE \
  $(for i in `cat build.args`; do out+="--build-arg $i " ; done; echo $out;out="")

build.args

name="application-name"
version="1.2.3"
port="3000"
title="Great image"
description="This is a wonderful description of nothing"

但是对于每个值,我确实收到了多个错误,例如 eval: line 149: out+=--build-arg name="application-name" : not found。我错过了什么,为什么我的代码有误?


更新

deploy:
  stage: deployment
  image:
    name: gcr.io/kaniko-project/executor:v1.6.0-debug
    entrypoint: ['']
  script:
    - mkdir -p /kaniko/.docker
    - echo "{\"auths\":{\"$CI_REGISTRY\":{\"auth\":\"$(echo -n ${CI_REGISTRY_USER}:${CI_REGISTRY_PASSWORD} | base64)\"}}}" > /kaniko/.docker/config.json
    - |
      apps=$(cat $AFFECTED_APPS)
      for item in $apps; do
        domain=${item%-*}
        app=${item##*-}
        path=$CI_PROJECT_DIR/dist/apps/${domain}/${app}
        echo "Build docker image ${item}"
        ${$CI_PROJECT_DIR}/tools/scripts/build_docker.sh ${path}/build.args \
          --context $path \
          --destination $CI_REGISTRY_IMAGE \
          --build-arg BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
      done

【问题讨论】:

  • Busybox sh 不支持+=
  • @BenjaminW。哦。什么是busybox解决方案?
  • 即兴发挥,类似于out="$out --build-arg $i",但它仍然受到所有未引用扩展的影响,因此使用起来不是超级安全。 github.com/dylanaraps/pure-sh-bible 可能有更好方法的指针,但我现在什么也没看到。
  • @BenjaminW。你能解释一下为什么它可能不安全吗?
  • “不安全”不是正确的词,而是“不健壮” - 由于分词和文件名扩展(“globbing”),未加引号的扩展可能会产生各种副作用。如果显示build.args的内容,或许可以提出更好的解决方案,可能使用位置参数,或者使用sed等处理文件内容。

标签: shell busybox


【解决方案1】:

我建议您使用 Benjamin 建议的位置参数在 shell 脚本中执行 shell 操作:

#!/bin/sh

while IFS= read -r line; do
    set -- "$@" --build-arg "$line"
done < build.args

/kaniko/executor "$@"

那么 CI 管道步骤将是

/path/to/executor.sh --context "$path" --destination "$CI_REGISTRY_IMAGE"

要将 build.args 文件作为参数传递,我会这样做:

#!/bin/sh
build_args=$1
shift

while IFS= read -r line; do
    set -- "$@" --build-arg "$line"
done < "$build_args"

/kaniko/executor "$@"
/path/to/executor.sh /path/to/build.args --context "$path" --destination "$CI_REGISTRY_IMAGE"

【讨论】:

  • build.args 的路径必须是动态的,所以我必须将它传递给脚本。我该怎么做?
  • 谢谢。但我确实收到了错误syntax error: bad substitution。我正在busybox中运行脚本。这是导致错误的原因吗?
  • 这似乎表明您正在做一些特定于 bash 的事情。 Busybox sh 不是 bash。 -- stackoverflow.com/q/20323738/7552
  • 嗯...在我的 CI 阶段,我使用的是 kaniko:debug 图像,它有busybox (github.com/GoogleContainerTools/kaniko#debug-image)。我没有看到问题:-(
  • 您使用的是相同的代码吗?
猜你喜欢
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 2019-10-25
  • 1970-01-01
  • 1970-01-01
  • 2014-01-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多