Search code examples
azureazure-devopsazure-pipelines-build-taskazure-pipelines-tasks

How to get Azure pipeline Task Status for a specific task?


My purpose is to get the status of a specific pipeline task in a job.

I've tried to send an API call referencing the method at the Reference Link but it only returned the running log of the task.

Is there exist any method to get the status information of a a pipeline task? Can we achieve to get status foe one task?

Any suggestions and directives is appreciated.


Solution

  • For release pipeline, I think this API should meet your requirements:

    https://vsrm.dev.azure.com/<Organization Name>/<Project Name>/_apis/Release/releases/<Release ID>?api-version=6.0-preview.6

    Sample response:

    {
        ...,
        "environments": [
            {
                ...,
                },
                "deploySteps": [
                    {...,
                        "releaseDeployPhases": [
                            {...,
                                "deploymentJobs": [
                                    {
                                        "job": {
                                            "id": 8,
                                            "timelineRecordId": "9a737f0c-559e-5660-b520-9a7c39e93149",
                                            "name": "Agent job",
                                            "dateStarted": "2022-07-22T07:46:39.81Z",
                                            "dateEnded": "2022-07-22T07:46:46.2633333Z",
                                            "startTime": "2022-07-22T07:46:39.81Z",
                                            "finishTime": "2022-07-22T07:46:46.2633333Z",
                                            "status": "failed",
                                            "rank": 1,
                                            "issues": [],
                                            "agentName": "Hosted Agent",
                                            "logUrl": "https://vsrm.dev.azure.com/xxx/c6358b04-e91a-4bd1-a894-1adb543134d6/_apis/Release/releases/37/environments/37/deployPhases/41/tasks/8/logs"
                                        },
                                        "tasks": [
                                            {
                                                "id": 4,
                                                "timelineRecordId": "0f34296d-9707-4f4e-a489-e841851c4f47",
                                                "name": "Initialize job",
                                                "dateStarted": "2022-07-22T07:46:40.1733333Z",
                                                "dateEnded": "2022-07-22T07:46:41.7833333Z",
                                                "startTime": "2022-07-22T07:46:40.1733333Z",
                                                "finishTime": "2022-07-22T07:46:41.7833333Z",
                                                "status": "succeeded",
                                                "rank": 1,
                                                "issues": [],
                                                "agentName": "Hosted Agent",
                                                "logUrl": "https://vsrm.dev.azure.com/xxx/c6358b04-e91a-4bd1-a894-1adb543134d6/_apis/Release/releases/37/environments/37/deployPhases/41/tasks/4/logs"
                                            },
                                            {
                                                "id": 5,
                                                "timelineRecordId": "f9eb04f6-a1bd-4d30-86ed-d32704f0f6ec",
                                                "name": "Download Artifacts",
                                                "dateStarted": "2022-07-22T07:46:41.7933333Z",
                                                "dateEnded": "2022-07-22T07:46:42.5233333Z",
                                                "startTime": "2022-07-22T07:46:41.7933333Z",
                                                "finishTime": "2022-07-22T07:46:42.5233333Z",
                                                "status": "succeeded",
                                                "rank": 2,
                                                "issues": [],
                                                "agentName": "Hosted Agent",
                                                "logUrl": "https://vsrm.dev.azure.com/xxx/c6358b04-e91a-4bd1-a894-1adb543134d6/_apis/Release/releases/37/environments/37/deployPhases/41/tasks/5/logs"
                                            },
                                            {
                                                "id": 6,
                                                "timelineRecordId": "ee21b6e5-e611-5d0a-7967-2a6e400a3b0f",
                                                "name": "Extract files ",
                                                "dateStarted": "2022-07-22T07:46:42.5233333Z",
                                                "dateEnded": "2022-07-22T07:46:46.2433333Z",
                                                "startTime": "2022-07-22T07:46:42.5233333Z",
                                                "finishTime": "2022-07-22T07:46:46.2433333Z",
                                                "status": "failed",
                                                "rank": 3,
                                                "issues": [
                                                    {
                                                        "issueType": "Error",
                                                        "message": "Failed rmRF: Command failed: rd /s /q \"D:\\a\\r1\\a\"\nThe process cannot access the file because it is being used by another process.\r\n",
                                                        "data": {
                                                            "type": "error",
                                                            "logFileLineNumber": "16"
                                                        }
                                                    }
                                                ],
                                                "task": {
                                                    "id": "5e1e3830-fbfb-11e5-aab1-090c92bc4988",
                                                    "name": "ExtractFiles",
                                                    "version": "1.200.0"
                                                },
                                                "agentName": "Hosted Agent",
                                                "logUrl": "https://vsrm.dev.azure.com/xxx/c6358b04-e91a-4bd1-a894-1adb543134d6/_apis/Release/releases/37/environments/37/deployPhases/41/tasks/6/logs"
                                            },
                                            {
                                                "id": 0,
                                                "timelineRecordId": "eae0669a-e2ad-530b-f008-4105bdd5d304",
                                                "name": "PowerShell Script",
                                                "dateStarted": "2022-07-22T07:46:46.2433333Z",
                                                "dateEnded": "2022-07-22T07:46:46.2466667Z",
                                                "startTime": "2022-07-22T07:46:46.2433333Z",
                                                "finishTime": "2022-07-22T07:46:46.2466667Z",
                                                "status": "skipped",
                                                "rank": 4,
                                                "issues": [],
                                                "task": {
                                                    "id": "e213ff0f-5d5c-4791-802d-52ea3e7be1f1",
                                                    "name": "PowerShell",
                                                    "version": "2.200.0"
                                                },
                                                "agentName": "Hosted Agent",
                                                "logUrl": "",
                                                "resultCode": "Evaluating: succeeded()\r\nResult: False\r\n"
                                            },
                                            {
                                                "id": 7,
                                                "timelineRecordId": "cfbbae06-d8fc-4d1e-a79b-f920071e5481",
                                                "name": "Finalize Job",
                                                "dateStarted": "2022-07-22T07:46:46.25Z",
                                                "dateEnded": "2022-07-22T07:46:46.2566667Z",
                                                "startTime": "2022-07-22T07:46:46.25Z",
                                                "finishTime": "2022-07-22T07:46:46.2566667Z",
                                                "status": "succeeded",
                                                "percentComplete": 100,
                                                "rank": 5,
                                                "issues": [],
                                                "agentName": "Hosted Agent",
                                                "logUrl": "https://vsrm.dev.azure.com/xxx/c6358b04-e91a-4bd1-a894-1adb543134d6/_apis/Release/releases/37/environments/37/deployPhases/41/tasks/7/logs"
                                            }
                                        ]
                                    }
                                ],
                                "manualInterventions": [],
                                "startedOn": "2022-07-22T07:46:36.16Z"
                            }
                        ],...,
                }
            }
        ],
        "variables": {},
        "variableGroups": [],
        "artifacts": [
            {
                "sourceId": "c6358b04-e91a-4bd1-a894-1adb543134d6:221",
                "type": "Build",
                "alias": "_buildartifact",
                "definitionReference": {
                    "artifactSourceDefinitionUrl": {
                        "id": "https://dev.azure.com/xxx/_permalink/_build/index?collectionId=b1cc953d-b564-4eec-a222-84393e4406b1&projectId=c6358b04-e91a-4bd1-a894-1adb543134d6&definitionId=221",
                        "name": ""
                    },
                    "branches": {
                        "id": "main",
                        "name": "main"
                    },
                    "buildUri": {
                        "id": "vstfs:///Build/Build/2119",
                        "name": null
                    },
                    "definition": {
                        "id": "221",
                        "name": "buildartifact"
                    },
                    "IsMultiDefinitionType": {
                        "id": "False",
                        "name": "False"
                    },
                    "IsXamlBuildArtifactType": {
                        "id": "False",
                        "name": null
                    },
                    "project": {
                        "id": "c6358b04-e91a-4bd1-a894-1adb543134d6",
                        "name": "xxx"
                    },
                    "repository.provider": {
                        "id": "TfsGit",
                        "name": null
                    },
                    "repository": {
                        "id": "a433b7a0-a23c-4c18-9d14-d0859568dec3",
                        "name": "buildartifact"
                    },
                    "requestedFor": {
                        "id": "15365687987",
                        "name": null
                    },
                    "requestedForId": {
                        "id": "af91e22a-cc35-4c8e-8af3-f49c4a1b9b6a",
                        "name": null
                    },
                    "sourceVersion": {
                        "id": "3d0b3f71bc2391b95325fabb0b3b48a8ade2014f",
                        "name": null
                    },
                    "version": {
                        "id": "2119",
                        "name": "20220722.1"
                    },
                    "artifactSourceVersionUrl": {
                        "id": "https://dev.azure.com/xxx/_permalink/_build/index?collectionId=b1cc953d-b564-4eec-a222-84393e4406b1&projectId=c6358b04-e91a-4bd1-a894-1adb543134d6&buildId=2119",
                        "name": ""
                    },
                    "branch": {
                        "id": "refs/heads/main",
                        "name": "refs/heads/main"
                    }
                },
                "isPrimary": true,
                "isRetained": true
            }
        ],
        "releaseDefinition": {
            "id": 9,
            "name": "DownloadArtifactAndShow",
            "path": "\\",
            "projectReference": null,
            "url": "https://vsrm.dev.azure.com/xxx/c6358b04-e91a-4bd1-a894-1adb543134d6/_apis/Release/definitions/9",
            "_links": {
                "self": {
                    "href": "https://vsrm.dev.azure.com/xxx/c6358b04-e91a-4bd1-a894-1adb543134d6/_apis/Release/definitions/9"
                },
                "web": {
                    "href": "https://dev.azure.com/xxx/c6358b04-e91a-4bd1-a894-1adb543134d6/_release?definitionId=9"
                }
            }
        },
        ...
    }
    

    You can see that all of the tasks' situations are in the response. The status of the task is also there.

    For a build pipeline, no public API to get the status of the task directly, you need to analyze the logs in the response to get the status of the task.

    https://learn.microsoft.com/en-us/rest/api/azure/devops/build/builds/get-build-logs?view=azure-devops-rest-6.0