1

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!

3
  • Do you want to clone the definition you get with the first GET? Commented Aug 25, 2021 at 12:45
  • i'm trying to get the list of build pipelines using GET, so that i can try to clone using build definition id. Commented Aug 25, 2021 at 12:47
  • 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. Commented Aug 25, 2021 at 12:54

1 Answer 1

1

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

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

1 Comment

How and where to run the PowerShell? In Azure portal?

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.