Reputation: 2302
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.
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
Reputation: 63264
A few simple steps.
Directory.Build.props
file to your project folder, so it will be automatically imported by MSBuild.<Project>
<Target Name="MyCustomTarget">
<Message Text="$(VersionPrefix)-$(VersionSuffix)" Importance="High" />
</Target>
</Project>
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