好吧,是的,那是一回事。在trying to run the PS script directly失败之后,让我们尝试从节点脚本运行回旋的方式,这在GitHub action环境中是合法的。因此,经过多次尝试,这是我的最后一个(摘自this answer:

var spawn = require("child_process").spawn,child;
var workspace = process.env.GITHUB_WORKSPACE;
var file = workspace + "\\upgrade.ps1";
console.log( "Workspace ", workspace,  " file ", file );
child = spawn("powershell.exe",[ file ]); // more stuff to print output after this

fails具有:
Workspace  d:\a\rakudo-star-fix-action\rakudo-star-fix-action  file  d:\a\rakudo-star-fix-action\rakudo-star-fix-action\upgrade.ps1
Powershell Errors: d:\a\rakudo-star-fix-action\rakudo-star-fix-action\upgrade.ps1 : The term

Powershell Errors: 'd:\a\rakudo-star-fix-action\rakudo-star-fix-action\upgrade.ps1' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is
correct and try again.
At line:1 char:1
+ d:\a\rakudo-star-fix-action\rakudo-star-fix-action\upgrade.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (d:\a\rakudo-sta...ion\upgrade.ps1:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException


Powershell Script finished

我真的不知道这里发生了什么。节点脚本和PS脚本位于存储库的根目录的同一目录中,该目录应在该环境变量下可用。

最佳答案

实际上,工作空间不同于您在回购中文件的位置。

在nodejs中,您可以编写以下内容来获取当前的工作目录及其文件:

const directoryPath = __dirname;
console.log(directoryPath);
fs.readdir(directoryPath, function(err, files) {
  if (err) {
    console.log("Error getting directory information.")
    console.log(err)
  } else {
    files.forEach(function(file) {
      console.log(file)
    })
  }
})

然后,您可以指定const变量const directoryPath = __dirname;,然后将其连接到var file中:
const directoryPath = __dirname;
var spawn = require("child_process").spawn,child;
var file = directoryPath + "\\upgrade.ps1";
child = spawn("powershell.exe",["-NoProfile", "-File", file ])

powershell脚本应从此处运行。

编辑:

我刚刚在测试仓库here上测试了

node.js - 在GitHub操作中从 Node 程序运行Powershell脚本-LMLPHP

关于node.js - 在GitHub操作中从 Node 程序运行Powershell脚本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59184730/

10-14 13:20
查看更多