I work for Microsoft as an App Innovation Cloud Solution Architect in the UK. The posts and comments on this site are my own opinions and are not endorsed in any way by my employer.

Automatically tag build runs in Azure DevOps.

One little known feature of Azure DevOps Pipelines is the ability to tag a pipeline run. By default this is a manual process.
Once the build has completed you select Edit Tags and add the values.

The tags then show up under the title for that run.

You can add these tags using the REST API. This article has a good description of how to get an authentication token for the api

and then add tags to a build run using the following API call.

PUT https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}/tags/{tag}?api-version=6.0
Authorization: Basic {your base64 encoded PAT}

or alternatively you can add multiples using the plural version

POST https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}/tags?api-version=6.0
Authorization: Basic {your base64 encoded PAT}
Content-Type: application/json

[ "tag1", "tag2"]

If you want to add tags automatically on every build you need to use the Definition Tag(s) APIs, so to add a single tag use

PUT https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{DefinitionId}/tags/{tag}?api-version=6.0-preview.2
Authorization: Basic {your base64 encoded PAT}

and for multiples

POST https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{DefinitionId}/tags?api-version=6.0-preview.2
Authorization: Basic {your base64 encoded PAT}
Content-Type: application/json

[ "tag1", "tag2"]

Unfortunately these definition tags don’t seem to be surfaced in the portal anywhere so to retrieve them use the Get API.

GET https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{DefinitionId}/tags?api-version=6.0-preview.2
Authorization: Basic {your base64 encoded PAT}

This will return a JSON list of the tags applied to the Pipeline definition.

{
    "count": 4,
    "value": [
        "hello",
        "world",
        "test1",
        "test2"
    ]
}