如何使用 azure DevOps 将经典管道转换为 yml 管道 api

How to convert classic pipeline to yml pipeline using azure DevOps api

我可以使用这个 api 并将经典管道转换为 yml 管道吗 -

放置https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{definitionId}?api-version=6.0

我试图通过将此 yml 内容放入邮递员正文并使用 PUT 请求在 API 上方调用 -

来覆盖 json 正文
{
    "_links": {
        "self": {
            "href": "https://dev.azure.com/{org}/xxxx/_apis/pipelines/7?revision=2"
        },
       "web": {
           "href": "https://dev.azure.com/{org}/xxxx/_build/definition?definitionId=7"
        }
    },
    "configuration": {
        "path": "azure-pipelines.yml",
        "repository": {
            "id": "xxxxx",
            "type": "azureReposGit"
        },
        "type": "yaml"
    },
    "url": "https://dev.azure.com/{org}/xxxx/_apis/pipelines/7?revision=2",
    "id": 7,
    "revision": 2,
    "name": "testput",
    "folder": "\"
    }

邮递员出现错误 - “203 非权威信息”

有什么办法可以实现吗?

另外,如何使用 azure DevOps 下载 yml 文件 api?

在UI我可以从这里下载。我想使用 API 下载它,然后将经典管道转换为 yml 管道?

要为您的管道下载 yaml,您需要调用此端点

https://dev.azure.com/<YOUR-ORGANIZATION>/<YOUR-PROJECT-ID>/_apis/build/definitions/45?dummyValue=

对对对,最后还有?dummyValue=

那么你会得到这样的回复:

{
    "yaml": "variables:\r\n- name: BuildParameters.RestoreBuildProjects\r\n  value: dotnet-core-on-windows/**/*.csproj\r\n- name: BuildParameters.TestProjects\r\n  value: dotnet-core-on-windows/*[Tt]ests/*.csproj\r\nname: $(date:yyyyMMdd)$(rev:.r)\r\nresources:\r\n  repositories:\r\n  - repository: self\r\n    type: git\r\n    ref: master\r\njobs:\r\n- job: Job_1\r\n  displayName: Agent job 1\r\n  pool:\r\n    vmImage: ubuntu-16.04\r\n  steps:\r\n  - checkout: self\r\n  - task: UseDotNet@2\r\n    displayName: Use .Net Core sdk 3.1.x\r\n    inputs:\r\n      version: 3.1.x\r\n  - task: Bash@3\r\n    displayName: Bash Script\r\n    inputs:\r\n      targetType: inline\r\n      script: >-\r\n        ls $(Build.SourcesDirectory) *\r\n\r\n        echo 'siema'\r\n  - task: DotNetCoreCLI@2\r\n    displayName: Restore\r\n    inputs:\r\n      command: restore\r\n      projects: $(BuildParameters.RestoreBuildProjects)\r\n  - task: DotNetCoreCLI@2\r\n    displayName: Build\r\n    inputs:\r\n      projects: $(BuildParameters.RestoreBuildProjects)\r\n      arguments: --configuration $(BuildConfiguration)\r\n  - task: DotNetCoreCLI@2\r\n    displayName: Test\r\n    inputs:\r\n      command: test\r\n      projects: $(BuildParameters.TestProjects)\r\n      arguments: --configuration $(BuildConfiguration) --collect:\"XPlat Code Coverage\"\r\n  - task: DotNetCoreCLI@2\r\n    displayName: dotnet install report-generator\r\n    inputs:\r\n      command: custom\r\n      custom: tool\r\n      arguments: install --tool-path . dotnet-reportgenerator-globaltool\r\n  - task: PowerShell@2\r\n    displayName: PowerShell Script\r\n    enabled: False\r\n    inputs:\r\n      targetType: inline\r\n      script: dotnet tool install --global dotnet-reportgenerator-globaltool --version 4.5.8\r\n  - task: PowerShell@2\r\n    displayName: PowerShell Script\r\n    inputs:\r\n      targetType: inline\r\n      script: >-\r\n        dotnet tool list\r\n\r\n\r\n        ./reportgenerator -reports:$(Agent.TempDirectory)/**/coverage.cobertura.xml -targetdir:$(Build.SourcesDirectory)/coverlet/reports -reporttypes:\"Cobertura\"\r\n\r\n\r\n        ls $(Build.SourcesDirectory)/coverlet/reports\r\n      pwsh: true\r\n  - task: PowerShell@2\r\n    displayName: PowerShell Script\r\n    inputs:\r\n      targetType: inline\r\n      script: ls $(Build.SourcesDirectory)/coverlet/reports\r\n  - task: PublishCodeCoverageResults@1\r\n    displayName: Publish code coverage from $(Build.SourcesDirectory)/coverlet/reports/Cobertura.xml\r\n    inputs:\r\n      codeCoverageTool: Cobertura\r\n      summaryFileLocation: $(Build.SourcesDirectory)/coverlet/reports/Cobertura.xml\r\n  - task: DotNetCoreCLI@2\r\n    displayName: Publish\r\n    enabled: False\r\n    inputs:\r\n      command: publish\r\n      publishWebProjects: True\r\n      projects: $(BuildParameters.RestoreBuildProjects)\r\n      arguments: --configuration $(BuildConfiguration) --output $(build.artifactstagingdirectory)\r\n      zipAfterPublish: True\r\n  - task: PublishBuildArtifacts@1\r\n    displayName: Publish Artifact\r\n    condition: succeededOrFailed()\r\n    enabled: False\r\n    inputs:\r\n      PathtoPublish: $(build.artifactstagingdirectory)\r\n      TargetPath: '\\my\share\$(Build.DefinitionName)\$(Build.BuildNumber)'\r\n...\r\n"
}

然后你应该使用Pipelines - Create

https://dev.azure.com/{organization}/{project}/_apis/pipelines?api-version=6.0-preview.1

您可以找到的示例 - 不用担心这个问题,因为它现在已经解决了。我在这里复制了上面的例子

curl -X POST \
  'https://dev.azure.com/<myorg>/<myproj>/_apis/pipelines?api-version=6.0-preview.1' \
  -H 'Authorization: Basic <b64string>' \
  -H 'Cache-Control: no-cache' \
  -H 'Content-Type: application/json' \
  -d '{
    "configuration":{
    
        "repository": {
            "id": "<repo-guid>",
            "name": "<repo-name>",
            "type": "azureReposGit"
        },
        "path": "pipeline.yaml",
        "type": "yaml"
    },
    "folder": "\custompath\",
    "name": "<pipelinename>"
}

In UI I can download from here. I want to download this using API and then convert classic pipeline to yml pipeline ?

没有这样的文件 REST API 将经典管道导出到 yml 管道。但是我们可以使用 F12 来获取 URL:

https://dev.azure.com/<YourOrganizationName>/<YourProjectName>/_apis/build/definitions/<DefinitionsId>?dummyValue=

return 结果是:

{
    "yaml": "jobs:\r\n- job: Job_1\r\n  displayName: Agent job\r\n  pool:\r\n    vmImage: vs2017-win2016\r\n  steps:\r\n  - checkout: self\r\n  - task: NuGetToolInstaller@1\r\n    displayName: 'Use NuGet '\r\n...\r\n"
}

很明显,这不是标准的YAML文件,包括headers和换行符等。我们无法用它来创建一个新的管道。因此,我们必须将其转换为标准 YAML。请检查我的 powershell 脚本:

$outfile = "D:\YAMLTempFolder\test.yaml"

$connectionToken="Your PAT Here"

$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::  
ASCII.GetBytes(":$($connectionToken)"))

$YAMLURL = "https://dev.azure.com/<YourOrganizationName>/<YourProjectName>/_apis/build/definitions/165?dummyValue=" 

$YAMLInfo = Invoke-RestMethod -Uri $YAMLURL -Headers @{authorization = "Basic $base64AuthInfo"} -Method Get 

$yaml = $YAMLInfo.yaml

echo $yaml.Replace("...","") > $outfile

我的YAML文件的测试结果是:

现在,我们得到了 YAML 文件。我们可以 将此 YAML 提交到存储库 ,然后使用 REST API 使用该 YAML 创建一个新的 YAML 管道。

之所以需要将YAML提交到repo:

How to create new build pipeline using Azure DevOps REST API?

如何使用现有 YAML 创建新的 YAML 管道:

Azure DevOps create build definition via REST API given existing YAML

更新:

如果我的 ORG 在美国中部,REST API 应该是:

Yaml - Get:

GET https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{definitionId}/yaml?api-version=6.1-preview.1

https://dev.azure.com/<YourOrganizationName>/<YourProjectName>/_apis/build/definitions/1/yaml

要获取 REST API,请打开您的定义并按 F12,然后单击导出到 YAML 选项: