通过Jenkins运行时Powershell脚本出现问题

kx7yvsdv  于 2023-01-08  发布在  Jenkins
关注(0)|答案(1)|浏览(212)

我正在尝试将NodeJS, NPM, PM2, and Yarn安装到运行Windows的EC2机器上。我在下面编写了PowerShell脚本来完成这项工作。当我直接在EC2机器上运行此脚本时,一切都正常。

$target="c:/omnilink/assetwhere/platform/node.msi"

function SetPathVariables {

    [CmdletBinding()]
    param (
        [string]$PathName
    )
    $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine")
    [Environment]::SetEnvironmentVariable('Path', $env:Path + ';'+$PathName, 'Machine')
    #Removing duplicates
    [Environment]::SetEnvironmentVariable('Path',(([Environment]::GetEnvironmentVariable('Path','Machine') -split ';'|Sort-Object -Unique) -join ';'),'Machine')
    $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine")
}

echo "Downloading nodeJS"
Read-S3Object -BucketName assetwhere-build-components -File $target -Key 'Components/Nodejs/node-v18.12.1-x64.msi'

echo "Installing NodeJS"
Start-Process msiexec.exe -Wait -ArgumentList '/I C:\omnilink\assetwhere\platform\node.msi /quiet'
echo "Setting Path"
SetPathVariables -PathName 'C:\Program Files\nodejs\'
echo "Verifying NodeJS Installation"
node --version
npm --version
echo "Successfully Installed NodeJS"

echo "Installing yarn"
npm install --global yarn
echo "Installing pm2"
npm install --global pm2
SetPathVariables 'C:\Users\Administrator\AppData\Roaming\npm'
echo "Verifying global packages installations"
echo "Yarn Version: "
yarn --version 

echo "Pm2 Version: "
pm2 --version

当我尝试通过Jenkins管道执行此脚本时,脚本成功安装到节点js。但是,无法安装yarn & pm2,我可以知道为什么吗?这是我的Jenkins日志

Downloading nodeJS

Mode                LastWriteTime         Length Name                                                                  
----                -------------         ------ ----                                                                  
-a----         1/7/2023  10:49 AM       30683136 node.msi                                                              
Installing NodeJS
Setting Path
Verifying NodeJS Installation
v18.12.1
8.19.2
Successfully Installed NodeJS
Installing yarn

added 1 package, and audited 2 packages in 920ms

found 0 vulnerabilities
Installing pm2

added 184 packages, and audited 185 packages in 23s

12 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
Verifying global packages installations
Pm2 Version: 
pm2 : The term 'pm2' 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:\omnilink\configure\InstallAssetWhereAPIRequirementsScript.ps1:36 char:1
+ pm2 --version
+ ~~~
    + CategoryInfo          : ObjectNotFound: (pm2:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
hkmswyz6

hkmswyz61#

在我更改了windows中的npm global path后,默认情况下它指向C:\Users\Administrator\AppData\Roaming\npm。这需要管理员的权限。
我已经更改了node js目录的全局路径。我在C:/Program Files/nodejs中创建了一个名为npm-global的文件夹,更改了npm全局配置。
更新脚本:

$target="c:/omnilink/assetwhere/platform/node.msi"
$npmGlobalPath = "C:/Program Files/nodejs/npm-global"
$npmGlobalCachePath = "C:/Program Files/nodejs/npm-cache"

function SetPathVariables {

    [CmdletBinding()]
    param (
        [string]$PathName
    )
    $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine")
    [Environment]::SetEnvironmentVariable('Path', $env:Path + ';'+$PathName, 'Machine')
    #Removing duplicates
    [Environment]::SetEnvironmentVariable('Path',(([Environment]::GetEnvironmentVariable('Path','Machine') -split ';'|Sort-Object -Unique) -join ';'),'Machine')
    $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine")
}

function createFolder($path) {
    If(!(test-path -PathType container $path)) {
        mkdir $path;
    }
}

echo "Downloading nodeJS"
Read-S3Object -BucketName assetwhere-build-components -File $target -Key 'Components/Nodejs/node-v18.12.1-x64.msi'

echo "Installing NodeJS"
Start-Process msiexec.exe -Wait -ArgumentList '/I C:\omnilink\assetwhere\platform\node.msi /quiet'
echo "Setting Path"
SetPathVariables -PathName 'C:\Program Files\nodejs\'
echo "Verifying NodeJS Installation"
node --version
npm --version
echo "Successfully Installed NodeJS"

echo "Chaning NPM global path"
createFolder $npmGlobalPath
createFolder $npmGlobalCachePath
npm config --global set prefix $npmGlobalPath
npm config --global set cache  $npmGlobalCachePath

echo "Installing latest npm"
npm install -g npm@latest

echo "Installing yarn"
npm install --global yarn
echo "Installing pm2"
npm install --global pm2
SetPathVariables $npmGlobalPath
echo "Verifying global packages installations"
echo "Yarn Version: "
yarn --version 

echo "Pm2 Version: "
pm2 --version

相关问题