2021年3月28日星期日

Azure DevOps API - Push a YAML file from a powershell script

Objective : Push a YAML file from a powershell script using the Azure DevOps API.

According to the Azure DevOps documentation I was able to get this following code :

$pushFileJSON = @{      refUpdates = @(          @{              name = $branchToPush              oldObjectId = $oldObjectId          }      )      commits = @(          @{              comment = "Added azure-pipelines.yml file."              changes = @(                  @{                      changeType = "add"                      item = @{                          path = "/azure-pipelines.yml"                      }                      newContent = @{                          content = $yamlContent                          contentType = "rawtext"                      }                  }              )          }      )  } | ConvertTo-Json -Depth 5  $request = 'https://dev.azure.com/' + $organization + '/' + $projectName + '/_apis/git/repositories/' + $repositoriesId + '/pushes?api-version=6.0'  $responsePushYAML = Invoke-RestMethod $request -Method 'POST' -Headers $headers -Body $pushFileJSON -ContentType "application/json"  

But due to the JSON field newContent and so content who is a string field if, as I do currently, give the YAML content this last one when he is pushed on the branch I got a file like this :

# Starter pipeline # Start with a minimal pipeline that you can customize to build and deploy your code. # Add steps that build, run tests, deploy, and more: # https://aka.ms/yaml  variables:   - group: 'AutoMergeTestBranch'   # Agent & Container parameter   - name:  'AgentPool'     value: 'DemoAgent' jobs: - job: Start_Build_Agent   displayName: "Start Build Agent"   workspace:     clean: all   pool: 'Hosted Windows 2019 with VS2019'   steps:     - task: PowerShell@2    

Without any indentation or correct syntax :/ Does anyone know hot to push a YAML file to a repo ?

https://stackoverflow.com/questions/66816139/azure-devops-api-push-a-yaml-file-from-a-powershell-script March 26, 2021 at 07:32PM

没有评论:

发表评论