docker-build-with-cache-action icon indicating copy to clipboard operation
docker-build-with-cache-action copied to clipboard

Unable to pass in a multiline string in build arg

Open bob-bins opened this issue 4 years ago • 8 comments

This config does not work:

build_extra_args: --build-arg some_arg="$multiline_string"

For

multiline_string='----first line of text----
middle lines of text
----last line of text----'

I get an error like:

docker build --tag my_awesome_image --file ./Dockerfile --target release --build-arg 'some_arg="----first' line of text---- *** middle lines of text ----last line of 'text----"' .
bad flag syntax: ----last
See 'docker build --help'.

However, this does work if I run the equivalent docker build command using the cli.

bob-bins avatar Feb 08 '21 22:02 bob-bins

@bob-bins I should allow something like JSON in build_extra_args to support your use case. I can give it a try today or tomorrow. On the other hand, using an env variable might be more appropriate (just an opinion) in your scenario and you don't have to wait until I have the fix.

whoan avatar Feb 09 '21 13:02 whoan

What do you mean by "using an env var"? Do you mean hardcoding the value directly in the Dockerfile? If so I cannot do that as the value is a secret that I don't want checked into code.

bob-bins avatar Feb 09 '21 17:02 bob-bins

With ARG you will have the secret as part of your image, with ENV you can override/set with docker run --env <key>=<value>

whoan avatar Feb 09 '21 17:02 whoan

Wouldn't --env run into the same issue as --build-args since they are all parsed the same way in here https://github.com/whoan/docker-build-with-cache-action/blob/118708cf883bf7bcd2b659ecf5753c1cab7e22cc/docker-build.sh#L283?

bob-bins avatar Feb 09 '21 18:02 bob-bins

Not really as you don't have to run docker run in the action. In addition, you have options to forward the environment variable to the container:

  • docker run -e SOME=THING ...
  • docker run --env-file envfile ...
  • SOME=THING docker run -e SOME ... or export SOME=THING; docker run -e SOME ...

whoan avatar Feb 09 '21 18:02 whoan

Oh I see what you mean. Unfortunately I need this value at build time so injecting it at runtime will not work for me. I'll just keep a lookout for the fix to the build_extra_args multiline parsing. Thank you!

bob-bins avatar Feb 09 '21 18:02 bob-bins

@bob-bins Could you please try with branch support-json-for-extra-build-args?

- uses: whoan/docker-build-with-cache-action@support-json-for-extra-build-args

You will have to use something like this:

build_extra_args: '{"--build-arg": "myarg=Hello\nWorld"}'

whoan avatar Feb 11 '21 14:02 whoan

Hey sorry for the late response!!

For the --target extra arg, it's giving an error when i include quotations for the value since it parses it as literally part of the value: build_extra_args: '{"--target": "release"}'. The solution is to not have the quotations, which means it's no longer valid json which contradicts the README.

[Action Step] Building image...
+ docker build --tag my_awesome_image --file ./Dockerfile --target '"release"' --build-arg '"ssh_key=\$single_line_ssh_key"' .
+ tee build-output.log
Sending build context to Docker daemon  63.77MB

Error response from daemon: failed to reach build target "release" in Dockerfile

I attempted it again and it didn't work for me when I passed in an ssh key. It doesn't correctly convert it back to the correct format.

      - name: Convert ssh key to single line
        run: |
          single_line_ssh_key=$(echo "${{ secrets.GIT_SSH_KEY }}" | sed 's/$/\\n/'  | tr -d '\n')
          echo "single_line_ssh_key=$single_line_ssh_key" >> $GITHUB_ENV
      - uses: whoan/docker-build-with-cache-action@v5
        with:
          registry: <account_id>.dkr.ecr.us-east-1.amazonaws.com
          image_name: test
          image_tag: ${{github.event.pull_request.head.sha}}
          build_extra_args: '{"--target": release, "--build-arg": "ssh_key=$single_line_ssh_key"}'
# Error log
Load key "/root/.ssh/id_rsa": invalid format

bob-bins avatar Feb 17 '21 17:02 bob-bins

Closing as dup of #95

whoan avatar Oct 17 '22 22:10 whoan

@bob-bins I don't know if you ever figured out your own solution to this, but I managed to get this working by encoding the mutli-line secret with cat secret_multi_line_file | base64 -w 0 into a secret variable named something like SECRET_MULTI_LINE_FILE_BASE64 then setting up the line as:

build_extra_args: "--build-arg=SECRET_MULTI_LINE_FILE_BASE64=${{ secrets.SECRET_MULTI_LINE_FILE_BASE64 }}"

In the Dockerfile, I decoded the file like

RUN echo "$SECRET_MULTI_LINE_FILE_BASE64" | base64 -d > secret_mutli_line_file

I hope this gives other people an idea on how to handle this when searching for this issue.

nickistre avatar Jan 24 '23 05:01 nickistre