簡體   English   中英

如何添加到 Azure Devops 管道中構建的擴展屬性集合

[英]How can I add to the extended properties collection of a build in Azure Devops pipelines

我正在使用 Azure DevOps 的 .NET 客戶端庫來獲取有關我的構建的信息並將它們顯示在網站上。 我想獲得正在構建的應用程序的程序集版本以顯示在 web 站點上。

在這里看到有一組任意鍵值對,我可以通過 REST API(以及 dotnet 客戶端庫)獲得。 但是如何在構建過程中設置這些屬性?

我想獲得正在構建的應用程序的程序集版本以顯示在 web 站點上。

對於知道的程序集版本,請使用ILDASM、ILSpy Reflector或 Jetbrains dotpeek 等工具。

ILDASM 將在以下路徑中可用:

C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\ildasm.exe

其中 v8.1A 安裝 Windows SDK 的版本。

ILDASM: Ildasmtooltoseeversion

在此處輸入圖像描述

在此處輸入圖像描述

如何在構建過程中設置這些屬性? 您需要以環境變量的形式設置擴展屬性。

您可以使用帶有表達式的變量來有條件地分配值並進一步自定義管道。 有關詳細信息,請參閱 Microsoft Doc

使用這個名為 SetBuildProperties.yml 的 YAML 任務模板在管道中添加構建屬性:

SetBuildProperties.yml:

# Sets custom build properties. Use this to transfer info between pipelines.
parameters:

- name: buildId
  type: string

steps:
  
- task: PowerShell@2
  displayName: 'Set build properties'
  name: 'SetBuildProperties'
  inputs:
    targetType: 'inline'
    script: |

        # prepare the auth token
        $authHeader = "Bearer $(System.AccessToken)"

        $requestHeader = @{
            "Authorization" = $authHeader
            "Accept" = "application/json"
        }

        # array of operations
        # @{} is a hashtable in PowerShell. 
        # @() is an array (list) in PowerShell.
        $paramsObject = 
        @(
            @{
                "op" = "add"
                "path" = "/myProperty"
                "value" = "myValue"
            }
        )

        $params = ConvertTo-Json -InputObject $paramsObject

        Write-Host "JSON: " $params

        $contentType = "application/json-patch+json;charset=utf-8"        


        $apiurl = "https://dev.azure.com/MyOrg/MyProject/_apis/build/builds/${{ parameters.buildId }}/properties?api-version=6.0"
        
        Write-Host "Invoking REST API url: " $apiurl

        try 
        {
            $properties = Invoke-RestMethod -Uri $apiurl -Method PATCH -Headers $requestHeader -Body $params -ContentType $contentType

        }
        catch
        {
            # Writes an error to build summary and to log in red text
            Write-Host "##vso[task.LogIssue type=error;]Error occurred: " $_

            exit 1
        }

如果您需要讀取相同或不同管道中的屬性,可以使用此模板 GetBuildProperties.yml:

# Gets custom build properties. Use this to transfer info between pipelines.
parameters:

- name: buildId
  type: string

steps:
  
- task: PowerShell@2
  displayName: 'Get build properties'
  name: 'GetBuildProperties'
  inputs:
    targetType: 'inline'
    script: |

        # prepare the auth token
        $authHeader = "Bearer $(System.AccessToken)"

        $requestHeader = @{
            "Authorization" = $authHeader
            "Accept" = "application/json"
        }

        $apiurl = "https://dev.azure.com/MyOrg/MyCompany/_apis/build/builds/${{ parameters.buildId }}/properties?api-version=6.0"
        
        Write-Host "Invoking REST API url: " $apiurl

        try 
        {
            $properties = Invoke-RestMethod -Uri $apiurl -Method Get -Headers $requestHeader

            $propertiesJSON = ConvertTo-Json -InputObject $properties
            Write-Host 'properties: ' $propertiesJSON
        }
        catch
        {
            # Writes an error to build summary and to log in red text
            Write-Host "##vso[task.LogIssue type=error;]Error occurred: " $_

            exit 1
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM