powershellazure-devopsazure-pipelines-release-pipelineazure-devops-rest-apiazure-devops-pipelines

Error while trying to create a release definition using Azure DevOps Rest API


I am using below script to create a release definition in Azure DveOps with a PowerShell script, but its failing with error message as below. Not sure where am giving incorrect details.

Invoke-RestMethod : {"$id":"1","innerException":null,"message":"VS402903: The specified value is not convertible to type ReleaseDefinition. Make sure it is convertible to type ReleaseDefinition and try again."

Code snippet is:

Param( 
[string]$organisation = "ORGNAME", 
[string]$project = "PROJECTNAME", 
[string]$keepForever = "true", 
[string]$user = "userid", 
[string]$token = "tokenID" ) 

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
Write-host $base64AuthInfo

$postresults = "https://vsrm.dev.azure.com/$organisation/$project/_apis/release/definitions?api-version=6.0"
Write-host $postresults

 $body = 
@"
{  "name": "New release pipeline ",
     "comment": "test",
    "definitionId": 860,
    "description": "Create Release from PowerShell",
    "artifacts": [],
    "isDraft": false,
    "reason": "Demo purpose",
    "manualEnvironments": null,
    "environmentsMetadata": null, 
    "properties": null, 
    "variables": null
    "environments": [
    {
      "name": "PROD",
      "preDeployApprovals": {
        "approvals": [
          {
            "rank": 1,
            "isAutomated": false,
            "isNotificationOn": false,
            "approver": {
              "displayName": null,
              "id": ""
            },
            "id": 0
          }
        ]
      },
      "postDeployApprovals": {
        "approvals": [
          {
            "rank": 1,
            "isAutomated": true,
            "isNotificationOn": false,
            "id": 0
          }
        ]
      },
      "deployPhases": [
        {
          "deploymentInput": {
            "parallelExecution": {
              "parallelExecutionType": "none"
            },
            "skipArtifactsDownload": false,
            "artifactsDownloadInput": {},
            "queueId": 391,
            "demands": [],
            "enableAccessToken": false,
            "timeoutInMinutes": 0,
            "jobCancelTimeoutInMinutes": 1,
            "condition": "succeeded()",
            "overrideInputs": {}
          },
          "rank": 1,
          "phaseType": "agentBasedDeployment",
          "name": "Run on agent",
          "workflowTasks": []
        }
      ],
      "retentionPolicy": {
        "daysToKeep": 30,
        "releasesToKeep": 3,
        "retainBuild": true
      }
    }
  ],
  "path": "\\",
  "releaseNameFormat": "Release",
  "description": ""
} 
"@ | ConvertTo-Json -Depth 100

$result = Invoke-RestMethod -Uri $postresults -Method Post -Body $body -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

if ($result.count -eq 0)
{
     throw "Unable to locate Release Definition Id $($definitionId)"
}
else
{
    Write-host "Success!!!"
}

Solution

  • You have few issues in the json body:

    1. The description exist twice.

    2. You have missing , near the "variables": null.

    3. You need to specify a valid id in the preDeployApprovals:

          "approver": {
        "displayName": null,
        "id": "PUT-HERE-ID"
      },
      
    4. Add the above section also to the

    5. You don't need to convert it again to json, remove the | ConvertTo-Json -Depth 100 from the code.