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

Viewed 146

I am using the .NET client libraries for Azure DevOps to get information about my builds and display them on a website. I want to get the assembly version of my application that is being built to display on the web site.

I see here that there is a collection of arbitrary key-value pairs that I can get via the REST API (and also with the dotnet client library). But how do I set these properties during my build?

2 Answers

I want to get the assembly version of my application that is being built to display on the web site.

For the assembly version knowing, use either of the tools like ILDASM, ILSpy, Reflector or Jetbrains dotpeek.

ILDASM will be available in the path of:

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

where v8.1A is installed Windows SDK's version.

ILDASM: Ildasmtooltoseeversion

enter image description here

enter image description here

how do I set these properties during my build? You need to set extended properties in the form of environment variables.

You can use variables with expressions to conditionally assign values and further customize pipelines. Refer to Microsoft Doc for more information.

Use this YAML task template called SetBuildProperties.yml to add build properties in a pipeline:

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
        }

If you need to read the properties in the same or a different pipeline you can use this template 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
        }
Related