1

In the old TFS build definition(XAML builds), I have an option to make Team Build not build the same code again if the code hasn't been changed.XAML Build Definition

However, in the new XML Build (vNext), I don't see this option and the build server keeps build the code again and again. Is there any way I could achieve the same behavior that we had in the old XAML build definition in term of not building the code again if it hasn't been changed? xml build

Thank for your help...

[UPDATE]

I wrote a powershell script to achieve what Eddie suggested in the answer

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
    }
}
LxL
  • 1,878
  • 2
  • 20
  • 39

1 Answers1

3

There is no option to configure this in vNext build definition. There are several feature requests submitted for this feature on VSTS User Voice.

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

To achieve this feature for now, you can create another build definition and set it to scheduled build. In the build definition, just add a power-shell script task to compare the current source version and previous build source version. If the current version is newer than the built one, then trigger the real build definition to build the code via Rest API.

Eddie Chen - MSFT
  • 29,708
  • 2
  • 46
  • 60
  • I added the powershell script that I wrote per your suggestion. I also have related question and I thought it's better to ask here given the context had been set. My question is: right now I use my PAT in the header of every request. Since this script is run by a build definition. I wonder if I could replace my credential by the account that runs this build definition (e.g. service account)? I think it would be possible otherwise other build task (e.g. tfvc get) wouldn't be able to access to the REST API. Btw, I use VSTS not TFS on premise, and the build agent is on a dedicated machine. – LxL Jul 27 '16 at 06:13
  • @LxL Under the build definition settings, check "Allow Scripts to Access OAuth Token" under "Options" tab. Then you can use the oauth token of the build to access to VSTS. – Eddie Chen - MSFT Jul 27 '16 at 06:17
  • Here is the link of how to use OAuth in script in case other people is also missing it like me https://www.visualstudio.com/docs/build/scripts/index#oauth – LxL Jul 27 '16 at 17:03
  • Please advice me if I need to post this as a new question. So after the script running fine and queue a new build, I got strange path for sourceBranch value "$/705194f5-f797-48ed-ad69-1444580e7d24/Dev6.2" instead of "$/TotalVision/Dev6.2". After further investigation, I realized that string is the guid of TotalVision branch. If I queue a build manually, I got the correct value for sourceBranch "$/TotalVision/Dev6.2". Is this a bug or should I put sourceBranch in the body of my build request to get the sourceBranch correctly? – LxL Jul 28 '16 at 17:50
  • I filed the item under this bug https://connect.microsoft.com/VisualStudio/feedback/details/2986329 – LxL Jul 28 '16 at 18:39