maikvandergaag / msft-extensions

Repository for extensions mainly used for Azure DevOps Extensions
https://msftplayground.com
MIT License
126 stars 81 forks source link

Version Number Counter Fails for Release #492

Closed rupreck closed 9 months ago

rupreck commented 1 year ago

Describe the issue Version Number Counter Fails when used with Releases. Operates ok with Build Pipelines

To Reproduce Add to a Release Pipeline, set the variable and run.

Expected behavior As designed

Situation (please complete the following information):

Log info

2023-07-01T10:55:54.4646883Z ##[section]Starting: Version Number Counter
2023-07-01T10:55:54.4754984Z ==============================================================================
2023-07-01T10:55:54.4755209Z Task         : Version number counter
2023-07-01T10:55:54.4755338Z Description  : Build and release task for a version number counter
2023-07-01T10:55:54.4755502Z Version      : 2.0.10
2023-07-01T10:55:54.4755610Z Author       : Maik van der Gaag
2023-07-01T10:55:54.4755726Z Help         : 
2023-07-01T10:55:54.4755822Z ==============================================================================
2023-07-01T10:55:55.3697491Z VersionVariable      : node.version
2023-07-01T10:55:55.3699629Z UpdateMinorVersion   : true
2023-07-01T10:55:55.3701857Z MaxValuePatchVersion : 12
2023-07-01T10:55:55.3704017Z UpdateMajorVersion   : true
2023-07-01T10:55:55.3706050Z MaxValueMinorVersion : 12
2023-07-01T10:55:55.3722436Z DevOpsPAT            : ***
2023-07-01T10:55:55.3724457Z DevOps Uri           : https://dev.azure.com/my/
2023-07-01T10:55:55.3726540Z Project Name         : SQA
2023-07-01T10:55:55.3728938Z Project Id           : 37c04e5d-3288-4161-96ab-1cf73b175a40
2023-07-01T10:55:55.3730698Z Only Update Minor    : False
2023-07-01T10:55:55.3732699Z API Version          : 6.0
2023-07-01T10:55:55.3734682Z BuildId              : 2340
2023-07-01T10:55:55.3889156Z Invoking rest method 'Get' for the url: https://dev.azure.com/my/project/_apis/build/builds/2340?api-version=6.0.
2023-07-01T10:55:57.4778169Z ##[debug]Working with definition id: 28
2023-07-01T10:55:57.4786638Z Trying to retrieve the build definition with the url: https://dev.azure.com/my/project/_apis/build/definitions/28?api-version=6.0.
2023-07-01T10:55:57.8081808Z ##[error]The variables can not be found on the definition: , , 
2023-07-01T10:55:58.0308740Z ##[section]Finishing: Version Number Counter
maikvandergaag commented 1 year ago

That is correct and expected. The solution is build for build pipelines. Can mark this as a feature request.

rupreck commented 1 year ago

Hello,

Thanks for getting back to me.

The documentation says for both Build and Release Pipelines as follows...

_"Version number counter is a Release and Build pipeline tasks that can increment a version number.

The default version number should be saved as a variable in the release or build pipeline. The task will increment the number based on your configuration."_

And in the Visual Studio Marketplace it is advertised as such:

image

It would be really helpful if it was updated to support Release Pipelines.

In the meantime the following PowerShell will successfully update a variable called Version within a Release Pipeline:

# Your Azure DevOps organization, project, and definition details
$organization = 'MyOrganisation'
$project = 'MyProject'
$releaseDefinitionName = 'MyPipeline'
$pat = 'My Personal Access Token'

# Access the Azure DevOps variable called Version
$variableName = $env:VERSION

# Split version number into components
$version = $variableName.Split('.')
$major = [int]$version[0]
$minor = [int]$version[1]
$patch = [int]$version[2]

# Increment version number
if ($patch -lt 10) {
    $patch++
} else {
    $patch = 0
    if ($minor -lt 10) {
        $minor++
    } else {
        $minor = 0
        $major++
    }
}

# Combine version number components back into a string
$newVersion = "$major.$minor.$patch"

# Base64-encode the Personal Access Token (PAT)
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($pat)"))

# Get all release definitions and find the one with the specified name
$url = "https://vsrm.dev.azure.com/$($organization)/$($project)/_apis/release/definitions?api-version=6.1-preview.4"
$definitions = Invoke-RestMethod -Uri $url -Method Get -ContentType 'application/json' -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
$definitionId = $definitions.value | Where-Object { $_.name -eq $releaseDefinitionName } | Select-Object -ExpandProperty id

# Get the release definition by ID
$url = "https://vsrm.dev.azure.com/$($organization)/$($project)/_apis/release/definitions/$($definitionId)?api-version=6.1-preview.4"
$definition = Invoke-RestMethod -Uri $url -Method Get -ContentType 'application/json' -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

# Update the variable in the release definition
$definition.variables.$variableName.value = $newVersion

# Convert the definition back to JSON
$body = $definition | ConvertTo-Json -Depth 10

# Update the release definition
$response = Invoke-RestMethod -Uri $url -Method Put -Body $body -ContentType 'application/json' -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

# Output the response
Write-Output $response

Best,

Will

maikvandergaag commented 1 year ago

Ok will well check what is going wrong. Could you share a debug log file?