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

时间:2021-01-08 01:18:09

标签: azure-devops azure-pipelines azure-devops-rest-api azure-pipelines-yaml

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

PUT 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 api 下载 yml 文件?

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

enter image description here

2 个答案:

答案 0 :(得分:2)

要为您的管道下载 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

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

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>"
}

答案 1 :(得分:2)

<块引用>

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

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

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

返回结果为:

{
    "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 文件,包括标题和换行符等。我们无法使用它来创建新管道。因此,我们必须将其转换为标准的 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文件的测试结果是:

enter image description here

现在,我们得到了 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 选项:

enter image description here