0

I am trying to queue a build using azure devops REST API but keep running into errors. I am passing all the mandatory value required for the API but still get value cannot be null error.

Already went through the documentation but couldnt find any sample code. I have tried to create powershell script using the API documentation but struggling to run it.

Param(
   [string]$vstsAccount = "xxxxx",
   [string]$projectName = "yyyyy",
   [string]$definitionId = "1",
   [string]$keepForever = "true",
   [string]$personalAccessToken  = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz",
   [string]$accountname = "example",
   [String]$adminemail = "example@example.com",
   [String]$adminpassword = "example"
)

# Base64-encodes the Personal Access Token (PAT)
$headers = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($personalAccessToken)")) }

# Construct the REST URL
$uri = "https://dev.azure.com/$vstsAccount/$projectName/_apis/build/builds?api-version=5.0"

Write-Host "Uri :" $uri

$params = 
@"
{
    "definition": $definitionId,
    "description": "Create Release from PowerShell Script",
    "artifacts": [],
    "isDraft": false,
    "reason": "New tenant email Trigger",
    "manualEnvironments": null,
    "environmentsMetadata": null, 
    "properties": null, 
    "variables": {
        "accountname": {
          "value": "$accountname",
          "allowOverride": true
        },
        "adminemail": {
          "value": "$adminemail",
          "allowOverride": true
        },
        "adminpassword": {
          "value": "$adminpassword",
          "allowOverride": true
        }
    }
}
"@

Write-Host "Request Body :" $params

# Invoke the REST call and capture the results
$result = Invoke-RestMethod -Uri $uri -Method POST -Body $params -Headers $headers -ContentType "application/json" -Verbose -Debug

Write-Host "Result :" $result

# This call should only provide a single result
if ($result.count -eq 0)
{
    Write-host "Unable to locate Release Definition Id $definitionId"
}
else
{
    Write-host "Successfully triggered the VSTS release job !!!"
}

Hoping to get some advice that will execute this. I am also passing 3 variables to the build that will be leveraged as pipeline variables ahead in the azure devops build pipeline.

RB.
  • 155
  • 12
  • able to trigger the job by updating params to - "definition": { "id": $definitionId } but still not able to pass variables to pipepline – RB. Oct 08 '19 at 11:38

1 Answers1

0

You have to pass parameters like this:

    {
    "parameters":  "{\"ReleaseNumber\":  \"1.0.50\", \"AnotherParameter\":  \"a value\"}",
    "definition":  {
                       "id":  2
                   }
    }

Take a look at this post for further information Start a build and passing variables through VSTS Rest API

point
  • 290
  • 3
  • 12