Search code examples
shellbusybox

Wrong syntax to use file content as parameters


In a CI pipeline I am running kaniko executor command (using busybox). Beside two parameters I want to get all --build-arg values using a build.args file. The file has a simple key/value content

/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"

But I do get multiple error like eval: line 149: out+=--build-arg name="application-name" : not found for each value. What am I missing and why is my code wrong?


Update

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

Solution

  • I'd recommend you do the shell stuff in a shell script, using the positional parameters as Benjamin suggests:

    #!/bin/sh
    
    while IFS= read -r line; do
        set -- "$@" --build-arg "$line"
    done < build.args
    
    /kaniko/executor "$@"
    

    Then the CI pipeline step would be

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

    To pass the build.args file as a parameter, I would do this:

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