我在Azure DevOps中具有管道,在其中每晚执行一次加特林加载测试方案。如果上次的总运行时间大于以前的时间(例如31.03.2020的总运行时间为10分钟,01.04.2020的总运行时间为12分钟),我如何在Azure中(以及也在电子邮件上)获得通知如果我想得到通知)?
1,设置新管道并配置构建完成触发器
转到
获取来源->选中
单击代理作业->选中允许脚本访问OAuth令牌
,以允许脚本调用rest api转到触发器选项卡->构建完成
->单击添加将测试管道选择为触发器构建2,添加脚本任务以调用Build rest api并计算持续时间。和store the result to a variable。检查下面的powershell脚本:$url = "$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_apis/build/builds/$(Build.TriggeredBy.BuildId)?api-version=5.1"
echo $url
$result = Invoke-RestMethod -Uri $url -Headers @{authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"} -ContentType "application/json" -Method get
# calculate the totaltime of the newest build
$time = [datetime]$result.finishTime - [datetime]$result.startTime
$thisBuild= $time.TotalMinutes
# get the last build
$lasturl = "$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_apis/build/builds?definitions=$(Build.TriggeredBy.DefinitionId)&resultFilter=succeeded&`$top=2&api-version=5.1"
$lastResult =Invoke-RestMethod -Uri $lasturl -Headers @{authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"} -ContentType "application/json" -Method get
#Caculate the totaltime of the last build
$lasttime = [datetime]$lastResult.value[1].finishTime - [datetime]$lastResult.value[1].startTime
$lastBuild = $lasttime.TotalMinutes
#Store the result to varialbe isLonger
if($thisBuild -ge $lastBuild ){ echo "##vso[task.setvariable variable=isLonger]True" }
3,添加发送电子邮件任务以发送电子邮件。并将自定义条件设置为eq(variables.isLonger, 'True')
。因此只有在总时间更长时才执行此任务。
希望以上帮助!