【问题标题】:Running a Powershell script from a node program in a GitHub action在 GitHub 操作中从节点程序运行 Powershell 脚本
【发布时间】:2020-03-29 18:28:51
【问题描述】:

嗯,是的,那是一回事。在trying to run the PS script directly 失败后,让我们尝试从节点脚本运行它的迂回方式,这在 GitHub 操作环境中是合法的。所以经过多次尝试,这是我的最后一个(取自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 脚本在同一个目录,存储库的根目录,在那个环境变量下应该是可用的。

【问题讨论】:

  • 顺便说一句(我无法解释您的症状):最好使用-File CLI 参数来运行脚本(并使用-NoProfile 进行可预测性):child = spawn("powershell.exe", [ '-NoProfile', '-File', file ])跨度>
  • +1 to @mklement0 这是从 CLI 执行 powershell.exe 可执行文件时调用 .ps1 脚本文件的首选方式。

标签: node.js powershell github-actions


【解决方案1】:

工作区实际上与您的存储库中文件的位置不同。

在 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 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 上对此进行了测试

【讨论】:

    猜你喜欢
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-28
    • 2012-12-12
    相关资源
    最近更新 更多