简体   繁体   English

在 Azure DevOps 中,是否可以使用 API 递归地枚举子管道构建工件?

[英]In Azure DevOps, is it possible to enumerate children pipeline build artifacts recursively with API?

In Azure DevOps, I want to get a list of recursive artifact elements from a pipeline build.在 Azure DevOps 中,我想从管道构建中获取递归工件元素的列表。 It would be nice if I don't have to download the whole artifact root object.如果我不必下载整个工件根 object,那就太好了。 Does any one know how to do this with the current API?有谁知道如何用当前的 API 做到这一点?

The portal already supports this feature in the pipeline artifacts view.门户已在管道工件视图中支持此功能。 You can open and browse child artifacts, with the ability to download.您可以打开和浏览具有下载功能的子工件。 The API however does not seem to support this use case.然而,API 似乎不支持此用例。

在此处输入图像描述

Current API https://docs.microsoft.com/en-us/rest/api/azure/devops/build/Artifacts/List?view=azure-devops-rest-6.0#buildartifact当前 API https://docs.microsoft.com/en-us/rest/api/azure/devops/build/Artifacts/List?view=azure-devops-rest-6.0#buildartifact

I was able to find a request for the feature, but I'm not sure if it will be implemented soon.我能够找到对该功能的请求,但我不确定它是否会很快实施。 https://developercommunity.visualstudio.com/idea/1300697/api-list-artifacts-enumerate-recursively-same-as-w.html https://developercommunity.visualstudio.com/idea/1300697/api-list-artifacts-enumerate-recursively-same-as-w.html

Has anyone else been able to work around this?有没有其他人能够解决这个问题?

This is not documented but you can use the same API call as it is done on Azure DevOps.这没有记录,但您可以使用与 Azure DevOps 上相同的 API 调用。 So it would be所以它会是

Post https://dev.azure.com/{organization1}/_apis/Contribution/HierarchyQuery/{projectId}?api-version=5.0-preview.1

In my case it was就我而言,它是

https://dev.azure.com/thecodemanual/_apis/Contribution/HierarchyQuery/project/4fa6b279-3db9-4cb0-aab8-e06c2ad550b2?api-version=5.0-preview.1

With similar payload similar to this one:使用与此类似的有效负载:

{
    "contributionIds": [
        "ms.vss-build-web.run-artifacts-data-provider"
    ],
    "dataProviderContext": {
        "properties": {
            "artifactId": 1158,
            "buildId": 7875,
            "sourcePage": {
                "url": "https://dev.azure.com/thecodemanual/DevOps%20Manual/_build/results?buildId=7875&view=artifacts&pathAsName=false&type=publishedArtifacts",
                "routeId": "ms.vss-build-web.ci-results-hub-route",
                "routeValues": {
                    "project": "DevOps Manual",
                    "viewname": "build-results",
                    "controller": "ContributedPage",
                    "action": "Execute",
                    "serviceHost": "be1a2b52-5ed1-4713-8508-ed226307f634 (thecodemanual)"
                }
            }
        }
    }
}

So you would get such response:所以你会得到这样的回应:

{
    "dataProviderSharedData": {},
    "dataProviders": {
        "ms.vss-web.component-data": {},
        "ms.vss-web.shared-data": null,
        "ms.vss-build-web.run-artifacts-data-provider": {
            "buildId": 7875,
            "buildNumber": "20201114.2",
            "definitionId": 72,
            "definitionName": "kmadof.hadar",
            "items": [
                {
                    "artifactId": 1158,
                    "name": "/hadar.zip",
                    "sourcePath": "/hadar.zip",
                    "size": 1330975,
                    "type": "file",
                    "items": null
                },
                {
                    "artifactId": 1158,
                    "name": "/scripts",
                    "sourcePath": "/scripts",
                    "size": 843,
                    "type": "directory",
                    "items": [
                        {
                            "artifactId": 1158,
                            "name": "/scripts/check-hadar-settings.ps1",
                            "sourcePath": "/scripts/check-hadar-settings.ps1",
                            "size": 336,
                            "type": "file",
                            "items": null
                        },
                        {
                            "artifactId": 1158,
                            "name": "/scripts/check-webapp-settings.ps1",
                            "sourcePath": "/scripts/check-webapp-settings.ps1",
                            "size": 507,
                            "type": "file",
                            "items": null
                        }
                    ]
                }
            ]
        }
    }
}

Of course you need to use PAT token to authorize your request.当然,您需要使用 PAT 令牌来授权您的请求。

You can try as the steps below:您可以尝试以下步骤:

  1. Execute the endpoint " Artifacts - Get Artifact " of the Artifacts API.执行 Artifacts API 的端点“ Artifacts - Get Artifact ”。 From the response body, you can see the value of " downloadUrl " like as this.从响应正文中,您可以看到“ downloadUrl ”的值是这样的。
https://artprodcus3.artifacts.visualstudio.com/{organization_ID}/{project_ID}/_apis/artifact/{object_ID}/content?format=zip

在此处输入图像描述

This URL is used to download ( GET ) the whole artifact as a ZIP file.这个 URL 用于下载( GET )整个工件作为 ZIP 文件。

  1. If you want to download a specified sub-folder or file in the artifact.如果要下载工件中的指定子文件夹或文件。
  • To download a specified sub-folder in the artifact, you can execute the following endpoint.要下载工件中的指定子文件夹,您可以执行以下端点。

     GET https://artprodcus3.artifacts.visualstudio.com/{organization_ID}/{project_ID}/_apis/artifact/{object_ID}/content?format=zip&subPath={/path/to/the/folder}

    For example:例如:

     GET https://artprodcus3.artifacts.visualstudio.com/{organization_ID}/{project_ID}/_apis/artifact/{object_ID}/content?format=zip&subPath=/ef-tools

    This will download the folder " ef-tools " and its content as a ZIP file from your artifact " drop ".这将从您的工件“ drop ”下载文件夹“ ef-tools ”及其内容作为 ZIP 文件。

  • To download a specified file in the artifact, you can execute the following endpoint.要下载工件中的指定文件,您可以执行以下端点。

     GET https://artprodcus3.artifacts.visualstudio.com/{organization_ID}/{project_ID}/_apis/artifact/{object_ID}/content?format=file&subPath={/path/to/the/file}

    For example:例如:

     GET https://artprodcus3.artifacts.visualstudio.com/{organization_ID}/{project_ID}/_apis/artifact/{object_ID}/content?format=file&subPath=/ef-tools/migrate.exe

    This will download the file " ef-tools/migrate.exe " from your artifact " drop ".这将从您的工件“ drop ”下载文件“ ef-tools/migrate.exe ”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM