0
votes

I know somewhere i'm doing wrong. I'm new to powershell. I'm trying to automate cloning build pipelines for releases using powershell invoke-restmethod. I'm able to get the list of build pipelines but unable to clone. Here is the code

$resp = Invoke-RestMethod -Uri $url -Method GET -UseDefaultCredential -ContentType "application/json" -headers $headers
#Write-Output $resp


foreach($pipe_id in $resp.value.id)
{
   Write-Output $pipe_id
   $build_url = "https://dev.azure.com/$organization/$project/_apis/build/definitions?definitionId=**$pipe_id**&api-version=5.0-preview.7"
   $res = Invoke-RestMethod -Uri $build_url -Method GET -UseDefaultCredential -ContentType "application/json" -headers $headers | ConvertTo-Json
   #Write-Output $res

   $JSON = 
    {
    "repository":  {
                       "properties":  {
                                          "cleanOptions":  "1",
                                          "labelSources":  "0",
                                          "reportBuildStatus":  "true",
                                          "gitLfsSupport":  "false",
                                          "skipSyncSource":  "false",
                                          "checkoutNestedSubmodules":  "false",
                                          "fetchDepth":  "0"
                                      },
                       "id":  "repo_id",
                       "type":  "TfsGit",
                       "name":  "common",
                       "url":  "https://dev.azure.com/$organization/$project/_git/common",
                       "defaultBranch":  "refs/heads/Releases/Release_branch",
                       "clean":  "true",
                       "checkoutSubmodules":  false
                   },
                   "id":  7,
                   "name":  "common-clone",
                   "path":  "\\Releases",
                    "type":  "build",
                    "queueStatus":  "enabled"
     }

$clone_url = "https://dev.azure.com/$organization/$project/_apis/build/definitions?api-version=5.0-preview.7"
$res = Invoke-RestMethod -Uri $build_url -Method POST -Body $JSON -UseDefaultCredential -ContentType "application/json" -headers $headers
Write-Output $res
}

I'm getting the below error

Invoke-RestMethod : {"$id":"1","innerException":null,"message":"Value cannot be null.\r\nParameter name: definition.Process","typeName":"System.ArgumentNullException, mscorlib","typeKey":"ArgumentNullException","errorCode":0,"eventId":0}

Thanks!

1
Do you want to clone the definition you get with the first GET?Shayki Abramczyk
i'm trying to get the list of build pipelines using GET, so that i can try to clone using build definition id.Kart
You don't need to create the json by yourself, you can use the $res variable you get, just convert it to json $res = $res.value | ConvertTo-Json and give it to the body.Shayki Abramczyk

1 Answers

0
votes

To clone a classic pipeline with Rest API, you need to use these two Rest APIs:

Definitions - Get

Definitions - Create

Here is the PowerShell example:

$connectionToken="{pat}"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$BuildDefinitionInfoURL = "https://dev.azure.com/{org name}/{project name}/_apis/build/definitions/386" 
$BuildDefinitionInfo = Invoke-RestMethod -Uri $BuildDefinitionInfoURL -Headers @{authorization = "Basic $base64AuthInfo"} -Method Get 
Write-Host $BuildDefinitionInfo.name

$BuildDefinitionInfo.name = $BuildDefinitionInfo.name +" clone"

Write-Host $BuildDefinitionInfo.name

$body = $BuildDefinitionInfo | ConvertTo-Json -Depth 99
$createBuildDefinitionURL = "https://dev.azure.com/{org name}/{project name}/_apis/build/definitions?api-version=6.0"
$response = Invoke-RestMethod -Uri $createBuildDefinitionURL -ContentType "application/json" -Body $body -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method POST

Write-Host $response.id

Result:

enter image description here