Jay
Jay

Reputation: 2302

Is there a way to use dotnet command to determine the version number of the project build?

How can I retrieve the version number of a dotnet project using a GitHub Actions? If I can determine the version using a command line, I can execute that same command within the GitHub workflow and store the result in a variable. For instance, if my project contains the following:

<PropertyGroup>
<VersionPrefix>3.0.0</VersionPrefix>
<VersionSuffix>beta</VersionSuffix>
</PropertyGroup>

I want to output 3.0.0-beta as the version number.

UPDATED

After feedback from @Lex Li, I added the following conditional check

<Project>

  <PropertyGroup>
    <VersionPrefix>3.0.0</VersionPrefix>
    <VersionSuffix>beta</VersionSuffix>
    <VersionSuffix Condition="'$(VersionSuffix)'!='' AND '$(BuildNumber)' != ''">$(VersionSuffix)-$(BuildNumber)</VersionSuffix>
  </PropertyGroup>

  <Target Name="MyCustomTarget">
    <Message Text="$(VersionPrefix)-$(VersionSuffix)" Importance="High" />
  </Target>

  <Target Name="MyCustomTarget" Condition="'$(VersionSuffix)'!='' AND '$(BuildNumber)' != ''">
    <Message Text="$(VersionPrefix)-$(VersionSuffix)" Importance="High" />
  </Target>

</Project>

The condition check gives me the following error

error MSB4057: The target "MyCustomTarget" does not exist in the project.

If I remove the Condition="'$(VersionSuffix)'!='' AND '$(BuildNumber)' != ''" from the second target, I get 3.0.0-beta as expected.

Upvotes: 0

Views: 202

Answers (1)

Lex Li
Lex Li

Reputation: 63264

A few simple steps.

Define a custom target

  1. Add a Directory.Build.props file to your project folder, so it will be automatically imported by MSBuild.
  2. Use the following contents to add a custom target to print out the full version string you wanted. 
<Project>
    <Target Name="MyCustomTarget">
        <Message Text="$(VersionPrefix)-$(VersionSuffix)" Importance="High" />
    </Target>
</Project>

Call this target

Now at command prompt you can run the following

$ dotnet msbuild /t:MyCustomTarget /nologo
3.0.0-beta

/nologo is needed to suppress the standard MSBuild output like "MSbuild version ... for .NET".

Upvotes: 1

Related Questions