A short post on how to pass TeamCity variables in your Fake build config. Multiple people on the web were looking for this, including myself. I Therefore decided to write a blog post covering the basics.
It is helpful to understand what I ‘m aiming for before we continue. The goal is to include the build counter as part of the version number. This so we can trackback what’s included within the build artifacts. Best practices for .Net Assembly versioning can be found within this MSDN article.
For this blog-post, I will use the following format [Major version].[Minor version].[Build Number]. I’ve simplified the setup by adding the Major and Minor version within the build config. Feel free to move this to any location of your liking. (version file, environment variable etc).
Passing Parameters into FAKE Build.
I prefer wrapping the FAKE initiation within a batch or PowerShell file. Doing so will allow you to add additional logic/validation. In addition, here you can setup environment requirements.
Let’s first look at the batch script:
echo version %1
"tools\NuGet\NuGet.exe" "Install" "FAKE" "-OutputDirectory" "tools" "-ExcludeVersion"
"tools\Fake\tools\Fake.exe" "build.fsx" version=%1
The script passes the batch parameter into FAKE, TeamCity will need to provide the value which will be covered later.
Note: I’ve include Nuget.exe as part of the project. Feel free to point to the version to a shared location. Just make sure the location is reachable from the build server and dev environments..
When calling FAKE, pass in the new parameters along with the fake build config. You can read the value by using getBuildParam or getBuildParamOrDefault.
let version = getBuildParam "version"
Updating the Assembly Info c# file can be done using CreateCSharpAssemblyInfo.
To glue all together, open the team city build config for your project. Within the build config, open the step which calls your batch or PowerShell file.
Given that I am calling a .bat file, I have selected executable with parameters. Within the Command parameters section, select the data you would like to pass.
Well, that was it! As you can see, it’s pretty simple to get this all working. I’m planning to write a detailed guide on how to use Fake Build together with TeamCity. This includes running build flow, MSTest and the creation of NuGet packages.