即使 PS 脚本已成功执行,Jenkins Job 仍然失败
Jenkins Job is failing even PS script is successfully executed
我有下面的脚本
停止网络服务
删除网络服务
将项目从 Jenkins 工作区复制到服务器路径
创建网络服务
启动网络服务
我在 Jenkins 作业中 运行 这个 PS 脚本,执行后,我收到一封电子邮件,因为构建失败。
val STOPPED
[SC] DeleteService SUCCESS
val DELETED
Remove-Item : Cannot remove item \Location.dll: Access to the
path '\Location.dll' is denied.
At C:\Users\Administrator\AppData\Local\Temp\hudson7587011077362516847.ps1:33 char:5
+ Remove-Item "$val\*" -Force -Recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (\10.0.1.190\d$...h\Grpc.Core.dll:FileInfo) [Remove-Item], Unauthoriz
edAccessException
+ FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
我的PS脚本
# force strict - so any variable used before being assigned causes an error
Set-PsDebug -Strict
# force PowerShell to exit with a non-zero code on teh first error
$ErrorActionPreference = 'Stop'
# set $LASTEXITCODE to zero, so the script doesn't fail if no exit code returned
$LASTEXITCODE = 0
# set directories here once, so we can reuse
$MystiflySearchDir = "\Location"
$ReleaseDir = "C:\Location"
# get directory contents (are you expecting these to return to Jenkins?)
Get-ChildItem "$ReleaseDir\*"
# create the search directory if it doesn't exist
if (-not (Test-Path -Path $val -PathType Container)) { New-Item -Path $val-type directory -Force }
# get the service, but fail gracefully if it doesn't exist
$service = Get-Service -Name val -Computername $env:SERVER -ErrorAction SilentlyContinue
# if we have a service, stop and delete it
if($service.Status)
{
sc.exe \$env:SERVER stop val
if ($LASTEXITCODE -ne 0) { throw "error stopping the service: $LASTEXITCODE" }
Write-Host "val STOPPED"
Start-Sleep -s 10
sc.exe \$env:SERVER delete val
if ($LASTEXITCODE -ne 0) { throw "error deleting the service: $LASTEXITCODE" }
Write-Host "val DELETED"
Start-Sleep -s 25
Remove-Item "$val\*" -Force -Recurse
}
# copy release to search
xcopy "$ReleaseDir\*" $val/k/e/d/Y
# (re)create the service
sc.exe \$env:SERVER create val start=auto DisplayName= "value" binPath= D:\Location.exe
if ($LASTEXITCODE -ne 0) { throw "error creating the service: $LASTEXITCODE" }
sc.exe \$env:SERVER description val "val"
if ($LASTEXITCODE -ne 0) { throw "error adding description to service: $LASTEXITCODE" }
sc.exe \$env:SERVER start val
if ($LASTEXITCODE -ne 0) { throw "error starting the service: $LASTEXITCODE" }
Write-Host "val STARTED"
这里有什么我遗漏的吗
您的代码需要稍微整理一下,因为您有几个重复的部分,无论服务是否存在都会执行这些部分。还翻转了 if 逻辑。
在您的代码开头添加了一些步骤,使 PowerShell 更加严格并在 non-zero 代码出现任何错误时退出。
这还没有经过测试,所以可能需要一点fine-tuning。但是,应该有 95%,希望如此!
# force strict - so any variable used before being assigned causes an error
Set-PsDebug -Strict
# force PowerShell to exit with a non-zero code on the first error
$ErrorActionPreference = 'Stop'
# set $LASTEXITCODE to zero, so the script doesn't fail if no exit code returned
$LASTEXITCODE = 0
# se directories her once, so we can reuse
$AirSearchDir = "\Location"
$ReleaseDir = "C:\Location"
# get directory contents (are you expecting these to return to Jenkins?)
Get-ChildItem "$ReleaseDir\*"
# create the search directory if it doesn't exist
if (-not (Test-Path -Path $AirSearchDir -PathType Container)) { New-Item -Path $AirSearchDir -type directory -Force }
# get the service, but fail gracefully if it doesn't exist
$service = Get-Service -Name val -Computername $env:SERVER -ErrorAction SilentlyContinue
# if we have a service, stop and delete it
if($service.Status)
{
sc.exe \$env:SERVER stop val
if ($LASTEXITCODE -ne 0) { throw "error stopping the service: $LASTEXITCODE" }
Write-Host "val STOPPED"
sc.exe \$env:SERVER delete val
if ($LASTEXITCODE -ne 0) { throw "error deleting the service: $LASTEXITCODE" }
Write-Host "val DELETED"
}
# copy release to search
Copy-Item "$ReleaseDir\*" $AirSearchDir -Force -Recurse
# (re)create the service
sc.exe \$env:SERVER create val start=auto DisplayName="val" binPath= D:\Location.exe
if ($LASTEXITCODE -ne 0) { throw "error creating the service: $LASTEXITCODE" }
sc.exe \$env:SERVER description val "val"
if ($LASTEXITCODE -ne 0) { throw "error adding description to service: $LASTEXITCODE" }
sc.exe \$env:SERVER start val
if ($LASTEXITCODE -ne 0) { throw "error starting the service: $LASTEXITCODE" }
Write-Host "val STARTED"
尝试 运行 使用管理员帐户的 Jenkins,出现此错误的原因是您的 jenkins 用户无法与服务交互。
运行 它与管理员帐户将赋予它足够的权限与其他服务进行交互。
我有下面的脚本
停止网络服务
删除网络服务
将项目从 Jenkins 工作区复制到服务器路径
创建网络服务
启动网络服务
我在 Jenkins 作业中 运行 这个 PS 脚本,执行后,我收到一封电子邮件,因为构建失败。
val STOPPED
[SC] DeleteService SUCCESS
val DELETED
Remove-Item : Cannot remove item \Location.dll: Access to the
path '\Location.dll' is denied.
At C:\Users\Administrator\AppData\Local\Temp\hudson7587011077362516847.ps1:33 char:5
+ Remove-Item "$val\*" -Force -Recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (\10.0.1.190\d$...h\Grpc.Core.dll:FileInfo) [Remove-Item], Unauthoriz
edAccessException
+ FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
我的PS脚本
# force strict - so any variable used before being assigned causes an error
Set-PsDebug -Strict
# force PowerShell to exit with a non-zero code on teh first error
$ErrorActionPreference = 'Stop'
# set $LASTEXITCODE to zero, so the script doesn't fail if no exit code returned
$LASTEXITCODE = 0
# set directories here once, so we can reuse
$MystiflySearchDir = "\Location"
$ReleaseDir = "C:\Location"
# get directory contents (are you expecting these to return to Jenkins?)
Get-ChildItem "$ReleaseDir\*"
# create the search directory if it doesn't exist
if (-not (Test-Path -Path $val -PathType Container)) { New-Item -Path $val-type directory -Force }
# get the service, but fail gracefully if it doesn't exist
$service = Get-Service -Name val -Computername $env:SERVER -ErrorAction SilentlyContinue
# if we have a service, stop and delete it
if($service.Status)
{
sc.exe \$env:SERVER stop val
if ($LASTEXITCODE -ne 0) { throw "error stopping the service: $LASTEXITCODE" }
Write-Host "val STOPPED"
Start-Sleep -s 10
sc.exe \$env:SERVER delete val
if ($LASTEXITCODE -ne 0) { throw "error deleting the service: $LASTEXITCODE" }
Write-Host "val DELETED"
Start-Sleep -s 25
Remove-Item "$val\*" -Force -Recurse
}
# copy release to search
xcopy "$ReleaseDir\*" $val/k/e/d/Y
# (re)create the service
sc.exe \$env:SERVER create val start=auto DisplayName= "value" binPath= D:\Location.exe
if ($LASTEXITCODE -ne 0) { throw "error creating the service: $LASTEXITCODE" }
sc.exe \$env:SERVER description val "val"
if ($LASTEXITCODE -ne 0) { throw "error adding description to service: $LASTEXITCODE" }
sc.exe \$env:SERVER start val
if ($LASTEXITCODE -ne 0) { throw "error starting the service: $LASTEXITCODE" }
Write-Host "val STARTED"
这里有什么我遗漏的吗
您的代码需要稍微整理一下,因为您有几个重复的部分,无论服务是否存在都会执行这些部分。还翻转了 if 逻辑。
在您的代码开头添加了一些步骤,使 PowerShell 更加严格并在 non-zero 代码出现任何错误时退出。
这还没有经过测试,所以可能需要一点fine-tuning。但是,应该有 95%,希望如此!
# force strict - so any variable used before being assigned causes an error
Set-PsDebug -Strict
# force PowerShell to exit with a non-zero code on the first error
$ErrorActionPreference = 'Stop'
# set $LASTEXITCODE to zero, so the script doesn't fail if no exit code returned
$LASTEXITCODE = 0
# se directories her once, so we can reuse
$AirSearchDir = "\Location"
$ReleaseDir = "C:\Location"
# get directory contents (are you expecting these to return to Jenkins?)
Get-ChildItem "$ReleaseDir\*"
# create the search directory if it doesn't exist
if (-not (Test-Path -Path $AirSearchDir -PathType Container)) { New-Item -Path $AirSearchDir -type directory -Force }
# get the service, but fail gracefully if it doesn't exist
$service = Get-Service -Name val -Computername $env:SERVER -ErrorAction SilentlyContinue
# if we have a service, stop and delete it
if($service.Status)
{
sc.exe \$env:SERVER stop val
if ($LASTEXITCODE -ne 0) { throw "error stopping the service: $LASTEXITCODE" }
Write-Host "val STOPPED"
sc.exe \$env:SERVER delete val
if ($LASTEXITCODE -ne 0) { throw "error deleting the service: $LASTEXITCODE" }
Write-Host "val DELETED"
}
# copy release to search
Copy-Item "$ReleaseDir\*" $AirSearchDir -Force -Recurse
# (re)create the service
sc.exe \$env:SERVER create val start=auto DisplayName="val" binPath= D:\Location.exe
if ($LASTEXITCODE -ne 0) { throw "error creating the service: $LASTEXITCODE" }
sc.exe \$env:SERVER description val "val"
if ($LASTEXITCODE -ne 0) { throw "error adding description to service: $LASTEXITCODE" }
sc.exe \$env:SERVER start val
if ($LASTEXITCODE -ne 0) { throw "error starting the service: $LASTEXITCODE" }
Write-Host "val STARTED"
尝试 运行 使用管理员帐户的 Jenkins,出现此错误的原因是您的 jenkins 用户无法与服务交互。
运行 它与管理员帐户将赋予它足够的权限与其他服务进行交互。