VSTS builds have the option to set build variable values at the time of queuing a build. You have to select option for build variable “Settable at queue time” to enable the variable to be set its value at the time of queuing build. However, this feature was not available for VSTS Release and there was a user voice raised here requesting to allow setting release variables at the time of creating a release. As promised by MSFT, this feature is now available for VSTS Release. Let’s look at how to use it with VSTS web UI and with the VSTS REST API.
With VSTS Web UI
In your release definition now you can selection option “Settable at release time”.
This allows you to set the value of the variable at the time of creating the release.
With VSTS REST API
First you need to enable variables to be settable at release time as shown in above in the VSTS Release Definition. However, VSTS REST API documentation here for creating a release does not show you how to supply the variables when creating a release.
You can use below json syntax to add variables to the REST API call request body to set the variable values at the time of creating a release.
"variables": {
"Variable1Name": {
"value": "Variable1Value"
},
"Variable2Name": {
"value": "Variable2Value"
}
}
Here is a sample PowerShell script calling VSTS REST API to create a release while setting variables (setting artifacts is omitted in this sample script but it is just a matter of adding it to the request body).
param( [Parameter(Mandatory=$true)] [string] $token, [Parameter(Mandatory=$true)] [string] $VSTSAccoutName, [Parameter(Mandatory=$true)] [string] $teamProjectName, [Parameter(Mandatory=$true)] [string] $ReleaseDefId, [Parameter(Mandatory=$true)] [string] $TriggerDescription, [Parameter(Mandatory=$true)] [string] $octopusProject ) $User="" # Base64-encodes the Personal Access Token (PAT) appropriately $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $User,$token))); $header = @{Authorization=("Basic {0}" -f $base64AuthInfo)}; $ReleaseMetadata = '{"definitionId": ' + $ReleaseDefId + ',"description": "' + $TriggerDescription + '","isDraft": false,"reason": "none","manualEnvironments": null, "variables": { "OctopusProject": { "value": "' + $octopusProject + '" }, "ToEnv": { "value": "AutoDumDum" } }'; $Uri = 'https://' + $VSTSAccoutName + '.vsrm.visualstudio.com/' + $teamProjectName + '/_apis/release/releases?api-version=4.1-preview.6' $ReleaseResponse = Invoke-RestMethod -Method Post -ContentType application/json -Uri $Uri -Body $ReleaseMetadata -Headers $header Write-Host $ReleaseResponse
No comments:
Post a Comment