值不能为空。参数名称:build.Definition

时间:2019-10-08 11:01:13

标签: azure-devops azure-deployment

我正在尝试使用azure devops REST API将构建排队,但是一直遇到错误。我传递了API所需的所有强制性值,但仍然获取值不能为null错误。

已经遍历了文档,但是找不到任何示例代码。我曾尝试使用API​​文档创建powershell脚本,但是却难以运行。

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 !!!"
}

希望得到一些可以执行此建议的建议。我还向构建传递了3个变量,这些变量将在天蓝色devops构建管道中用作管道变量。

1 个答案:

答案 0 :(得分:0)

您必须传递这样的参数:

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

有关详细信息,请查看此帖子Start a build and passing variables through VSTS Rest API