如何防止在新的TFS build vNext 2015中重复构建

时间:2016-07-04 21:17:17

标签: azure-devops tfsbuild

在旧的TFS构建定义(XAML构建)中,如果代码没有被更改,我可以选择让Team Build不再构建相同的代码。XAML Build Definition

但是,在新的XML Build(vNext)中,我没有看到此选项,构建服务器会一次又一次地构建代码。有没有什么方法可以实现我们在旧的XAML构建定义中所具有的相同行为,如果它没有被更改,又不再构建代码? xml build

感谢您的帮助......

[ 更新

我写了一个powershell脚本来实现Eddie在答案中建议的内容

Write-Debug -Message ("System URL: "+$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)
Write-Debug -Message ("Team Project Name: "+$env:SYSTEM_TEAMPROJECT)
$baseURI=$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI+"DefaultCollection/"+$env:SYSTEM_TEAMPROJECT+"/_apis/"
$oauth= @{Authorization="Bearer $env:SYSTEM_ACCESSTOKEN"}
Write-Debug -Message ("Base URL: "+$baseURI)
$BuildDefList=(Invoke-RestMethod -Uri $baseURI/build/definitions?api-version=2.0 -Headers $oauth -Method Get).value | where {$_.name -like "*Build"}

Write-Host -ForegroundColor Cyan -Object ("Found "+$BuildDefList.count+" build definition(s)")

foreach ($buildDef in $BuildDefList)
{   
    Write-Host -ForegroundColor Yellow -Object ("Checking Build Definition: "+$buildDef.name+" ...")
    $id=$buildDef.id
    $build= Invoke-RestMethod -Uri $baseURI/build/builds?api-version=2.0"&"definitions=$id"&"`$top=1"&"resultFilter=succeeded -Headers $oauth -Method Get
    $changeset= $build.value.sourceVersion
    "The changeset used in the lastest build in this build definition is "+$changeset

    $sourceBranch=$build.value.sourceBranch
    $latestChangeset=(Invoke-RestMethod -Uri $baseURI/tfvc/changesets?api-version=1.0"&"searchCriteria.itemPath=$sourceBranch"&"`$top=1 -Headers $oauth -Method Get).value.changesetId   
    "The latest changset need to build is "+$latestChangeset

    if($latestChangeset -and $changeset -lt $latestChangeset)
    {
        Write-Host -ForegroundColor Green -Object ("The build is old. Queuing new build for "+$buildDef.name+" now")
        $body= @{Definition = @{ Id=$id}} | ConvertTo-Json
        Invoke-RestMethod -Uri $baseURI/build/builds?api-version=2.0 -Body $body -ContentType "application/json" -Headers $oauth -Method Post
    }
}

1 个答案:

答案 0 :(得分:3)

在vNext构建定义中没有选项可以配置它。在VSTS用户语音上为此功能提交了多项功能请求。

Add an vNext option to build only if something has changed to scheduled build trigger

TFBuild 2015: Run sheduled build only when source has changed

要立即实现此功能,您可以创建另一个构建定义并将其设置为预定构建。在构建定义中,只需添加power-shell脚本任务即可比较当前源版本和以前的构建源版本。如果当前版本比构建版本更新,则触发实际构建定义以通过Rest API构建代码。