Skip to content

Latest commit

 

History

History
104 lines (64 loc) · 5.15 KB

README.md

File metadata and controls

104 lines (64 loc) · 5.15 KB

VersionReaderTask

Azure DevOps / VSTS build task to read <Version> tag from project files and append Build numbers.

The task reads the <Version> tag from new csproj and vbproj 2017 format files into environment variables.

This tool was created to fix an issue with the new .xxproj project format. The new format does not support wildcard in the version and therefore auto-append of build suffix, which was previously supported.

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.

A useful article on versions from Andrew Lock

Operation

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

Example Usage

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

VersionReaderTask

The value is then used in a dotnet pack task as follows:

packtask 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.

YAML usage

Add the task to your build pipeline, usually after a build and test task:

- task: conficient.VersionReaderTask.version-reader-build-task.VersionReaderTask@2
  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. If your build only runs version reader once, you can omit the prefix.

Version History

Version 2.4

Reverted changes in 2.3, not working

Version 2.3

Updated xmldom package to 0.5.0 for security issue.

Version 2.2

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. Please only use 2.2

Version 1.15

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.

Version 1.14

Added functionality to extract VersionPrefix and VersionSuffix if present, see #4.

Version 1.13

Fixed version number in task.json - need to increment to update package.

Version 1.12

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.

Version 1.11

Amended code to ensure version has a '.' at the end before the BUILDNO (fixed bug in v1.10)

Version 1.9

Added .Trim() to version reads - can sometimes have space after the version tag.

Version 1.8

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.