Get TFS Source Version Changeset and apply to AssemblyInfo - powershell

I am trying to get the Source Version Changeset number from TFS. I am using a PowerShell script to add the AssemblyInformationalVersion. I have tried using $env:TF_BUILD SOURCEGETVERSION but this dosen't work it just return blank. We are using TFS 2012 with a custom build template calling the PowerShell scripts (http://www.colinsalmcorner.com/post/build-script-hooks-for-tfs-2012-builds)
I really want to keep everything within the PowerShell Script

No, this can't be achieved simply by an environment variable in a powershell script . You need to create a custom task, follow the steps below to have a try:
Get the latest changeset number in your TFS version control. You can
just get it by using TFS API. Please see this thread for
detailed information: Programmatically retrieve the latest changeset number available in a workspace
You can write another small program to modify your FileVersion field
in the .rc file be the changeset number you get in Setp1.
Or you can try the method descried by Ivan's answer in this question Versioning .NET builds
Add more similar quesitons in SO for your reference:
Getting TFS to put the changeset in the assembly version
TFS and msbuild version number with last changeset
Take control of assembly numbering during a tfs build

The answer is in the article you referred to(The Challenge 2 – Environment Variables).
When I created a script for a 2013 build to version the assemblies, I
relied on the fact that the 2013 build sets some environment variables
that you can use in your scripts.You can see I’m getting
$env:TF_BUILD_BUILDNUMBER. Well, in the 2012 workflow, these variables
aren’t set, so you have to add an activity to do it.
That's why $env:TF_BUILD SOURCEGETVERSION returns blank, so you just need to follow the steps in Challenge 2 to set it.

Related

TFS 2012 build changeset version in PowerShell script

We have a TFS 2012 instance and a build agent w/ VS 2017. I am trying to modify a XAML build template to create a nuget package from a given project. I am required to put the changeset number in the description of the package but I am unable to get the correct value.
I have an InvokeProcess activity in the AgentScope of my template which runs a PowerShell script to pack the package. I have tried getting the TF_BUILD_* environment variables but none seem to be populated. I also tried BUILD_SOURCEVERSION with no luck. At the moment I am passing in the BuildDetail.SourceGetVersion value but that is returning the current changeset for the entire team project, not the specific project I am building.
What am I doing wrong? Are those variables only on the TFS server and not on the agent (build server)?
TFS 2012 build changeset version in PowerShell script
According to the article Build Script Hooks for TFS 2012 Builds:
When I created a script for a 2013 build to version the assemblies, I
relied on the fact that the 2013 build sets some environment variables
that you can use in your scripts.You can see I’m getting
$env:TF_BUILD_BUILDNUMBER. Well, in the 2012 workflow, these variables
aren’t set, so you have to add an activity to do it.
That the reason why you tried getting the TF_BUILD_* environment variables but none seem to be populated.
So, to resolve this issue, you can just to follow the steps in Challenge 2 to set it.
Or, you can use TFS API to get the latest changeset number in your TFS version control:
Please see this thread for detailed information:
Programmatically retrieve the latest changeset number available in a workspace
Then you can write another small program to modify your description field in the .cs file be the changeset number you get in above.
Hope this helps.

Get only checked in files from TFS as part of TFS build

I have Continuous integration checked for my folder in TFS which triggers a build in TFS2015 . Now as part of by Build Definition, I want to add a step which would identify and pull only those files which were checked in as part of the changeset which triggered the current build and copy them to a target location.
A powershell script may be? Help please
You can add a Powershell script task in your build definition to do this. A simple script for your reference:
$changesetid = $Env:Build_SourceVersion
$TFSURI = $Env:System_TeamFoundationCollectionUri
$tfs = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($TFSURI)
$vcs = $tfs.GetService("Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer")
$cs = $vcs.GetChangeset($changesetid)
foreach ($change in $cs.Changes)
{
$change.Item.DownloadFile('D:\a\test\' + $change.Item.ServerItem.Substring(1))
}
This code just get the changed items, if the changeset includes other changes like deletion, you may need to add code to check this.
And you also need to import the TFS Client Library to use this script, refer to this link for details: PowerShell and TFS: The Basics and Beyond.
This isn't possible in TFS 2015. TFS 2017 has introduced the ability to "don't sync sources", after which you can perform your own get operation. You could try cloaking the whole repository on the repository tab and then creating a CI TFVC Include trigger definition on the trigger tab to recreate this behavior.
Then you can use the tfpt getcs option to get the changes of the changeset. You should be able to get the version from the builds Build.SourceVersion variable. You can get tfpt by installing the Team Foundation Server Power Tools 2015 and Team Explorer 2015 on the build server.

VSTS (TFVC): customising builds and hosting NuGet packages

We are still using an in-house TFS 2012 server but I'm now looking at moving to VSTS. I have a couple of questions though:
Years ago I customised our build process template to perform a number of additional tasks, and I was wondering if VSTS builds can be customised in a similar way, specifically to do what we currently do:
Run StyleCop
Change the version number in every AssemblyInfo.cs file in the solution prior to building (the major and minor numbers are specified in the build definition).
Run a batch file at the end of the build which runs an InnoSetup script to create a "setup.exe" file (the batch filename is againspecified in the build definition).
(The first two are (I think) DLLs that came from the now defunct tfsbuildextensions.codeplex.com site).
Second question: we currently have an in-house NuGet repository. Am I right in saying I could host this on VSTS instead? And will that be accessible both to VSTS builds and our dev team?
The newer build system is fully extensible. You can simply add "Command Line", "Batch File", or "PowerShell" tasks to run whatever commands you'd like during your build process. Any customizations you made to your XAML build process templates will have to be ported manually, but it's entirely possible that someone has created free extensions that are available to install from the VSTS marketplace that replicate the behavior you're seeking.
VSTS supports package management feeds. It's an extension and requires additional licensing, but the simple answer to your question is "yes".

Update Changed Assembly Version Number TFS Build

I'd like to be able to automatically update the assembly version off all changed projects on a TFS check-in. We'd like to do this as part of our gated check-in so that developers don't have to remember to manually update those numbers on every check-in.
My current approach would be to:
Determine what projects contains changes on check-in
Check out the AssemblyInfo.cs in each project with changes
Increment the version number in each AssemblyInfo.cs file
Begin build process
I assume there is a way to accomplish this using a combination of PowerShell and customizing the TFS build template, but have little familiarity with either. Any help on the matter would be greatly appreciated.
Use the script located here as a pre-build script in your build template.
The script will change all AssemblyInfo.cs files that are found with the build number.
How to update assembly version of your projects using PowerShell scripts. A very good guide.
The answer is bit late but you can find a good guide here , Have a look into that

Get Changesets Associated With Build

In TFS (2013 Update 4) I am trying to write a PowerShell script to copy modified SQL files that are tied to a build. I can get and copy the appropriate files if I know the changeset number, which will often be enough (I can use the TF_BUILD_SOURCEGETVERSION environment variable when the build is triggered by a merge). However, occasionally there will be a handful of changesets that are associated with the build in TFS.
Using the Build Number, how do I get a list of Changesets?
You need to use your build number to find the previous build number. You will then have both a start changeset (from previous build) to current changeset (current build).
You can then walk the gap with the API and find all the intervening changesets.
So I've done this in my last engagement, in essence we solved it by doing a get of all SQL related files EVERY build and produce a csv file that contained information about each file, name, version, and most importantly and MD5 hash of the file. Then with each deployment we create/update/insert into a special deployment table in our DB all SQL "run" against that DB. Then our build script is really just producing the csv file but our deployment script has the intelligence and checks to see if anything as changed in the csv file vs. the target DB and only applies changes (new SQL, changed SQL with new MD5). So we essentially use two scripts. I can't share the scripts but you have the idea. Also I would look at this article by Alexander
Automating SQL Server Database Deployments: Scripting Details where he explains a lot about db migration.