1

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Shayki, I have made the changes, now I get this error. Invoke-RestMethod : {"$id":"1","innerException":null,"message":"No agent pool found with identifier 391.","typeName":"Microsoft.TeamFoundation.DistributedTask.WebApi.TaskAgentQueueNotFoundException, Microsoft.TeamFoundation.DistributedTask.WebApi","typeKey":"TaskAgentQueueNotFoundException","errorCode":0,"eventId":3000}
Change the queueId from 391 to a valid number. you can go here https://dev.azure.com/{org-name}/_settings/agentpools, enter a pool and see the id in the URL.
could you suggest me for the query in below link? stackoverflow.com/q/67670649/13450322

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.