简体   繁体   中英

TFS2015 vNext Build + PowerShell Customization — Exception calling Save: TF215070: The build URI is not valid

Was previously using TFS2013 and XAML-based builds. Had created a number of custom tasks for the XAML builds in C#, including one to set the build number programmatically through the TFS .NET Client API libraries (not the REST API). Have now moved to TFS2015 and trying to move to vNext-based builds. Trying to convert our C#-based customizations to the new build process by re-coding as PowerShell scripts. I am trying to use the following script to programmatically set the build number (as we did previously via C#):

# Step 1: load the TFS assemblies
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Build.Common")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Build.Client")

# Step 2: get the collection
$collection=[Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)

# Step 3: get the build server
$buildServer=$collection.GetService([Microsoft.TeamFoundation.Build.Client.IBuildServer])

# Step 4: get the details for the currently running build
$buildDetail = $buildServer.GetBuild($env:BUILD_BUILDURI)

# Step 5: update the build number
$buildDetail.BuildNumber = "1.2.3.4-MyBuild"

# Step 6: save the changes to the build details
$buildDetail.Save()

Everything seems to work just fine until step 6, when I try and save the change made to the buildDetail.BuildNumber . When the "Save()" method is called, the following error is generated:

Exception calling "Save" with "0" argument(s): "TF215070: The build URI vstfs:///Build/Build/40177 is not valid. Make sure that this build exists and has not been deleted.

As best I can tell, step 4 is returning an instance that implements the Microsoft.TeamFoundation.Build.Client.IBuildDetail interface (as I expected it would). Also, clearly the build URI is valid as it is specifically used to load the build information in that same step. Again, this logic mimics the same logic we use in our C#-based XAML customizations, just rewritten under PowerShell.

Searching the internet, I can find nothing related to this error and cannot figure out why I would be receiving it. I did find a (much more complex) version of what I am trying to do here: https://github.com/voice4net/build-scripts/blob/master/ApplyVersionFromSourceControl.ps1 . While I haven't tried using this script directly, it appears to be doing basically the same thing and, presumably, worked for its author.

Is there anyone out there that can help me to understand this error, why I am getting it, and, ideally, how to fix it?

SIDE NOTE: This is not Microsoft's hosted TFS; this is a traditional TFS system that our company has installed internally.

The methods in the TFS SOAP object model (such as Microsoft.TeamFoundation.Build.Common ) only apply to the XAML build system. You're targeting the wrong build system, which is why you're having problems.

Use the appropriate object model (either the REST APIs directly , or the C# REST API wrappers ).

It seems like you are trying to use the incorrect API to update your build number. You should be using the WebAPI/REST portion of the client DLLs.

That said I highly suggest you create a custom task in pure PowerShell or TypeScript in order to achieve your goal.

Here is an example of how to do so in TypeScript:

console.log(`##vso[build.updatebuildnumber]${newBuildNumber}`);

or in PowerShell:

Write-VstsUpdateBuildNumber -Value $newBuildNumber

You can check the reference here on how to create build tasks: https://github.com/Microsoft/vsts-task-lib

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM