【问题标题】:Azure DevOps API - Push a YAML file from a powershell scriptAzure DevOps API - 从 powershell 脚本推送 YAML 文件
【发布时间】: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 字段 newContentcontent 是一个字符串字段,如果像我目前所做的那样,当他被推到分支上时,给 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


【解决方案1】:

没有任何缩进或正确的语法

查看这个示例:Update a file,它添加了字段\n 进行换行,我们还需要添加\n 来更改缩进和语法。

$yaml 内容:

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

结果:

您也可以查看ticket

【讨论】:

  • 您好 Vito,我已经尝试过您的解决方案,但不幸的是这对我不起作用。事实上,我得到的结果与之前的单行文件相同。您能否分享一下您的 powershell 脚本解决方案,它可以让您在上面获取正确的 yaml 文件?
猜你喜欢
  • 2021-01-20
  • 1970-01-01
  • 1970-01-01
  • 2021-07-14
  • 2019-09-01
  • 2021-03-25
  • 1970-01-01
  • 2019-02-21
  • 1970-01-01
相关资源
最近更新 更多