1
votes

Is it possible to trigger a release from PowerShell (PowerShell task in a build pipeline) and pass process variable (settable at release time)?

Objective:

We have on build pipeline using multiple branches. I want to trigger a release (which has variables settable at release time) from the build based on the branch name.

The reason behind using variables at release time is that his variable is used to target specific resources on azure.

1

1 Answers

2
votes

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" 

)

# Base64-encodes the Personal Access Token (PAT) appropriately
$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