We can trigger a release from Powershell by calling the REST API(Releases - Create) but cannot pass process variable (settable at release time).
Because we have to provide the specific artifact version ID
(which artifact be used to trigger the release) in the request body. Once the artifact version ID
is provided,the variable is no longer useful.
You can reference below PowerShell script to trigger a release:
Param(
[string]$collectionurl = "https://{account}.vsrm.visualstudio.com",
[string]$projectName = "GIT",
[string]$user = "username",
[string]$token = "password",
[string]$releasedDefinitionId = "3"
)
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
function CreateJsonBody
{
$value = @"
{
"definitionId":$releasedDefinitionId,
"artifacts":[{"alias":"_BitBucketDev",
"instanceReference":{"id":"454",
"name":"454",
"definitionId":"58",
"sourceBranch":"master",
}
}
],
"isDraft":false,
"manualEnvironments":[]
"@
return $value
}
$json = CreateJsonBody
$uri = "$($collectionurl)/$($projectName)/_apis/Release/releases?api-version=4.1-preview.6"
$result = Invoke-RestMethod -Uri $uri -Method Post -Body $json -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
$ReleaseID = $result.id
Write-Host "ReleaseID:" $ReleaseID