Toolkit: @actions/exec doesn't support multiple commands

Created on 17 May 2020  路  8Comments  路  Source: actions/toolkit

Describe the bug

Hello! I ran this script through @actions/exec:

await exec.exec('npm ci && npm run build --if-present')

It was expanded and run:

So it looks like the first npm command was prefixed with /usr/local/bin, but the second wasn't. It also didn't throw an error, which made this difficult to debug. What's the right way to

cc https://github.com/JasonEtco/build-and-tag-action/issues/4

To Reproduce

Create an action:

package.json:

{
  "scripts": {
    "build": "echo \"Hello!\""
  }
}

The action:

const exec = require('@actions/exec')

async function main () {
  await exec.exec('npm ci && npm run build')
}

main()

Observe that the build command isn't properly run.

Expected behavior

I'd expect the above command to "just work" - but if not, it should throw an error instead of fail silently.

enhancement exec

All 8 comments

You can use which to get the full path to npm.

I would expect @actions/exec to do that under the hood, not just for the first command but for all commands chained with &&s.

@JasonEtco, AFAIK exec is not a bash interpreter.

Sure enough, this works great

    var commandsToRun = commands.replace( /&& \\\n/m, '&& ' ).split("\n");
    var commandArrayLength = commandsToRun.length;
    for (var i = 0; i < commandArrayLength; i++) {
        await exec.exec('bash', ['-c', commandsToRun[i]], options);
    }
    - uses: ./
      with:
        commands: |
          ls -laht ./
          ls -laht ../
          pwd
          echo "HERE" && \
          echo "THERE HERE WHERE"

    - uses: ./
      with:
        commands: "echo \"test1\" && echo \"Test2\" > /tmp/test && cat /tmp/test"

    - uses: ./
      with:
        commands: "./.github/workflows/test.bash 1 2 3 4"

Screen Shot 2020-05-31 at 10 57 44 AM

imo such design create big overhead when run multiple commands
on every command exec.exec child_process spawns
its will be awesome if multiple commands execution will be added

imo such design create big overhead when run multiple commands
on every command exec.exec child_process spawns
its will be awesome if multiple commands execution will be added

Is it big overhead though? I can't imagine any modern container/system would have a problem with this... :)

Related to #346, #359.This would be a very nice feature to have! :rocket: I currently use the following workaround that was given in #359 by @hrueger.

LINUX/MAC:

exec.exec(`/bin/bash -c "my command"`)

WINDOWS:

exec.exec(`cmd /c "my command"`)

exec isn't intended to be a bash interpreter. We don't support shell functions currently. I'm going to retag this as an enhancement.

You can use the above workaround to invoke a shell with your command and run it that way, if you need access to that.

Was this page helpful?
0 / 5 - 0 ratings