0
votes

I'm posting this question to share my investigation about this issue and to ask for help in case anyone found something interesting.

Context

We are using Service Hooks with classic release pipeline for various purposes. We use Service Hooks trigger on event Release deployment completed. It's was very easy to setup.

When triggered by Azure DevOps, those events are stored for use in an external system. In this external system, we use Azure DevOps REST API to retrieve all the information we need.

Issue

Since YAML pipelines were released in GA, they are slowly replacing our classic release pipelines and we noticed that the existing Service Hooks were not longer triggered for YAML pipelines.

After some investigation, I discovered that those YAML pipelines doesn't trigger existing Release deployment completed events. Instead, there is a new publisher named pipelines that contains new events that are triggered for YAML pipelines.

You can use the REST API to get all those events :

GET https://dev.azure.com/{{organization}}/_apis/hooks/publishers/pipelines?api-version=6.1-preview.1

Only 2 events looks promising for our needs here:

  • Run stage state changed: A new stage has started, or a stage has transitioned to canceling, canceled, failed, partially succeeded or succeeded
  • Run state changed: A new run has started, or a run has transitioned to canceling, canceled, failed, partially succeeded or succeeded

Those events are triggered for ALL pipelines: YAML, classic build and release.

Looking into those new events

Now I have found interesting events, we need to find a way to detect a deployment done using a deployment job in a YAML pipeline (released in GA last year). I'm digging to discover what I can get from them. First step is to setup a new Service Hook with those new events, run a pipeline and get the payload sent when the event is triggered.

Here is a sample payload from the event stage-state-changed-event :

{
    "id": "37f42f2c-3061-4c0a-a0ff-e67d235ecfec",
    "eventType": "ms.vss-pipelines.stage-state-changed-event",
    "publisherId": "pipelines",
    "message": {
        "text": "Run 1.21.027.2 stage DeployStage running.",
        "html": "Run 1.21.027.2 stage <a href=\"https://dev.azure.com/yourorganizationhere/19d42179-19ab-4242-aa1c-3f8e533daaaa/_build/results?buildId=248532\">DeployStage</a> running.",
        "markdown": "Run 1.21.027.2 stage [DeployStage](https://dev.azure.com/yourorganizationhere/19d42179-19ab-4242-aa1c-3f8e533daaaa/_build/results?buildId=248532) running."
    },
    "detailedMessage": {
        "text": "Run 1.21.027.2 stage DeployStage running.",
        "html": "Run 1.21.027.2 stage <a href=\"https://dev.azure.com/yourorganizationhere/19d42179-19ab-4242-aa1c-3f8e533daaaa/_build/results?buildId=248532\">DeployStage</a> running.",
        "markdown": "Run 1.21.027.2 stage [DeployStage](https://dev.azure.com/yourorganizationhere/19d42179-19ab-4242-aa1c-3f8e533daaaa/_build/results?buildId=248532) running."
    },
    "resource": {
        "stage": {
            "_links": {
                "web": {
                    "href": "https://dev.azure.com/yourorganizationhere/19d42179-19ab-4242-aa1c-3f8e533daaaa/_build/results?buildId=248532"
                },
                "pipeline.web": {
                    "href": "https://dev.azure.com/yourorganizationhere/19d42179-19ab-4242-aa1c-3f8e533daaaa/_build/definition?definitionId=1545"
                }
            },
            "id": "6884a131-87da-5381-61f3-d7acc3b91d76",
            "name": "DeployStage",
            "displayName": "DeployStage stage",
            "state": "running"
        },
        "run": {
            "id": 248532,
            "name": "1.21.027.2"
        },
        "pipeline": {
            "url": "https://dev.azure.com/yourorganizationhere/19d42179-19ab-4242-aa1c-3f8e533daaaa/_apis/pipelines/1545?revision=9",
            "id": 1545,
            "revision": 9,
            "name": "Pipeline Definition Name Here",
            "folder": "\\"
        },
        "runId": 248532,
        "stageName": "DeployStage",
        "runUrl": "https://dev.azure.com/yourorganizationhere/19d42179-19ab-4242-aa1c-3f8e533daaaa/_apis/pipelines/1545/runs/248532"
    },
    "resourceVersion": "5.1-preview.1",
    "createdDate": "2021-01-27T09:40:51.0986307Z"
}

As you can see, this payload contains only generic information. You have the stage name and the pipeline id and name.

Next step, trying to query REST API to get more information about the pipeline, run or stages.
A stage can contains multiple jobs and we are interested in a specific kind of job: deployment job.

Pipelines endpoints

Pipelines REST API endpoints are still in preview, you need to use version 6.1-preview.1.

I tried to get some information from those endpoints:

GET https://dev.azure.com/{organization}/{project}/_apis/pipelines/{pipelineId}?api-version=6.1-preview.1
GET https://dev.azure.com/{organization}/{project}/_apis/pipelines/{pipelineId}/runs/{runId}?api-version=6.1-preview.1
GET https://dev.azure.com/{organization}/{project}/_apis/pipelines/{pipelineId}/runs/{runId}/logs?api-version=6.1-preview.1

I didn't find anything useful in the data returned. No way to detect that the event triggered could be related to a deployment job done with a YAML pipeline.

Summarize

When using classic pipelines, it was easy to detect a deployment was completed using Service Hooks. I didn't find the equivalent for YAML pipelines.

Am I missing something ?
Or is this yet another feature that isn't available yet for YAML pipelines ?
Any idea how to track deployments done with YAML pipelines ?

Any help appreciated ????
Thank you for your time.

1

1 Answers

0
votes

You are correct that the available event types for Pipelines are as below:

  • Run state changed
  • Run stage state changed
  • Run stage waiting for approval
  • Run stage approval completed

You can by default detect a stage state changed using Service Hooks only in a YAML pipeline. You could add a script task after the deployment, once the deployment succeeded send the message via API in the script to the service.