首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >任务控制选项-自定义条件-在上次失败或超时时运行任务

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

Stack Overflow用户
提问于 2020-03-23 18:04:24
回答 2查看 3.3K关注 0票数 1

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

目前,我使用的是当任务失败时工作的Only when a previous task has failed。如果任务超时,那么它不会被认为是错误,而是被跳过。

那么,我需要一个自定义条件,类似于or(failed(), timedout())。有可能吗?

上下文

我们在npm install任务中遇到了这个间歇性的问题,我们找不到原因,但是在下一个作业运行时它已经解决了,所以我们正在寻找一个重试功能。部分解决方案是复制npm install并使用Control,但它并不适用于所有“失败”情况。由@Levi MSFT给出的解决方案似乎满足了我们所有的需求(确实有重试),但遗憾的是,它并没有解决问题,第二线重复任务也失败了。

样本错误:

代码语言:javascript
复制
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.

代码语言:javascript
复制
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 ]

有时候时间也不多了

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-03-24 04:29:04

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

但是,上面的自定义条件存在问题,在多任务场景中不能工作。

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

解决上述问题的方法是添加一个脚本任务,调用azure devops restful api以获取task的状态,并使用此表达式echo "##vso[task.setvariable variable=taskStatus]taskStatus"将其设置为变量。

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

代码语言:javascript
复制
$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。请参阅下面的截图。

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

票数 4
EN

Stack Overflow用户

发布于 2020-03-23 19:36:45

虽然我没有找到一个内置函数来检测某个构建步骤是否超时,但是您可以尝试通过变量来模拟这个过程。

考虑以下YAML管道声明:

代码语言:javascript
复制
steps:
- script: |
    echo Hello from the first task!
    sleep 90
    echo "##vso[task.setvariable variable=timedOut]false"
  timeoutInMinutes: 1
  displayName: 'A'
  continueOnError: true

- script: echo Previous task has failed or timed out!
  displayName: 'B'
  condition: or(failed(), ne(variables.timedOut, 'false'))

第一个任务(A)设置为1分钟后超时,但内部脚本模拟长时间运行的任务(sleep 90) 1.5分钟。结果,任务超时,timedOut变量是而不是设置为false。因此,任务B的条件计算为true并执行。如果将sleep 90替换为exit 1以模拟任务A失败,也会发生同样的情况。

另一方面,如果任务A成功,则任务B的两个条件部分都不为true,整个任务B被跳过。

这是一个非常简单的例子,但它演示了您可以进一步调整以满足管道需求的想法。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60819191

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档