1

I'm trying to set the version suffix on my DLLs to Git commit id available on Azure DevOps via environmental variable Build_SourceVersion. If that environmental variable is not available, the suffix should be set to random GUID. My Directory.Build.props file looks like this:

<Project>
 <PropertyGroup>
   <VersionPrefix>2.1.0</VersionPrefix>
    <VersionSuffix Condition="'$(Test-Path env:Build_SourceVersion)' == 'True'">$((Get-Variable Build_SourceVersion).Substring(0, 7))</VersionSuffix>
    <VersionSuffix Condition="'$(Test-Path env:Build_SourceVersion)' == 'False'">$([System.Guid]::NewGuid().ToString(N).ToLower().Substring(0, 7))</VersionSuffix>
 </PropertyGroup>
</Project>

Though when I build my DLLs do not get any Product version set. What am I doing wrong here? If I try to set the <VersionSuffix> to $([System.Guid]::NewGuid().ToString(N).ToLower().Substring(0, 7)) my DLLs are labeled with random GUID. If I try $((Get-Variable Build_SourceVersion).Substring(0, 7)) I don't get any errors at least but the combination seems not to be working.

Azimuth
  • 2,599
  • 4
  • 26
  • 33
  • @LeoLiu-MSFT, unfortunately it didn't work. I think that `Build_SourceVersion` is simply not populated in our setup of Azure DevOps... – Azimuth Mar 22 '20 at 15:50
  • Could you output it with command line task in pipeline with `Build.SourceVersion`? You could share a sample to test this issue, since it could not be reproduced in our side. – Leo Liu Mar 24 '20 at 08:19
  • @LeoLiu-MSFT, it outputs empty string – Azimuth Mar 29 '20 at 08:15
  • So, the reason for this issue is not the MSBuild scripts, should be the value of `Build.SourceVersion`. What is your build scource? Git repo or others? – Leo Liu Mar 30 '20 at 01:18
  • Yes, a repo on Github – Azimuth Mar 31 '20 at 14:18

1 Answers1

0

Set VersionSuffix to commit id if availabe

When you want to reference Azure devops build variables from MSBuild, you need to reference the environment variable that the agent sets.

So you can use it in MSBuild using $(Build_SourceVersion) instead of (Get-Variable Build_SourceVersion).

Then we could use following scripts to get the Substring of that value:

  <PropertyGroup>
    <VersionSuffixTest>$(Build_SourceVersion)</VersionSuffixTest>
  </PropertyGroup>

  <PropertyGroup>
    <VersionPrefix>2.1.0</VersionPrefix>
    <VersionSuffix Condition="'$(VersionSuffixTest)' != ''">$(VersionSuffixTest.Substring(0, 7))</VersionSuffix>
  </PropertyGroup>

As test, I use a MSBuild target output that value:

<Target Name="TestTask" AfterTargets="Build">
  <Message Text="$(VersionPrefix)" Importance="high" />
  <Message Text="$(VersionSuffix)" Importance="high" />
</Target>

Then, I could get that value:

enter image description here

Check this thread and this thread for some details.

Hope this helps.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • This doesn't work. I have updated Directory.Build.props to this https://pastebin.com/Nu0FYuun but I get `dev-#####` in output when building on Azure DevOps – Azimuth Mar 04 '20 at 09:19
  • @Azimuth, What is the build result for the custom target `TestTask`? It should not be start with `dev`. And you can try to add output the value `VersionSuffixTest` in that target, and share the log here. – Leo Liu Mar 04 '20 at 09:27
  • 1
    Unfortunately I get `dev` prefixed `VersionSuffix` in the output. Even `Build_SourceVersion` is empty. See https://imgur.com/a/Odp3tck – Azimuth Mar 04 '20 at 09:37
  • @Azimuth, I test your `Directory.Build.props` on my side, it works fine. If the value of `VersionSuffixTest` is empty, that should be not get the value by the `$(Build_SourceVersion)` , could you output it with command line task in pipeline with `Build.SourceVersion`? – Leo Liu Mar 04 '20 at 10:38
  • Unfortunately I have no permission to edit the pipeline but I asked those who have. – Azimuth Mar 04 '20 at 10:53