docker / build-push-action

GitHub Action to build and push Docker images with Buildx
https://github.com/marketplace/actions/build-and-push-docker-images
Apache License 2.0
4.28k stars 550 forks source link

Quotation marks don't work in build-args #852

Closed mallycrip closed 1 year ago

mallycrip commented 1 year ago

Troubleshooting

Before submitting a bug report please read the Troubleshooting doc.

Behaviour

Steps to reproduce this issue

Below is the Docker file

FROM alpine
ARG TEMP
RUN printenv TEMP
...

And here is the action file.

      ...
      - name: Get args
        id: get_args
        run: |
          STR=$(cat << EOF
          First line
          Second "line"
          "Third" "line"
          EOF
          )
          STR="${STR//'%'/'%25'}"
          STR="${STR//$'\n'/'%0A'}"
          STR="${STR//$'\r'/'%0D'}"
          echo "::set-output name=content::$STR"
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2

      - name: Build and push
        uses: docker/build-push-action@v4
        with:
          build-args: |
            "TEMP=${{ steps.get_args.outputs.content }}"
      ...

Expected behaviour

When building an image, all values including double quotes are passed as build-args arguments, and the value is printed correctly in the RUN printenv TEMP command.

Actual behaviour

After the double quotes, the --build-arg command was abnormally called again, and RUN printenv TEMP did not run because there was no value in TEMP.

image image
mallycrip commented 1 year ago

As additional information, it seems there is an issue with actions-toolkit.

image image
crazy-max commented 1 year ago

Thanks for taking time to debug.

As additional information, it seems there is an issue with actions-toolkit.

We have test cases that look to cover multi-line value already: https://github.com/docker/build-push-action/blob/f2a1d5e99d037542a71f64918e516c093c6f3fc4/__tests__/context.test.ts#L85-L107

I don't see any issue with the result from your screenshots. Let me know what you expect from your tests.

After the double quotes, the --build-arg command was abnormally called again, and RUN printenv TEMP did not run because there was no value in TEMP.

image image

If you have double quotes in a build-arg value, you need to escape them because this character is used to surround a field: https://csv.js.org/parse/options/quote/

What you can do for this case is just using an env var so you don't need to pass the build-arg value in the input:

      ...
      - name: Get args
        id: get_args
        run: |
          STR=$(cat << EOF
          First line
          Second "line"
          "Third" "line"
          EOF
          )
          echo 'STR_TEMP<<EOF' >> $GITHUB_ENV
          echo $STR >> $GITHUB_ENV
          echo 'EOF' >> $GITHUB_ENV

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2

      - name: Build and push
        uses: docker/build-push-action@v4
        with:
          build-args: |
            STR_TEMP
      ...
FROM alpine
ARG STR_TEMP
RUN printenv STR_TEMP
...

I'm closing this issue in the meantime but leave a comment if you think there is still an issue or if we can improve our documentation.