buildkite-plugins / docker-buildkite-plugin

🐳📦 Run any build step in a Docker container
MIT License
113 stars 106 forks source link

redirection via `>` is not possible #142

Closed joscha closed 2 years ago

joscha commented 5 years ago

e.g.: I can't disable quoting even though the command option of the docker plugin should allow me to pass things verbatim.

A step like this:

  - label: ':bash: Lint: SH'
    command: ''
    artifact_paths:
      - web/target/shellcheck-*.xml
    plugins:
      - 'docker#v3.3.0':
          image: 'koalaman/shellcheck:v0.7.0'
          debug: false
          workdir: /workdir
          command:
            - '-x'
            - a.sh
            - '--format'
            - checkstyle
            - '>'
            - web/target/shellcheck-42820572-83ee-4588-abf2-4f761754dba9.xml

results in:

koalaman/shellcheck:v0.7.0 -x x.sh --format checkstyle \> web/target/shellcheck-42820572-83ee-4588-abf2-4f761754dba9.xml

which means the output of the docker image is not redirected and thus not picked up as an artifact.

joscha commented 5 years ago

This is not specific to shellcheck but to any docker image - just in case you want to suggest using https://github.com/buildkite-plugins/shellcheck-buildkite-plugin for this particular problem: it also can't redirect the output and shellcheck doesn't have a first-class --out my.file flag either.

lox commented 5 years ago

I reckon this is probably outside the scope of what the docker plugin is supposed to do 🤔

I'd be happy to add support to the shellcheck plugin to write to a file? Alternately, perhaps it's easier to just call docker directly in a shellscript for this usecase?

joscha commented 5 years ago

I can definitely write a wrapper or something, but I think output redirection would be a responsibility of the plugin?

toote commented 2 years ago

From what I can see, this would be expected (albeit quite annoying) behaviour.

The command docker run ... image arg1 arg2 arg3 > output_file would redirect the output of the docker command, not what is run inside of the container itself. As you are clearly specifying that the redirection character is part of the docker command, it needs to be escaped or it wouldn't be passed on to the command running in the container... which will error out or ignore it. This is mostly because output redirection is a feature of terminal shells, not docker nor the programs run in them.

The way to workaround this issue is to use a shell as the command being executed. For example:

- label: ':bash: Lint: SH'
    command: ''
    artifact_paths:
      - web/target/shellcheck-*.xml
    plugins:
      - 'docker#v3.3.0':
          image: 'koalaman/shellcheck:v0.7.0'
          command:
            - '/bin/sh'
            - '-c'
            - 'shellcheck -x a.sh --format checkstyle > web/target/shellcheck-42820572-83ee-4588-abf2-4f761754dba9.xml'

Which is exactly what happens when you specify the command to be run through the step's command instead of the plugin's command option:

- label: ':bash: Lint: SH'
    command: 'shellcheck -x a.sh --format checkstyle > web/target/shellcheck-42820572-83ee-4588-abf2-4f761754dba9.xml'
    artifact_paths:
      - web/target/shellcheck-*.xml
    plugins:
      - 'docker#v3.3.0':
          image: 'koalaman/shellcheck:v0.7.0'

Note that this assumes that the image you are using is not forcing a particular executable through entrypoints (in which case you may have to override it)

toote commented 2 years ago

I believe there is nothing wrong with the plugin so I am closing this issue. Feel free to reply if you think it shouldn't have and we'll re-open it and continue the discussion