Azure DevOps build pipeline task to read <Version>
tag from project files and append Build numbers.
The task reads the <Version>
tag from new csproj
and vbproj
SDK-format files into environment variables.
This tool was created to fix an issue with the SDK project format. The new format does not support wildcard in the version and therefore auto-append of build suffix, which was previously supported in the .NET Framework and MSBuild.
Version 1.x runs as a PowerShell task and only works on windows build agents. Version 2.x has been rewritten in TypeScript and Node, so is now cross-platform. Version 2.x was rewritten using NodeJS. Version 3.x was added to support Node20.
A useful article on versions from Andrew Lock
The task will read the <Version>
tag - if this isn't present it looks for <AssemblyVersion>
and finally <VersionPrefix>
values from the project if these are present. If no values are found in these it assumes a version value of 1.0.0
The VersionReaderTask
can be added after the build or the test task to extract the version details from the project. In the example below it runs after the test task and extracts the version from the Ambolt.csproj
file.
The Variables Prefix setting can optionally be specified to differentiate the variable names, for example if you are running VersionReaderTask
several times in a build. For example you might specify a prefix DEMO_
for the Demo porject, and will result in the variables DEMO_VERSION
and DEMO_VERSION_BUILD
.
The Build Prefix is used to specify a separator between the version and the BuildID values - the default value is a dot '.'
. For example, if the version is 1.2.3
and the build ID is 5678
, and no prefix is specified, BUILD_VERSION
will be 1.2.35678
. If you specify .
as the prefix, the BUILD_VERSION
will be 1.2.3.5678
The value is then used in a dotnet pack
task as follows:
Note the Automatic package versioning is set to Use an environment variable
and the Environment variable is set to VERSION_BUILD
value generated by the version reader task.
Add the task to your build pipeline, usually after a build and test task:
- task: conficient.VersionReaderTask.version-reader-build-task.VersionReaderTask@3
displayName: 'Generate build variables'
inputs:
searchPattern: 'Demo/Demo.csproj'
variablesPrefix: DEMO
buildPrefix: '.'
If Demo.csproj
has a version of 1.2.3, for build 5678 the variable DEMO_VERSION_BUILD
would contain 1.2.3.5678
.
The NUGET packaging task would be
- task: DotNetCoreCLI@2
displayName: Pack
inputs:
command: pack
packagesToPack: 'Demo/Demo.csproj'
versioningScheme: byEnvVar
versionEnvVar: 'DEMO_VERSION_BUILD'
Note that the prefix mode
is used in this example, so the task prefixes the build variable with DEMO
. If your build only runs
version reader once, you can omit the prefix and just use VERSION_BUILD
.
Upgraded to Node20 to remove warning. https://aka.ms/node-runner-guidance. I am using version 3.0 so it is still possible to use 2.x for older builds if required, and should not be a breaking change for existing pipelines.
I've upgraded a number of packages and will try to drop xmldom
as this has warnings (TBC).
Reverted changes in 2.3, not working
Updated xmldom
package to 0.5.0 for security issue.
Task rewritten in TypeScript so that it is cross-platform (will work on both Win, Linux and Mac agents). Features and inputs are the same so it should be possible to just change the version for it to work in the same way as 1.x
I have added support for other tags in this release, e.g. <PackageVersion>
and <FileVersion>
.
Possible Breaking Changes:
- version 1.x would read all matching projects if the filter matched more than one, which just overwrites the variable values. I have amended 2.0 to only read the first matching file, and warn if more than one .csproj file was matched.
Versions 2.0 and 2.1 were broken due to various issues.
Pull request #5 merged: Behaviour fixed so Version
is checked first, then AssemblyVersion
. If not present the VersionPrefix
and VersionSuffix
are checked instead. If VersionPrefix
is not found, the default of 1.0.0 will be used. Thanks to Sean Wright for the fix.
Added functionality to extract VersionPrefix
and VersionSuffix
if present, see #4.
Fixed version number in task.json
- need to increment to update package.
Removed update in 1.11 - should have used BUILD PREFIX as . in the task. Also amended the logging to make it clearer exactly what values are being set.
Amended code to ensure version has a '.' at the end before the BUILDNO (fixed bug in v1.10)
Added .Trim()
to version reads - can sometimes have space after the version tag.
Added fix for blank Version. Sometimes in simple projects the Version
tag is absent because the value is the same as the AssemblyVersion
. Added a check for a blank version being returned and attempts to use the AssemblyVersion
value instead.