背景:我有一个 E2E 任务,通常需要 45 分钟,但有时开发服务器无法启动(Angular 存储库中报告的问题,尚无可用的解决方案,而且似乎是几年前的问题),因此 E2E 在 2- 后失败3分钟
我想做的:让E2E使用
retryCountOnTaskFailure
重试,但前提是开发服务器无法启动(发生在前5分钟),但如果真正的测试失败则不重试
有两个条件我可以使用其中任何一个,但我不知道如何将它们添加到
azure.yml
文件中
Error: Web server failed to start in 300000ms. This can be configured in cypress.config.ts.
我的问题如何写这些条件之一
恐怕没有现成的方法可以根据错误消息或任务运行时间来使用
retryCountOnTaskFailure
。
为了满足您的需求,我建议您可以在E2E任务之后添加一个powershell任务,然后运行Rest API(Timeline - Get)来遍历之前任务中的错误消息。
然后,您可以使用logging命令设置管道变量来确定E2E任务是否需要重新运行。
最后,您需要在powershell任务后添加一个额外的E2E任务,以根据条件重新运行该任务。
这是一个例子:
PowerShell 脚本示例:
$token = "$(PAT)"
$url="https://dev.azure.com/{Organizationname}/{ProjectNmae}/_apis/build/builds/$(build.buildID)/timeline?api-version=7.0"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get -ContentType application/json
ForEach( $issues in $response.records.issues )
{
if($issues.type -eq "error")
{
$errormessage = $issues.Message
if($errormessage.Contains("Web server failed to start in 300000ms"))
{
echo "##vso[task.setvariable variable=IsRequireRetry]true"
}
}
}
Yaml 示例:
steps:
- task: E2E Task
continueOnError: true
- powershell: |
$token = "$(PAT)"
$url="https://dev.azure.com/{OrganizationName}/{ProjectName}/_apis/build/builds/$(build.buildID)/timeline?api-version=7.0"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get -ContentType application/json
ForEach( $issues in $response.records.issues )
{
if($issues.type -eq "error")
{
$errormessage = $issues.Message
if($errormessage.Contains("Web server failed to start in 300000ms"))
{
echo "##vso[task.setvariable variable=IsRequireRetry]true"
}
}
}
displayName: 'Check Error'
- task: E2E Task
condition: eq(variables['IsRequireRetry'], 'true')
retryCountOnTaskFailure: 5
注意: 您需要将
continueOnError: true
添加到第一个 E2E 任务中。
结果:
当E2E任务中的错误包含该字符串时,将重新运行该任务。
否则会跳过重新运行的任务。