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

心已入冬 提交于 2020-01-24 12:14:14

问题


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

This fails with:

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

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.


回答1:


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

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)
    })
  }
})

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 ])

The powershell script should run from there.

EDIT:

I just tested this on a test repo here



来源:https://stackoverflow.com/questions/59184730/running-a-powershell-script-from-a-node-program-in-a-github-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!