How to use the dotnet-pack --version-suffix with csproj?
A little late to the party, but according the dotnet pack documentation examples, you can use this MSBuild property:
dotnet pack -p:PackageVersion=2.1.0
And I've confirmed that you can use it with pre-release versions as well (e.g., 2.1.0-preview
).
This allows people to set the <Version>
element in the .csproj file (and use the IDE they want) and then override it during the build process. At my company, we have a Powershell script that invokes dotnet pack
and sets the version number based on which branch the code was built from. If it's from the dev branch, then we add -alpha
, but if it's from the master branch, then we don't add anything.
According the documentation, the Version
property override the version on packing, instead, use the VersionPrefix
.
<PropertyGroup>
<VersionPrefix>1.0.0</VersionPrefix>
</PropertyGroup>
And use the command to pack solution:
dotnet pack --version-suffix beta
Optionally you can set VersionPrefix
and VersionSuffix
in .csproj
file.
<PropertyGroup>
<VersionPrefix>1.0.0</VersionPrefix>
<VersionSuffix>alpha</VersionSuffix>
</PropertyGroup>
The --version-suffix
argument simply sets the $(VersionSuffix)
msbuild parameter. From the dotnet pack
documentation:
With the project's version suffix configured as $(VersionSuffix) in the .csproj file, pack the current project and update the resulting package version with the given suffix:
dotnet pack --version-suffix "ci-1234"
Now, I think that the --version-suffix
only works with the <VersionPrefix>
value in the csproj and also fails to provide a mechanism for setting the version prefix.
You can override any msbuild parameter via dotnet
calls by using the /p:Parameter=Value
syntax. If you're invoking this from a shell terminal, such as git bash, you'll need to enter this as //p:Parameter=Value
.
Now, I've actually found it to be much easier to just avoid the entire prefix/suffix shenanigans as it has always seemed fragile to me. I instead just simply use the <Version>
property defined in my csproj as follows:
<Version>$(Version)</Version>
EDIT: I'm not sure if this behavior has changed or if
dotnet
always supported this, but it appears that the above csproj change is not required to properly set the version duringdotnet pack
operations. This actually makes tons of sense, since the xml tags in a csproj file are simply setting msbuild properties and the/p:Property=Value
syntax also sets those properties.
And then for your automated builds, you can resolve your prefix/suffixes as you wish and then combine them with a -
to produce the full version string. Another requirement not documented is that the version suffix must begin with a character to be semver compliant. A common suffix is something like ci-<unix-timestamp>
.
prefix=<prefix-value>
suffix=<suffix-value>
dotnet pack //p:Version="$prefix-$suffix" path/to/csproj