运行 来自节点程序的 Powershell 脚本 GitHub 操作

Running a Powershell script from a node program in a GitHub action

嗯,是的,这是一回事。 and failing, let's try the roundabout way of running it from a node script, which are legit in the GitHub actions environment. So after many attempts, here's my last one (taken from 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