Linux 防止应用程序执行的警告向VSO输出错误
我有一个pwsh任务,它作为psakeLinux 防止应用程序执行的警告向VSO输出错误,linux,powershell,azure-pipelines,azure-pipelines-build-task,psake,Linux,Powershell,Azure Pipelines,Azure Pipelines Build Task,Psake,我有一个pwsh任务,它作为psakeexec的一部分执行apt install。apt install似乎会抛出一个警告,使其进入管道输出流,无论我如何尝试捕获它: “警告:apt没有稳定的CLI界面。请在脚本中小心使用” ##[debug]已处理:##vso[task.issue type=error;]%0AWARNING:apt没有稳定的CLI接口。在脚本中小心使用。%0A%0A 我已经将它包装在一个try-catch中,设置退出0,但没有任何效果。我做错了什么,还没有想到,或者是我还没
exec
的一部分执行apt install
。apt install
似乎会抛出一个警告,使其进入管道输出流,无论我如何尝试捕获它:
“警告:apt没有稳定的CLI界面。请在脚本中小心使用”
##[debug]已处理:##vso[task.issue type=error;]%0AWARNING:apt没有稳定的CLI接口。在脚本中小心使用。%0A%0A
我已经将它包装在一个try-catch中,设置退出0
,但没有任何效果。我做错了什么,还没有想到,或者是我还没有意识到的其他问题
注意:我希望pwsh任务在大多数标准错误上失败,但显然不是警告,尤其是对于成功的代码
PowerShell
if ($IsLinux) {
Exec {
try {
Write-Host 'Executing apt install on Linux...'
sudo apt install nuget
Exit 0
} catch {
Exit 0
}
}
}...
管道任务
steps:
- pwsh: |
if ($null -eq (Get-Module -Name psake -listAvailable)) {
Install-Module psake -AcceptLicense -Force
}
Import-Module psake
Invoke-psake ./build/tasks.ps1 -taskList Test
exit ([int](-not $psake.build_success))
displayName: Execute Unit Tests and Build Package
failOnStderr: true
name: Build_and_Test
相关日志输出
Executing Init
Installing package 'PSDepend' Downloaded 0.00 MB out of 0.07 MB. [ ] Installing package 'PSDepend' Unzipping [ooooooooooooooooooooooooooooooooooooooooooooooo ] Installing package 'BuildHelpers' Downloaded 0.00 MB out of 0.08 MB. [ ] Installing package 'Pester' Downloaded 0.00 MB out of 0.85 MB. [ ] Installing package 'Pester' Downloaded 0.09 MB out of 0.85 MB. [oooo ] Executing Clean
Executing Build
Directory: /home/vsts/work/1/s
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 3/6/20 3:39 PM dist
Checking for NuGet install...
NuGet not installed, installing...
Executing apt install on Linux...
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
Reading package lists...
Building dependency tree...
Reading state information...
The following packages were automatically installed and are no longer required:
bc dns-root-data dnsmasq-base ebtables grub-pc-bin landscape-common
liblldb-6.0 liblldb-8 liblxc-common liblxc1 libuv1 linux-headers-4.15.0-88
lxcfs lxd lxd-client python3-attr python3-automat python3-click
python3-colorama python3-constantly python3-hyperlink python3-incremental
python3-pam python3-pyasn1 python3-pyasn1-modules python3-service-identity
python3-twisted python3-twisted-bin python3-zope.interface uidmap xdelta3
Use 'sudo apt autoremove' to remove them.
The following NEW packages will be installed:
nuget
0 upgraded, 1 newly installed, 0 to remove and 11 not upgraded.
Need to get 1307 kB of archives.
After this operation, 5826 kB of additional disk space will be used.
Get:1 https://download.mono-project.com/repo/ubuntu stable-bionic/main amd64 nuget all 5.5.0.6319.bin-0xamarin1+ubuntu1804b1 [1307 kB]
Fetched 1307 kB in 0s (12.1 MB/s)
Selecting previously unselected package nuget.
(Reading database ...
(Reading database ... 5%
(Reading database ... 10%
(Reading database ... 15%
(Reading database ... 20%
(Reading database ... 25%
(Reading database ... 30%
(Reading database ... 35%
(Reading database ... 40%
(Reading database ... 45%
(Reading database ... 50%
(Reading database ... 55%
(Reading database ... 60%
(Reading database ... 65%
(Reading database ... 70%
(Reading database ... 75%
(Reading database ... 80%
(Reading database ... 85%
(Reading database ... 90%
(Reading database ... 95%
(Reading database ... 100%
(Reading database ... 267481 files and directories currently installed.)
Preparing to unpack .../nuget_5.5.0.6319.bin-0xamarin1+ubuntu1804b1_all.deb ...
Unpacking nuget (5.5.0.6319.bin-0xamarin1+ubuntu1804b1) ...
Setting up nuget (5.5.0.6319.bin-0xamarin1+ubuntu1804b1) ...
##[debug]Exit code 0 received from tool '/usr/bin/pwsh'
##[debug]STDIO streams have closed for tool '/usr/bin/pwsh'
##[debug]task result: Failed
##[error]PowerShell wrote one or more lines to the standard error stream.
##[debug]Processed: ##vso[task.issue type=error;]PowerShell wrote one or more lines to the standard error stream.
##[debug]Processed: ##vso[task.complete result=Failed;]PowerShell wrote one or more lines to the standard error stream.
##[error]
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
##[debug]Processed: ##vso[task.issue type=error;]%0AWARNING: apt does not have a stable CLI interface. Use with caution in scripts.%0A%0A
Finishing: Execute Unit Tests and Build Package
代理Windows-2019存在一个已知问题,Windows-2019代理上安装的powershell会将警告消息写入错误流,并返回1作为退出代码,这会导致任务失败 有关更多信息,请查看此报告中的类似问题。您可以加入该线程,也可以为此案例打开一个新问题 如上所述,您可以尝试使用AzurePipelines/windows-2016代理或设置
ErrorActionPreference=Continue
作为解决方法
如果您使用的是内部代理。您还可以尝试在本地代理计算机上手动升级powershell版本
更新:
我进行了测试,如果failOnStderr设置为false,脚本将成功运行。
如果failOnStderr为false,则任务将依赖退出代码来确定失败。否则,如果将任何错误写入错误管道,或者将任何数据写入标准错误流,则此任务将失败
由于问题是由powershell将警告写入错误流引起的,因此可以通过向脚本添加2>&1来重定向输出流
sudo apt install nuget 2>&1
看一看
apt
是为最终用户(人类)设计的,其输出可能会在不同版本之间更改。这就是我学到的。实际上,我已经从使用apt切换到使用apt get,但留下了一个问题,因为我仍然想了解如何在PowerShell/pipeline中处理这样的输出。这是一个Linux托管的agentHi@RobbVandaveer,这是安装在代理上的当前版本PowerShell的问题。我进行了测试,如果failOnStderr
设置为false
,脚本将成功运行。您还可以使用'2>&1'重定向powershell错误流。请查看以上更新,了解您的回复。我认为这两种解决方案中的任何一种都存在一个问题,即如果发生实际错误,管道不会失败。这是正确的语句吗?否,如果failOnStderr设置为false,任务将依赖退出代码来确定失败。否则,如果将任何错误写入错误管道,或者将任何数据写入标准错误流,则此任务将失败。而sudo apt install nuget 2>&1
只会重定向此命令的输出流,它不会影响其他脚本。