Tasks
UZG Nuget Versioning task
Sets nuget version based on semantic versioning in SourceBranchName.
YAML snippet
#UZG Nuget Versioning
- task: UZGVersioning@0
None
This task will attempt to parse a Semver (optionally prefixed with "v" or "V") from the Build.SourceBranchName.
On success:
- sets $(
versioningScheme
) = "byEnvVar"
- sets $(
versioningPackageVersion
) = the parsed semver
- updates the
BuildNumber
to "$(Build.DefinitionName
) - $(semver)"
On failure:
- logs a warning
- sets $(
versioningScheme
) = "byBuildNumber"
- sets $(
versioningPackageVersion
) = $(Build.BuildNumber)
Examples
For more information about versioning schemes see the NuGet task documentation.
For more information about BuildNumber (set by the pipeline "name
" property) requirements when using "byBuildNumber", see the Nuget task documentation on the "versioningScheme"-argument
DotNetCoreCLI pack
name: $(BuildDefinitionName)_$(Year:yyyy).$(Month).$(DayOfMonth)$(Rev:.r)
#buildname containing a semver when UZGVersioning fails to parse $(Build.SourceBranchName) and will use versioningScheme = "byBuildNumber"
steps:
#UZG Nuget Versioning
- task: UZGVersioning@0
displayName: 'UZG Nuget Versioning'
#Pack a NuGetPackage with DotNetCoreCLI
- task: DotNetCoreCLI@2
displayName: Pack
inputs:
command: 'pack'
packagesToPack: '**/*.csproj'
# Use the variable set in the UZGVersioning task ("byEnvVar" or "byBuildNumber")
versioningScheme: $(versioningScheme)
# for "byEnvVar" provide the name of the variable to use
versionEnvVar: 'versioningPackageVersion'
NuGetCommand pack
Using the UZGVersioning with the NuGetCommand task works exactly the same.
...
- task: UZGVersioning@0
- task: NuGetCommand@2
inputs:
command: 'pack'
packagesToPack: '**/*.csproj'
versioningScheme: $(versioningScheme)
versionEnvVar: 'versioningPackageVersion'
...
git ref |
$(Build.SourceBranchName) |
$(versioningPackageVersion) |
$(versioningScheme) |
refs/tags/release/v1.2.3 |
v1.2.3 |
1.2.3 |
byEnvVar |
refs/tags/release/v1.2.3-preview1 |
v1.2.3-preview1 |
1.2.3-preview1 |
byEnvVar |
release/v1.2.3 |
v1.2.3 |
1.2.3 |
byEnvVar |
release/1.2.3 |
1.2.3 |
1.2.3 |
byEnvVar |
v1.2.3 |
v1.2.3 |
1.2.3 |
byEnvVar |
release/version1.2.3 |
version1.2.3 |
$(Build.BuildNumber) |
byBuildNumber |
release/v1.2 |
v1.2 |
$(Build.BuildNumber) |
byBuildNumber |
release/something |
something |
$(Build.BuildNumber) |
byBuildNumber |
Extract a Semantic version (semver) from a given input.
YAML snippet
#Extract Semantic Version.
- task: ExtractSemver@0
inputs:
#input: $(Build.SourceBranchName) # Optional
#inputFormat: '^[Vv]?{semver}$' # Optional
#outputFormat: '{major}.{minor}.{patch}{prerelease}' # Optional
#inputIgnorePrefixZero: true #Optional
Parameters |
Description |
input string |
(Optional) The input to extract a semantic version from. Default value: $(Build.SourceBranchName) |
inputFormat string |
(Optional) A regular expression where {semver} is the semver to parse. Default value: ^[Vv]?{semver}$ |
outputFormat string |
(Optional) Use {major}, {minor}, {patch}, {prerelease} and {buildmetadata} to refer to the respective parts of the version. {prerelease} will always start with a "-" (when not empty). {buildmetadata} will always start with a "+" (when not empty). Default value: {major}.{minor}.{patch}{prerelease} |
inputIgnorePrefixZero boolean |
(Optional) Allow and ignore/remove prefix "0" in {minor} and {patch} parts. Eg. the following input will be accepted when 'true': 2024.08.20: {major}=2024, {minor}=8, {patch}=20 2024.09.01: {major}=2024, {minor}=9, {patch}=1 Default value: true |
Task Output Variables
Parameters | Description |
isValid | A valid semver was found and parsed. |
match | The fully matched (non-formatted) semver from input . |
formatted | The formatted semver assembled with outputformat . |
major | The major version. |
minor | The minor version. |
patch | The patch version. |
prerelease | The pre-release label. This does not include the "-"-prefix. |
buildmetadata | The build metadata label. This does not include the "+"-prefix. |
Examples
PowerShell Write-Host
- Extract semver from the
exampleInput
-variable
- Display the resulting output variables (with PowerShell
Write-Host
).
variables:
exampleInput: 'v1.2.3-preview42'
steps:
# Extract semver from $(exampleInput)
- task: ExtractSemver@0
name: GetSemver
inputs:
input: '$(exampleInput)'
# Output the result
- powershell: |
Write-Host "isValid: $(GetSemver.isValid)"
Write-Host "match: $(GetSemver.match)"
Write-Host "formatted: $(GetSemver.formatted)"
Write-Host "major: $(GetSemver.major)"
Write-Host "minor: $(GetSemver.minor)"
Write-Host "patch: $(GetSemver.patch)"
Write-Host "prerelease: $(GetSemver.prerelease)"
Write-Host "buildmetadata: $(GetSemver.buildmetadata)"
The powershell task output:
isValid: True
match: 1.2.3-preview42
formatted: 1.2.3-preview42
major: 1
minor: 2
patch: 3
prerelease: preview42
buildmetadata:
DotNetCoreCLI Pack
- Extract semver from the
exampleInput
-variable
- Choose version control scheme depending on the GetSemver.isValid variable
- Use the
versioningScheme
and versioningPackageVersion
variables with DotNetCoreCLI "pack".
name: $(BuildDefinitionName)_$(Year:yyyy).$(Month).$(DayOfMonth)$(Rev:.r)
variables:
exampleInput: 'v1.2.3-preview42'
steps:
# Extract semver from $(exampleInput)
- task: ExtractSemver@0
name: GetSemver
inputs:
input: '$(exampleInput)'
# decide the 'versioningScheme'. If semver isValid; use versioningScheme="byEnvVar" otherwise, use "byBuildNumber"
- powershell: |
if ("$(GetSemver.isValid)" -eq $true) {
Write-Host "##vso[task.setvariable variable=versioningScheme]byEnvVar"
Write-Host "##vso[task.setvariable variable=versioningPackageVersion]$(GetSemver.formatted)"
} else {
Write-Host "##vso[task.setvariable variable=versioningScheme]byBuildNumber"
}
- task: DotNetCoreCLI@2
displayName: Pack
inputs:
command: 'pack'
packagesToPack: '**/*.csproj'
# Use the variable set in the UZGVersioning task ("byEnvVar" or "byBuildNumber")
versioningScheme: "$(versioningScheme)"
# for "byEnvVar" provide the name of the variable to use
versionEnvVar: 'versioningPackageVersion'
npm-version
- Extract semver from the
exampleInput
-variable
- Update package.json if the semver isValid
name: $(BuildDefinitionName)_$(Year:yyyy).$(Month).$(DayOfMonth)$(Rev:.r)
variables:
exampleInput: 'v1.2.3-preview42'
steps:
- task: ExtractSemver@0
name: PackageVersion
inputs:
input: '$(exampleInput)'
- task: Npm@1
condition: and(succeeded(), eq(variables['PackageVersion.isValid'], 'True'))
inputs:
command: 'custom'
customCommand: 'version $(PackageVersion.formatted) --no-git-tag-version --allow-same-version'
Using the default inputFormat
and outputFormat
, given input
:
input |
isValid |
match |
formatted |
major |
minor |
patch |
prerelease |
buildmetadata |
v1.2.3 |
True |
1.2.3 |
1.2.3 |
1 |
2 |
3 |
|
|
v1.2.3‑preview1 |
True |
1.2.3‑preview1 |
1.2.3‑preview1 |
1 |
2 |
3 |
preview1 |
|
v1.2.3‑preview1+build.123 |
True |
1.2.3‑preview1+build.123 |
1.2.3‑preview1 (*) |
1 |
2 |
3 |
preview1 |
build.123 |
1.2.3 |
True |
1.2.3 |
|
|
|
|
|
|
v2024.09.03 (**) |
True |
2024.9.3 |
2024.9.3 |
2024 |
9 |
3 |
|
|
version1.2.3 |
False |
|
|
|
|
|
|
|
v1.2 |
False |
|
|
|
|
|
|
|
something |
False |
|
|
|
|
|
|
|
(*) the default outputFormat doesn't output {buildmetadata})
(**) assuming inputIgnorePrefixZero
is (the default value); true