如何从 Gradle 任务 运行 远程托管 shell 脚本

How to run a shell script hosted remotely from a Gradle task

假设您有一个简单的 bash 脚本

echo $@

将其托管在 public 存储库中,因此您可以像

一样访问原始文件

https://raw.githubusercontent.com/.../test.sh

然后你可以运行它shell喜欢

bash <(curl -s https://raw.githubusercontent.com/.../test.sh) "hello"

我希望能够在 gradle 任务中实现这一目标。我试过:

task testScript(type: Exec) {
  workingDir projectDir
  executable "bash <(curl -s https://raw.githubusercontent.com/.../test.sh) 'hello'"
}

task testScript(type: Exec) {
  workingDir projectDir
  executable "bash"
  args "<(curl -s https://raw.githubusercontent.com/.../test.sh)" 'hello'
}

task testScript(type: Exec) {
  workingDir projectDir
  commandLine "bash", "<(curl -s https://raw.githubusercontent.com/.../test.sh)", "hello"
}

无济于事。我做错了什么?

在您的原始命令中,<(curl -s https://raw.githubusercontent.com/.../test.sh) 不是由您调用的 bash 命令解析的,而是由您调用它的 shell 解析的,实际执行的命令是像 bash /dev/fd/63 "hello".

Gradle 不是 shell,只会使用字符串 "<(curl -s https://raw.githubusercontent.com/.../test.sh)" 作为参数调用 bash,无需进一步处理。

您需要找到一个不需要由 shell 调用它的命令扩展的命令。例如,将您当前的命令作为纯文本处理并使用另一个 shell 来解析它:

bash -c 'bash <(curl -s https://raw.githubusercontent.com/.../test.sh) hello'

总而言之,我认为以下内容应该有效:

task testScript(type: Exec) {
  workingDir projectDir
  executable "bash"
  args "-c", "bash <(curl -s https://raw.githubusercontent.com/.../test.sh) hello"
}