Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Recently one of the customer wanted to add a release variable using RM Rest APIs and I shared with him the following code. You can get the PAT token using the instructions mentioned here.
Powershell Sample
param (
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string] $token
)
## Construct a basic auth head using PAT
function BasicAuthHeader()
{
param([string]$authtoken)
$ba = (":{0}" -f $authtoken)
$ba = [System.Text.Encoding]::UTF8.GetBytes($ba)
$ba = [System.Convert]::ToBase64String($ba)
$h = @{Authorization=("Basic{0}" -f $ba);ContentType="application/json"}
return $h
}
$getReleaseUri = "https://myaccount.vsrm.visualstudio.com/VSOnline/_apis/Release/releases/100?api-version=4.0-preview.4"
$h = BasicAuthHeader $token
$release = Invoke-RestMethod -Uri $getReleaseUri -Headers $h -Method Get
# Update an existing variable named d1 to its new value d5
$release.variables.d1.value = "d5";
####****************** update the modified object **************************
$release2 = $release | ConvertTo-Json -Depth 100
$release2 = [Text.Encoding]::UTF8.GetBytes($release2)
$updateReleaseUri = “https://myaccount.vsrm.visualstudio.com/defaultcollection/VSOnline/_apis/release/releases/100`?api-version=4.0-preview.4”
$content2 = Invoke-RestMethod -Uri $updateReleaseUri -Method Put -Headers $h -ContentType “application/json” -Body $release2 -Verbose -Debug
write-host "=========================================================="
C# Sample
string collectionUri = args[0];
string projectName = args[1];
string patToken = args[2];
int releaseId = int.Parse(args[3]);
VssConnection connection = new VssConnection(new Uri(collectionUri), new VssBasicCredential("username", patToken));
ReleaseHttpClient client = connection.GetClient<ReleaseHttpClient>();
var release = client.GetReleaseAsync(projectName, releaseId).Result;
var variableValue = new ConfigurationVariableValue();
variableValue.Value = "bar";
release.Variables.Add("foo", variableValue);
var updatedRelease = client.UpdateReleaseAsync(release, projectName, releaseId).Result;
You can get the complete solution from here.
Enjoy !!