【发布时间】:2021-06-23 05:38:37
【问题描述】:
目标:使用 Azure DevOps API 从 powershell 脚本推送 YAML 文件。
根据 Azure DevOps 文档,我能够获得以下代码:
$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"
但是由于 JSON 字段 newContent 和 content 是一个字符串字段,如果像我目前所做的那样,当他被推到分支上时,给 YAML 内容最后一个,我得到一个像这样的文件:
# 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
没有任何缩进或正确的语法:/ 有谁知道将 YAML 文件推送到 repo 很热?
【问题讨论】:
-
您是否有理由不使用 Git 客户端,例如您需要克隆存储库、添加 YAML 文件、提交并通过 Git 推送更改?
标签: git powershell azure-devops yaml azure-devops-rest-api