在GitHub操作中从节点程序运行Powershell脚本 [英] Running a Powershell script from a node program in a GitHub action

查看:135
本文介绍了在GitHub操作中从节点程序运行Powershell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,就是这样.在尝试直接运行PS脚本并失败后,让我们尝试从节点脚本运行它的一种回旋方式,这在GitHub动作环境中是合法的.因此,经过多次尝试,这是我的最后一个(摘录自此答案 :

Well, yes, that's a thing. After trying to run the PS script directly 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

失败与:

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脚本位于存储库的根目录的同一目录中,该目录应在该环境变量下可用.

And I really have no idea what's happening here. The node script and the PS script are in the same directory, root directory of the repository, which should be available under that environment variable.

推荐答案

工作空间实际上与存储库中文件的位置不同.

The workspace is actually different from the location of your files in your repo.

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

In nodejs, you can write the following to get your current working directory and its files:

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中:

You can then specify a const variable const directoryPath = __dirname; and then concatenate it in your 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脚本应从那里运行.

The powershell script should run from there.

我刚刚在测试仓库中进行了测试,此处

I just tested this on a test repo here

这篇关于在GitHub操作中从节点程序运行Powershell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆