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?
I want to get the assembly version of my application that is being built to display on the web site.
You can use Reflector, ILDASM, Jetbrains dotpeek or ILSpy to get the assembly version.
You usually can find ILDASM in C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\ildasm.exe (where v8.1A is the version of the Windows SDK installed).
ILDASM: Ildasmtooltoseeversion
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.
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
}