Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to pass in a multiline string in build arg #91

Closed
bob-bins opened this issue Feb 8, 2021 · 10 comments · Fixed by #92 or #93
Closed

Unable to pass in a multiline string in build arg #91

bob-bins opened this issue Feb 8, 2021 · 10 comments · Fixed by #92 or #93

Comments

@bob-bins
Copy link

bob-bins commented Feb 8, 2021

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.

@whoan
Copy link
Owner

whoan commented Feb 9, 2021

@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.

@bob-bins
Copy link
Author

bob-bins commented Feb 9, 2021

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.

@whoan
Copy link
Owner

whoan commented Feb 9, 2021

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

@bob-bins
Copy link
Author

bob-bins commented Feb 9, 2021

Wouldn't --env run into the same issue as --build-args since they are all parsed the same way in here

${INPUT_BUILD_EXTRA_ARGS} \
?

@whoan
Copy link
Owner

whoan commented Feb 9, 2021

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 ...

@bob-bins
Copy link
Author

bob-bins commented Feb 9, 2021

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!

@whoan
Copy link
Owner

whoan commented Feb 11, 2021

@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"}'

@bob-bins
Copy link
Author

bob-bins commented Feb 17, 2021

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

@whoan whoan reopened this Feb 21, 2021
@whoan whoan closed this as completed in #93 Mar 5, 2021
@whoan whoan reopened this Mar 5, 2021
@whoan
Copy link
Owner

whoan commented Oct 17, 2022

Closing as dup of #95

@whoan whoan closed this as completed Oct 17, 2022
@nickistre
Copy link

nickistre commented Jan 24, 2023

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants