github runner on Windows not finding executables on PowerShell - github-for-windows

I am trying to use a self-hosted Windows-VM to run west for repo management, python to run some scripts, and git to push back to the repo.
The work is to generate files using python from repo-A as artifacts to check-into repo-B. I had to use Windows because the file generation tools only works on Windows.
I have a self-hosted build agent setup and able to run github actions on it.
On the VM side, these are the path to the relevant commands (showing that they are properly added to $PATH)
PS C:\TouchGFXProjects\wallSwitch-gui-hesPrototype-480-272> gcm west; gcm tgfx.exe; gcm python; gcm git
CommandType Name Version Source
----------- ---- ------- ------
Application west.exe 0.0.0.0 C:\Users\tgfx\AppData\Local\Programs\Python\Python39\Scripts\west.exe
Application tgfx.exe 4.16.1.0 C:\TouchGFX\4.16.1\designer\tgfx.exe
Application python.exe 3.9.515... C:\Users\tgfx\AppData\Local\Programs\Python\Python39\python.exe
Application git.exe 2.31.1.1 C:\Program Files\Git\cmd\git.exe
The VM's execution Policy is set to:
PS C:\Windows\system32> Get-ExecutionPolicy -List
Scope ExecutionPolicy
----- ---------------
MachinePolicy Undefined
UserPolicy Undefined
Process Undefined
CurrentUser Undefined
LocalMachine Bypass
All the scrips and execution steps ran well when executed from powerShell on the VM itself.
But when I invoke the github runner, the behaviour indicates that it does not know where the executables are, despite it being known in $PATH (see debug output below).
github runner script:
name: CI
on:
workflow_dispatch:
pull_request:
branches:
- master
- 'feature/**'
jobs:
# Test-path access using powerShell shell
test-path-powerShell:
runs-on: [self-hosted, Windows, X64, touchGFX]
steps:
- name: Display the path
run: echo ${env:PATH}
shell: powershell
continue-on-error: true
- name: Check that we know where python is
run: gcm python
shell: powershell
continue-on-error: true
- name: Test calling "python.exe --version" via powershell
run: python.exe --version
shell: powershell
continue-on-error: true
- name: Test calling "python.exe --version" via powershell (abs path)
run: C:\Users\tgfx\AppData\Local\Programs\Python\Python39\python.exe --version
shell: powershell
continue-on-error: true
When running on the github runner, the echo ${env:PATH} command shows:
Run echo ${env:PATH}
C:\TouchGFX\4.16.1\designer;C:\Users\tgfx\AppData\Local\Programs\Python\Python39\;C:\Users\tgfx\AppData\Local\Programs\Python\Python39\Scripts\;C:\Users\tgfx\AppData\Local\Programs\Python\Python39\Lib\site-packages;C:\Program Files\Git\cmd;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Windows\ServiceProfiles\NetworkService\AppData\Local\Microsoft\WindowsApps
Which means it should have the location for python.exe
However, all commands that attempts to access it (even with Absolute path) returns errors like this:
Run gcm python
gcm : The term 'python' 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 C:\actions-runner\_work\_temp\c19697cd-d5e0-4102-a7b6-5f357f82aa91.ps1:2 char:1
+ gcm python
+ ~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (python:String) [Get-Command], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException,Microsoft.PowerShell.Commands.GetCommandCommand
I also tried appending python to $GITHUB_PATH with:
echo "C:\Users\tgfx\AppData\Local\Programs\Python\Python39" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
But that just extends the output of echo ${env:PATH} and still leads to the same error on execution.
What am I missing?
Thanks for everyone's time.

In my setup, the missing piece is the access permissions of the Github Action Runner Service. The default of NT AUTHORITY\NETWORK SERVICE used when installing the runner as a service does not work. I tested changing the service's permission to both Local Service and Local System and found that Local System works.
You need to restart the service when changing the permissions for it to take effect.
Within the github runner workflow, you can validate the settings using the whoami command from powershell.

Related

Why does ansible win_shell module use powershell 5 when powershell 7 is set as the default?

I'm running a packer build of windows using ansible to provision the VM.
I connect to the VM via OpenSHH. I have powershell 7 set as the default shell for OpenSSH. When I connect to the VM over ssh on the command line, the banner says PowerShell 7.1.3.
I run the following code via ansible-playbook:
- name: Check powershell version
ansible.windows.win_shell: |
Get-Host | Select-Object Version
- name: Check powershell version
ansible.windows.win_shell: |
Get-Host | Select-Object Version
args:
executable: pwsh
The first task returns 5.1.19041.1023
The second task returns 7.1.3
So when I don't specify the executable, win_shell is running powershell.exe, aka powershell 5.
When I do specify the executable as pwsh, aka powershell 7, it behaves correctly.
I can't figure out why the win_shell module is ignoring the default set in the registry. Does anyone know what I have done wrong?

Running Azure CLI script in Azure DevOps Pipeline however unable to find the CLI script

param (
$ProjectFilePath= $(Buildin.SourcesDirectory)\TESTREPO7\RuleConfig.json
$ExecutableFilePath= $(Buildin.SourcesDirectory)\TESTREPO7\Studio.zip\Studio\UiPath.Studio.CommandLine.exe
$OutputFilePath= $(Buildin.SourcesDirectory)\$(Get-Date -Format 'yyyy-MM-dd-HH-mm-ss')-Workflow-Analysis.json
)
Write-Output "$(Get-Date -Format 'HH:mm:ss') - STARTED - Workflow Analyzer CLI Script"
$Command = "$ExecutableFilePath analyze -p $ProjectFilePath"
Invoke-Expression $Command | Out-File -FilePath $OutputFilePath
Write-Output "$(Get-Date -Format 'HH:mm:ss') - COMPLETED - Workflow Analyzer CLI Script"
XXXXXXXXXXXXXXXXXXXXXXXXXXXX
Error:
##[section]Starting: Azure CLI UiPathWorkFlow
==============================================================================
Task : Azure CLI
Description : Run Azure CLI commands against an Azure subscription in a Shell script when running on Linux agent or Batch script when running on Windows agent.
Version : 1.152.1
Author : Microsoft Corporation
Help : https://learn.microsoft.com/azure/devops/pipelines/tasks/deploy/azure-cli
==============================================================================
##[error]Script failed with error: Error: Unable to locate executable file: 'C:\agent1\_work\362\s\TESTREPO7\UiPathAnalyze.ps1'. Please verify either the file path exists or the file can be found within a directory specified by the PATH environment variable. Also verify the file has a valid extension for an executable file.
##[section]Finishing: Azure CLI UiPathWorkFlow
XXXXXXXXXXXXXXXXXXXX
Very important- When on a local computer, uiath.studio.commandline.exe (workflow analyzer) has rules stored in User directory (%LocalAppData%\UiPath\Rules = C:\Users\User1\AppData\Local\UiPath\Rules). How would I be able to input this information in the script provided above??
Azure CLI latest version installed on azure DevOps agent server.
Tried adding environment parameter but not sure what to enter but this error occurred anyways. Added Environment made did not fix the error.
added both below environmentals—
PATH $(Buildin.SourcesDirectory)\TESTREPO7\UipathAnalyze.ps1
Build.SourcesDirectory $(Buildin.SourcesDirectory)\TESTREPO7\UipathAnalyze.ps1
Check this doc predefined variables, the path should be $(Build.SourcesDirectory) instead of $(Buildin.SourcesDirectory)
The error message is: Unable to locate executable file: 'C:\agent1\_work\362\s\TESTREPO7\UiPathAnalyze.ps1'
The UipathAnalyze.ps1 is the code you shared and it it the Script Path in the task Azure CLI, right? We need open the folder path C:\agent1\_work\362\s\TESTREPO7 to check if the file exists.
In addition, could you successfully run the .ps1 file on the local agent machine?

Yarn is installed but does not work. Command is recognized but no yarn commands do anything. Windows Powershell

node version 11.13.0
npm version 6.9.0
I have installed yarn multiple ways, first with the npm install -g yarn, then I tried the .msi installer, and I also tried installing with chocolatey from an elevated shell. Usually, when I try to use something I have not installed, I get a message that looks like this:
PS C:\Users\[me]> hello
hello : The term 'hello' 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
+ hello
+ ~~~~~
+ CategoryInfo : ObjectNotFound: (hello:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
However, yarn commands are recognized but do nothing.
PS C:\Users\[me]> yarn --version
PS C:\Users\[me]>
I've tried changing the Path environment variable to include the following:
C:\Users\[me]\AppData\Local\Yarn\bin
C:\Program Files (x86)\Yarn
C:\Program Files (x86)\Yarn\bin
but this didn't change anything.
you must be add %USERPROFILE%\AppData\Roaming\npm in environment path
1-search environment in the start menu
https://i.stack.imgur.com/y4zxD.jpg
2-Click environment variables
https://i.stack.imgur.com/B59yD.jpg
3-edit path of user variables
https://i.stack.imgur.com/Vz73p.jpg
4-add URI of yarn as new
https://i.stack.imgur.com/Rq7xK.jpg
then after that if can't use yarn command in vscode and get error yarn.ps1 cannot be loaded because running scripts is disabled on this system; open Powershell as Administrator and run below command
Set-ExecutionPolicy Unrestricted
--version is not a valid switch, use yarn version instead - https://classic.yarnpkg.com/en/docs/cli/version.
Even after adding environment variable on windows I got error. Here is fix
%USERPROFILE%\AppData\Roaming\npm in environment path
Open windows command shell as administrator and run command:
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted
For those who are not comfortable with changing Execution policy, here's a solution that worked for me:
Solution: the problem was one extra "\" was missing after bin.
Before: %USERPROFILE%\AppData\Roaming\npm
After: %USERPROFILE%\AppData\Roaming\npm\

How to troubleshoot the error [The term 'pwsh.exe' is not recognized as the name of a cmdlet, function, script file, or operable program]?

While creating a new pipeline on Azure DevOps to set up a CI for a .NET project, I set up the following PowerShell script to automate the .NET Core setup.
Here is the script:
$ErrorActionPreference="Stop"
$ProgressPreference="SilentlyContinue"
# $LocalDotnet is the path to the locally-installed SDK to ensure the
# correct version of the tools are executed.
$LocalDotnet=""
# $InstallDir and $CliVersion variables can come from options to the
# script.
$InstallDir = "./cli-tools"
$CliVersion = "1.0.1"
# Test the path provided by $InstallDir to confirm it exists. If it
# does, it's removed. This is not strictly required, but it's a
# good way to reset the environment.
if (Test-Path $InstallDir)
{
rm -Recurse $InstallDir
}
New-Item -Type "directory" -Path $InstallDir
Write-Host "Downloading the CLI installer..."
# Use the Invoke-WebRequest PowerShell cmdlet to obtain the
# installation script and save it into the installation directory.
Invoke-WebRequest `
-Uri "https://dot.net/v1/dotnet-install.ps1" `
-OutFile "$InstallDir/dotnet-install.ps1"
Write-Host "Installing the CLI requested version ($CliVersion) ..."
# Install the SDK of the version specified in $CliVersion into the
# specified location ($InstallDir).
& $InstallDir/dotnet-install.ps1 -Version $CliVersion `
-InstallDir $InstallDir
Write-Host "Downloading and installation of the SDK is complete."
# $LocalDotnet holds the path to dotnet.exe for future use by the
# script.
$LocalDotnet = "$InstallDir/dotnet"
When I try to run the build, I have got the following error:
and
I've already searched on Google for people who have the same problem and how to fix it. But I haven't found much information yet. The Azure DevOps forum doesn't help either.
As mentioned in the comment from above, all you have to do is install the appropriate version of PowerShell on the machine that Agent is running on. For example, PowerShell 7. Then you have to make sure that the environment variable path is set. This variable should point to the directory with PowerShell Core.
Windows
Just install PowerShell Core with the Windows Installer (.msi file from PowerShell Git repository). In this case, the path environment variable is automatically set or expanded so that there will be the path to the directory with pwsh.exe under this variable.
Linux
Install PowerShell Core that is supported by your distribution. Make sure that there is a path variable in your ~/.bashrc file and that path contains the path to the directory with PowerShell Core.
Note: If Azure Agent is already running, you have to restart it so that it sees the changes in the path variable. Hence, on Windows, just restart the agent if run interactively and restart the service if run as a service. On Linux, you can follow this guide in order to update the environment variables that were passed to the Agent.
I know you have already configured your script as a PowerShell Core script, but for completeness I add this: If you use a PowerShell task in your Azure pipeline, the Core version of PowerShell is not set for it by default. In order to run the task as the PowerShell Core script, add this to the YAML code of the task: pwsh: true. Otherwise, if you are still using the old graphical interface, check the "Use PowerShell Core" checkbox under the "Advanced" heading for the task.

Powershell for GitHub setup

I just installed GitHub for windows and for convenience I added entries for the powershell in the context menu in Windows Explorer. The registry commands are:
C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -NoExit
C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe -NoExit -Command Set-Location -LiteralPath '%L'
As suggested in shell.ps1 I added ". (Resolve-Path "$env:LOCALAPPDATA\GitHub\shell.ps1")" to my profile.ps1 to get poshgit and the rest setup within my powershell environment. It is not completely working though. I can see that shell.ps1 was executed (when running "$env:github_posh_git" I see the right value appearing) but when I navigate to a git repo I do not see the enhanced prompt.
What am I missing?
PS: The only version of Powershell I have came with GitHub for windows and even though the folder says "1.0" running
$psversiontable.psversion
returned
Major Minor Build Revision
----- ----- ----- --------
2 0 -1 -1
I got an answer from GitHub support:
The enhanced prompt is provided by posh-git [1], and it isn't added when you run the line in shell.ps1. However, it's easy enough to install.
Just cd to C:\Users\Username\AppData\GitHub\PoshGit_* and run the install.ps1 script from PowerShell. It will add it to your PowerShell $profile automatically.