使用文件内容作为参数的语法错误

Wrong syntax to use file content as parameters

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

/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

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

#!/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=
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"