【问题标题】:How to get filename strings from Azure DevOps artifact build如何从 Azure DevOps 工件构建中获取文件名字符串
【发布时间】:2022-12-16 10:51:36
【问题描述】:

我想创建一个脚本来计算特定文件在工件构建中出现的次数。 我正在尝试从 Azure DevOps Rest Apis 获取工件文件的名称,但它似乎不会退出任何 API 调用来获取该信息。 这些是我用来从特定管道获取工件名称的 API 调用:

  1. 此 api 获取特定管道的所有构建:https://learn.microsoft.com/en-us/rest/api/azure/devops/pipelines/runs/run-pipeline?view=azure-devops-rest-7.1
    GET https://dev.azure.com/{organization}/{project}/_apis/pipelines/{pipelineId}/runs/{runId}?api-version=7.1-preview.1
    
    1. 当我获得该管道的所有构建 ID 时,我可以开始搜索工件:
    GET https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}/artifacts?api-version=7.0
    

    现在我拥有特定构建的所有工件,但我不知道如何获取工件的文件名字符串。我在 Azure DevOps 文档中找到的唯一内容是一个 REST API 调用,用于下载工件的特定文件:

    https://learn.microsoft.com/en-us/rest/api/azure/devops/pipelines/artifacts/get?view=azure-devops-rest-7.1

    GET https://dev.azure.com/{organization}/{project}/_apis/pipelines/{pipelineId}/runs/{runId}/artifacts?artifactName={artifactName}&$expand={$expand}&api-version=7.1-preview.1
    

    或者

    https://learn.microsoft.com/en-us/rest/api/azure/devops/build/artifacts/get-artifact?view=azure-devops-rest-7.1

    GET GET https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}/artifacts?artifactName={artifactName}&api-version=7.1-preview.5
    

    我期望找到一个 REST API,它还可以获取工件内部文件的一些信息。

    有人知道如何获取该信息吗?

【问题讨论】:

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


    【解决方案1】:

    您可以使用以下可以在网络跟踪中捕获的 API:

    POST https://dev.azure.com/{org}/_apis/Contribution/HierarchyQuery?api-version=5.0-preview
    

    这是示例请求正文:

    {
        "contributionIds": [
            "ms.vss-build-web.run-artifacts-data-provider"
        ],
        "dataProviderContext": {
            "properties": {
                "artifactId": {artifactId},
                "buildId": {buildId},
                "sourcePage": {
                    "routeValues": {
                        "project": "{YourProjectName}"
                    }
                }
            }
        }
    }
    

    你会得到这样的回应:

    {
        "dataProviderSharedData": {},
        "dataProviders": {
            "ms.vss-web.component-data": {},
            "ms.vss-web.shared-data": null,
            "ms.vss-build-web.run-artifacts-data-provider": {
                "buildId": 1487,
                "buildNumber": "1487",
                "definitionId": 176,
                "definitionName": "test5-CI",
                "items": [
                    {
                        "artifactId": 117,
                        "name": "/TestResults",
                        "sourcePath": "/TestResults",
                        "size": 0,
                        "type": "directory",
                        "items": null
                    },
                    {
                        "artifactId": 117,
                        "name": "/a",
                        "sourcePath": "/a",
                        "size": 0,
                        "type": "directory",
                        "items": null
                    },
                    {
                        "artifactId": 117,
                        "name": "/b",
                        "sourcePath": "/b",
                        "size": 0,
                        "type": "directory",
                        "items": null
                    },
                    {
                        "artifactId": 117,
                        "name": "/s",
                        "sourcePath": "/s",
                        "size": 988,
                        "type": "directory",
                        "items": [
                            {
                                "artifactId": 117,
                                "name": "/s/README.md",
                                "sourcePath": "/s/README.md",
                                "size": 985,
                                "type": "file",
                                "items": null
                            },
                            {
                                "artifactId": 117,
                                "name": "/s/folder1",
                                "sourcePath": "/s/folder1",
                                "size": 3,
                                "type": "directory",
                                "items": [
                                    {
                                        "artifactId": 117,
                                        "name": "/s/folder1/file1",
                                        "sourcePath": "/s/folder1/file1",
                                        "size": 3,
                                        "type": "file",
                                        "items": null
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        }
    }
    

    供您参考,这是 Azure DevOps 中的工件:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-07
      • 1970-01-01
      • 1970-01-01
      • 2022-09-27
      • 2019-06-27
      • 1970-01-01
      • 2021-04-13
      • 1970-01-01
      相关资源
      最近更新 更多