Sdk: [Feture] Let CLI extract package version from the project.

Created on 15 Nov 2020  路  7Comments  路  Source: dotnet/sdk

These days project file contains properties which relates to Nu-Get packaging. Among those properties there is <Version> element.

In most cases of CI/CD we need to increment minor part or add some other suffix to the version. Hence, it would be nice if dotnet pack could return the value of <Version> element for further transformation.

ex: Project contains <Version>1.2</Version> and dotnet pack --get-package-version returns 1.2, so I can simply add .$CI_PIPELINE_IID to get the version of the package for packing and publishing.

All 7 comments

There are various ways to do this. You could pass the pipeline ID on the command line, for example: -p:PipelineID=$CI_PIPELINE_IID, and then use that in the project file to build the version number:

<Version>1.2-$(PipelineID)/</Version>

You can also set VersionPrefix in the project and pass in VersionSuffix and it will combine them automatically.

Finally, there are tools like GitVersion and Nerdbank.GitVersioning which can help with setting the version.

@dsplaisted Thanks for the hint, it works even for dotnet pack --no-build The only issue is that dotnet paket pack does not accept -p option. =(

Btw, it looks -p switch is not documented, I do not find it in description returned by dotnet pack --help

Many dotnet commands end up being handled by MSBuild, and they forward any arguments they don't process to MSBuild. The -p or /p command sets an MSBuild property. You can see the MSBuild command line options with dotnet msbuild /?.

@KathleenDollard @wli3 @sfoslund Do we have documentation anywhere that describes this? I didn't see it quickly browsing our dotnet command docs.

@voroninp MSBuild also has access to environment variables, so if paket doesn't support passing custom properties you could do something like $(CI_PIPELINE_IID) in your msbuild logic to read that environment variable.

@dsplaisted I suspect paket somehow ignores MSBuild altogether for packaging and just parses proj file as xml. But it's a problem of paket =)

Anyway, thanks for the help!

But "backward" functionality is still nice to have ;-)
https://stackoverflow.com/questions/65116782/is-it-possible-to-extract-the-value-of-element-in-csproj-file-with-dotnet-cli

I gave a sample of how to do this on the StackOverflow question.

Without define variable build was failing, so I had to add condition:

<Version Condition=" '$(Patch)' != '' ">1.0.$(Patch)</Version>
Was this page helpful?
0 / 5 - 0 ratings