Teamcity 9: How to add git short hash into assembly info patcher
If you want to write this to the Assembly Info field it can be done, but it requires a separate build configuration to generate the build number. The sole purpose of this step is to create the build number that has the hash appended to it.
1. Create a build configuration to generate the short hash
2. Add a step to generate the hash
3. Add a parameter to store the hash
4. Add a second build configuration and add a dependency to the first one
5. You can now consume the parameter in the dependent step
6. At this point you can use it in the assembly info patcher
The alternative to this is to write your build number back to Git using the VCS labeling build feature.
Hope this helps.
Related videos on Youtube
Neil P
SQL Server and business intelligence developer, also dabbles in C#
Updated on May 25, 2020Comments
-
Neil P almost 4 years
I'm trying to use assembly info patcher to create a version number something like:
1.2.3.1a3c19e
where the last bit is the git short hash.
I've tried using a powershell script build step to create the short hash (as I cant find a variable that has it) and adding this to a system variable but this build step appears to run after assembly info patcher, so isn't much use.
-
Matt almost 9 yearsIt is true that you can't write this hash to certain fields. I assume the goal here is to tie up the assembly version to the git commit, in which case it would make sens to write to the Assembly informational Version as this accepts a string.
-
Alexey Andrushkevich almost 9 yearsJust want to add that separated build configuration is needed because assembly info patcher is running after changes from VCS have been collected and before steps start to execute. There is a way how you can get rid of separated build configuraiton for the example above by using powershell script which go thru all AssemblyInfo.cs files and does replacement instead of assembly info patcher:
(Get-childitem -include AssemblyInfo.cs -recurse) | Foreach-Object { Set-Content -Encoding UTF8 $_ ((Get-content $_) -replace "1.0.0.0", "1.0.0-%GitShortHash%")}
. -
Matt almost 9 years@Alexey. I agree you could write a script to do this in powershell, but I would be very wary of dropping this code in as it's more likely to break the build. By default, AssemblyInfo.cs contains AssemblyVersion and AssemblyFileVersion which would fail to compile if you used the short hash. The AssemblyInfo.cs would need some "prepping" first for this script to not break the build.
-
Alexey Andrushkevich almost 9 years@DevOps, you are right for 100%. I have left my comment and example of script just to make note that it's possible to do this without adding another build configuration what can be problematic for smbd due to license limitations. The powershell script I've posted has to be updated in order to replace version for required attribute only.
-
flipdoubt about 5 yearsHow/when does the PS variable get into the
GitShortHash
parameter in Step 3, as you've left the value empty? Does the parameter in step 3 need to be an environment variable, configuration parameter, or system property to get the value? Mine is always empty even though the build log shows##teamcity[setParameter name='GitShortHash']
.