任务控制选项 - 自定义条件 - 在先前失败或超时时运行任务

2024-02-04

是否有一个选项可以设置自定义条件来测试上一个任务是否失败或超时?

目前,我正在使用Only when a previous task has failed当任务失败时它会起作用。如果任务超时,则不会将其视为错误并会跳过。

那么我需要一个自定义条件,比如or(failed(), timedout())。是否可以?

Context

我们有这个间歇性的问题npm install我们无法找到原因的任务,但它会在下一次作业运行时得到解决,因此我们正在寻找重试功能。部分解决方案是复制npm install并使用控制选项,但它不适用于所有“失败”情况。 @Levi Lu-MSFT 提供的解决方案似乎可以满足我们所有的需求(它会重试),但遗憾的是它没有解决问题,第二行重复任务也失败了。

错误示例:

20741 error   stack: 'Error: EPERM: operation not permitted, unlink \'C:\\agent2\\_work\\4\\s\\node_modules\\.staging\\typescript-4440ace9\\lib\\tsc.js\'',
20741 error   errno: -4048,
20741 error   code: 'EPERM',
20741 error   syscall: 'unlink',
20741 error   path: 'C:\\agent2\\_work\\4\\s\\node_modules\\.staging\\typescript-4440ace9\\lib\\tsc.js',
20741 error   parent: 's' }
20742 error The operation was rejected by your operating system.
20742 error It's possible that the file was already in use (by a text editor or antivirus),
20742 error or that you lack permissions to access it.

or

21518 verbose stack SyntaxError: Unexpected end of JSON input while parsing near '...ter/doc/TypeScript%20'
21518 verbose stack     at JSON.parse (<anonymous>)
21518 verbose stack     at parseJson (C:\agent2\_work\_tool\node\8.17.0\x64\node_modules\npm\node_modules\json-parse-better-errors\index.js:7:17)
21518 verbose stack     at consumeBody.call.then.buffer (C:\agent2\_work\_tool\node\8.17.0\x64\node_modules\npm\node_modules\node-fetch-npm\src\body.js:96:50)
21518 verbose stack     at <anonymous>
21518 verbose stack     at process._tickCallback (internal/process/next_tick.js:189:7)
21519 verbose cwd C:\agent2\_work\7\s
21520 verbose Windows_NT 10.0.14393
21521 verbose argv "C:\\agent2\\_work\\_tool\\node\\8.17.0\\x64\\node.exe" "C:\\agent2\\_work\\_tool\\node\\8.17.0\\x64\\node_modules\\npm\\bin\\npm-cli.js" "install"
21522 verbose node v8.17.0
21523 verbose npm  v6.13.4
21524 error Unexpected end of JSON input while parsing near '...ter/doc/TypeScript%20'
21525 verbose exit [ 1, true ]

有时也超时


可以添加自定义条件。如果您希望在上一个任务失败或跳过时执行该任务,您可以使用自定义条件not(succeeded())

但是上面的自定义条件有一个问题,在多任务场景下不起作用。

例如,有 A、B、C 三个任务。预期行为是任务 C 仅在任务 B 失败时执行。但实际行为是,即使任务 B 成功,当任务 A 失败时,任务 C 也会被执行。检查下面的屏幕截图。

解决上述问题的方法是添加一个脚本任务来调用Azure DevOps Restful API https://learn.microsoft.com/en-us/rest/api/azure/devops/build/timeline/get?view=azure-devops-rest-5.1#taskresult获取任务 B 的状态并使用此表达式将其设置为变量echo "##vso[task.setvariable variable=taskStatus]taskStatus".

对于下面的示例,添加一个 powershell 任务(您需要将此任务的条件设置为Even if a previous task has failed, even if the build was canceled始终在任务 C 之前运行此 powershell 任务)以运行以下内联脚本:

$url = "$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_apis/build/builds/$(Build.BuildId)/timeline?api-version=5.1"

$result = Invoke-RestMethod -Uri $url -Headers @{authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"} -ContentType "application/json" -Method get

#Get the task B's task result  
$taskResult = $result.records | where {$_.name -eq "B"} | select result  

#set the Task B's taskResult to variable taskStatus
echo "##vso[task.setvariable variable=taskStatus]$($taskResult.result)" 

为了使上述脚本可以访问访问令牌,您还需要单击代理作业并选中选项Allow scripts to access the OAuth token。请参阅下面的屏幕截图。

最后你可以使用自定义条件and(not(canceled()), ne(variables.taskStatus, 'succeeded'))对于任务 C。仅当任务 B 未成功时才应执行任务 C。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

任务控制选项 - 自定义条件 - 在先前失败或超时时运行任务 的相关文章

随机推荐