Azure DevOps API get status of previous release task - azure-devops

Does anyone know the api to get the status of the individual release task in a release?
I can get the status of the release itself but not the individual release tasks (PowerShell tasks in my case). https://learn.microsoft.com/en-us/rest/api/azure/devops/release/releases/get%20release?view=azure-devops-rest-6.0
Also if anyone knows where the source code is for ADO built in tasks condition. can you please share the link?

Does anyone know the api to get the status of the individual release task in a release?
Method 1: You can use a REST API that is not documented:
GET https://vsrm.dev.azure.com/{organization}/{project}/_apis/Release/releases/{releaseId}/environments/{environmentId}/attempts/{attemptId}/timelines/{timelineId}/tasks
It returns a brief overview and status(succeeded, inProgress, pending, failed...) of all tasks in the release.
The response body looks like the following example:
{
"count": 9,
"value": [
{
"id": 4,
"timelineRecordId": "d81751cc-e42f-407a-9091-f611d37df33b",
"name": "Initialize job",
"dateStarted": "2021-04-27T06:01:21.5233333Z",
"dateEnded": "2021-04-27T06:01:23.32Z",
"startTime": "2021-04-27T06:01:21.5233333Z",
"finishTime": "2021-04-27T06:01:23.32Z",
"status": "succeeded",
"rank": 1,
"lineCount": 121,
"issues": [],
"agentName": "Hosted Agent",
"logUrl": "https://vsrm.dev.azure.com/{organization}/{project}/_apis/Release/releases/6/environments/6/deployPhases/6/tasks/4/logs"
},
{
"id": 5,
"timelineRecordId": "68716c38-638d-551b-6964-2da9e273edef",
"name": "Command Line Script",
"dateStarted": "2021-04-27T06:01:23.33Z",
"dateEnded": "2021-04-27T06:01:28.5833333Z",
"startTime": "2021-04-27T06:01:23.33Z",
"finishTime": "2021-04-27T06:01:28.5833333Z",
"status": "succeeded",
"rank": 2,
"lineCount": 15,
"issues": [],
"task": {
"id": "d9bafed4-0b18-4f58-968d-86655b4d2ce9",
"name": "CmdLine",
"version": "2.182.0"
},
"agentName": "Hosted Agent",
"logUrl": "https://vsrm.dev.azure.com/{organization}/{project}/_apis/Release/releases/6/environments/6/deployPhases/6/tasks/5/logs"
},
{
"id": 6,
"timelineRecordId": "fced85f1-31cf-59f2-d2e0-ccd1645b2427",
"name": "Command Line Script",
"dateStarted": "2021-04-27T06:01:28.5866667Z",
"startTime": "2021-04-27T06:01:28.5866667Z",
"status": "inProgress",
"percentComplete": 0,
"rank": 3,
"issues": [],
"task": {
"id": "d9bafed4-0b18-4f58-968d-86655b4d2ce9",
"name": "CmdLine",
"version": "2.182.0"
},
"agentName": "Hosted Agent",
"logUrl": ""
}
]
}
Method 2: You can use the REST API Releases - Get Task Log:
GET https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/releases/{releaseId}/environments/{environmentId}/deployPhases/{releaseDeployPhaseId}/tasks/{taskId}/logs?api-version=6.0-preview.2
It returns the running log of a specific task.
The response body looks like the following example:
2021-04-27T05:50:50.3443852Z ##[section]Starting: Command Line Script
2021-04-27T05:50:50.4313144Z ==============================================================================
2021-04-27T05:50:50.4313918Z Task : Command line
2021-04-27T05:50:50.4314555Z Description : Run a command line script using Bash on Linux and macOS and cmd.exe on Windows
2021-04-27T05:50:50.4314878Z Version : 2.182.0
2021-04-27T05:50:50.4315342Z Author : Microsoft Corporation
2021-04-27T05:50:50.4315927Z Help : https://learn.microsoft.com/azure/devops/pipelines/tasks/utility/command-line
2021-04-27T05:50:50.4316336Z ==============================================================================
2021-04-27T05:50:53.2219858Z Generating script.
2021-04-27T05:50:53.2572235Z ========================== Starting Command Output ===========================
2021-04-27T05:50:53.2972287Z ##[command]"C:\Windows\system32\cmd.exe" /D /E:ON /V:OFF /S /C "CALL "D:\a\_temp\fda7641b-b8a2-4d04-8f83-94f5eda9c3b6.cmd""
2021-04-27T05:50:53.3085688Z Write your commands here
2021-04-27T05:50:53.3086888Z Hello world
2021-04-27T05:50:53.3788822Z ##[section]Finishing: Command Line Script
If anyone knows where the source code is for ADO built in tasks condition.
I don't think Azure DevOps built-in tasks have conditions source code, because conditions can be used not only for built-in tasks, but for other tasks, jobs, stages, etc... It is supported by YAML Schema and Azure DevOps classic UI pipeline, but it has nothing to do with specific tasks.
Click this github link for Azure DevOps built-in task source code.

Each of your PowerShell tasks is an environment; therefore, you can access through the environments that is returned by "Get Release" API. See https://learn.microsoft.com/en-us/rest/api/azure/devops/release/releases/get%20release?view=azure-devops-rest-6.0#releaseenvironment

Related

How to add task in Azure DevOps release pipeline using rest API

I want to add task in Azure DevOps release pipeline using rest API -
Here is rest API for updating release pipeline -
PUT https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/definitions?api-version=6.0
for example if I want to add powershell task in my existing release pipeline in Dev stage, What request body I need to put ?
After testing(clicking F12 to capture corresponding browser network requests), this request body for this API: Definitions - Update is the same content when you call this API: Definitions - Get to get its definition.
Therefore, you could get the release pipeline definition firstly, and then add below PowerShell task code segment to workflowTasks array, which locates in the result's environments array >>dev stage>>deployPhases array>>workflowTasks array
{
"environment": {},
"taskId": "e213ff0f-5d5c-4791-802d-52ea3e7be1f1",
"version": "2.*",
"name": "PowerShell Script test",
"refName": "",
"enabled": true,
"alwaysRun": false,
"continueOnError": false,
"timeoutInMinutes": 0,
"definitionType": "task",
"overrideInputs": {},
"condition": "succeeded()",
"inputs": {
"targetType": "inline",
"filePath": "",
"arguments": "",
"script": "# Write your PowerShell commands here.\n\nWrite-Host \"Hello World\"\n",
"errorActionPreference": "stop",
"failOnStderr": "false",
"showWarnings": "false",
"ignoreLASTEXITCODE": "false",
"pwsh": "false",
"workingDirectory": ""
}
}
And then there will an extra PowerShell task in Dev stage.
BTW, each updates will generate a new revision, which is the latest version of release definition, and you need to use the latest version to update release pipeline the next time.

How to execute a PowerShell Command from within Azure Data Factory custom activity?

I have a custom activity in Azure Data Factory, which attempts to execute the following command:
PowerShell.exe -Command "Write-Host 'Hello, world!'"
However, when I debug (run) this command from within Azure Data Factory, it runs for a long time, and finally fails.
I guess it fails because perhaps it could not locate "PowerShell.exe". How can I ensure that the ADF Custom Activity has access to PowerShell.exe?
Some sites say about specifying a package (.zip file) that contains everything needed for the exe to execute. However, since PowerShell is from Microsoft, I think it would be inappropriate to ZIP the PowerShell directory, and specify it as a package to the Custom Activity.
Please suggest as to how I can execute PowerShell command from Custom Activity of an Azure Data Factory. Thanks!
Whenever I search "Execute PowerShell from Custom Activity in Azure Data Factory", the search results are talking more about which Az PowerShell command to use to trigger start an ADF pipeline.
I saw two threads in Stackoverflow.com, where the answer just specifies to use a Custom Activity, and the answer is not specific to PowerShell command call from ADF
Here is the JSON for the task:
{
"name": "ExecutePs1CustomActivity",
"properties": {
"activities": [
{
"name": "ExecutePSScriptCustomActivity",
"type": "Custom",
"dependsOn": [],
"policy": {
"timeout": "7.00:00:00",
"retry": 0,
"retryIntervalInSeconds": 30,
"secureOutput": false,
"secureInput": false
},
"userProperties": [],
"typeProperties": {
"command": "PowerShell.exe -Command \"Write-Host 'Hello, world!'\"",
"referenceObjects": {
"linkedServices": [],
"datasets": []
}
},
"linkedServiceName": {
"referenceName": "Ps1CustomActivityAzureBatch",
"type": "LinkedServiceReference"
}
}
],
"annotations": []
}
}
I see "In Progress" for 3 minutes (180 seconds), and then it shows as "Failed."
I would suggest you to move all you scripting task in a powershell file and copy it to a storage account linked with your custom activity. . Once done try to call it like below:
powershell .\script.ps1
You can also provide the path of the script in json like below:
{
"name": "MyCustomActivityPipeline",
"properties": {
"description": "Custom activity sample",
"activities": [{
"type": "Custom",
"name": "MyCustomActivity",
"linkedServiceName": {
"referenceName": "AzureBatchLinkedService",
"type": "LinkedServiceReference"
},
"typeProperties": {
"command": "helloworld.exe",
"folderPath": "customactv2/helloworld",
"resourceLinkedService": {
"referenceName": "StorageLinkedService",
"type": "LinkedServiceReference"
}
}
}]
}
}
Please try it and see if it helps. Also i would suggest you to troubleshoot the pipeline steps to look for detailed error.
Also to your second point "Some sites say about specifying a package (.zip file) that contains everything needed for the exe to execute." This is required when you are building a custom activity using dot net then it is must copy all the Dll's and Exe's for execution.
Hope it helps.

Require build to Contain Unit tests VSTS

I have 1 question
We use VSTS in our company. Now I want to require my developers to write unit tests to each project
My question is: If I can to build in Team Service Web Portal to require contain Unit tests, and if project doesn't has any unit tests - set build fail?
Thank you.
You can add a PowerShell task to check if the build project contains unit tests. Detail steps as below:
Add a PowerShell task after VS test task, and get VS test build log by timeline REST API. For TFS the rest api format should be:
GET http://{tsserver}:8080/tfs/{collection}/{project}/_apis/build/builds/{buildId}/timeline?api-version=3.0
Then search the log for VS test task, and you can get the detail VS test build log by the url. Such as below example, VS test task build log can be find in http://wxv-xindo-12r2:8080/tfs/DefaultCollection/5dfb8b33-9949-4187-9d09-474c8fe87238/_apis/build/builds/1477/logs/5.
{
"id": "c43e4f8f-3db9-4fdc-90c0-1153f165b9a9",
"parentId": "39d66172-979a-46e8-a04d-fe8e4e77da43",
"type": "Task",
"name": "Test Assemblies",
"startTime": "2018-05-25T02:31:11.177Z",
"finishTime": "2018-05-25T02:31:17.133Z",
"currentOperation": null,
"percentComplete": null,
"state": "completed",
"result": "succeeded",
"resultCode": null,
"changeId": 17,
"lastModified": "0001-01-01T00:00:00",
"workerName": "mypc",
"order": 5,
"details": null,
"errorCount": 0,
"warningCount": 2,
"url": null,
"log": {
"id": 5,
"type": "Container",
"url": "http://tfsserver:8080/tfs/DefaultCollection/5dfb8b33-9949-4187-9d09-474c8fe87238/_apis/build/builds/1477/logs/5"
},
"task": {
"id": "ef087383-ee5e-42c7-9a53-ab56c98420f9",
"name": "VSTest",
"version": "2.0.55"
},
"issues": [
{
"type": "warning",
"category": "General",
"message": "",
"data": {
"type": "warning",
"code": "002004"
}
},
{
"type": "warning",
"category": "General",
"message": "No test assemblies found matching the pattern: **\\release\\*test*.dll,!**\\obj\\**.",
"data": {
"type": "warning"
}
}
]
}
Then check if the VS test task has the warning:
[warning]No test assemblies found matching the pattern: '**\*test*.dll;-:**\obj\**'
If the VS test build log contains above warning, fail the PowerShell task by exit 1.

TFS/VSTS vNext Build and Release Logs Location

Are the build and release logs saved locally on the Build Agent machine? I know that I can manually click on the "Download all logs as zip" link for each build and release, but if I want to automatically send these logs to someone else (or bulk send them), is there another way to find them (either on the Build Agent machine or in the database somewhere)?
Thanks!
Both for TFS and VSTS, the build/release logs are located in TFS/VSTS server (no business with what the agent is used).
In build/release retention, you can set the policies to keep the builds/releases. And it default keep the latest 30 days' builds and releases.
Except the way to click "Download all logs as zip" button to get a build/release logs, you can also get build/release logs by REST API. Such as below example:
GET https://account.visualstudio.com/DefaultCollection/Git2/_apis/build/builds/2373/timeline?api-version=2.0
And you can get each build step log in the response, such as:
{
"records": [
{
"id": "d2c6b274-40fe-4727-85b6-eb92fb4f6009",
"parentId": "ff7265dc-abe3-5e6a-6194-76bb88f00044",
"type": "Task",
"name": "Initialize Job",
"startTime": "2018-01-15T05:30:25.6Z",
"finishTime": "2018-01-15T05:30:26.0233333Z",
"currentOperation": null,
"percentComplete": null,
"state": "completed",
"result": "succeeded",
"resultCode": null,
"changeId": 8,
"lastModified": "0001-01-01T00:00:00",
"workerName": "V-myPC",
"order": 2,
"details": null,
"errorCount": 0,
"warningCount": 0,
"url": null,
"log": {
"id": 2,
"type": "Container",
"url": "https://account.visualstudio.com/DefaultCollection/f7855e29-6f8d-429d-8c9b-41fd4d7e70a4/_apis/build/builds/2373/logs/2"
},
"task": null
},
...
],
"lastChangedBy": "00000002-0000-8888-8000-000000000000",
"lastChangedOn": "2018-01-15T05:30:40.947Z",
"id": "4b4280d4-5358-4238-ab95-d44475c92bc9",
"changeId": 19,
"url": "https://account.visualstudio.com/DefaultCollection/f7855e29-6f8d-429d-8c9b-41fd4d7e70a4/_apis/build/builds/2373/Timeline/4b4280d4-5358-4238-ab95-d44475c92bc9"
}
As the above response, you can find the Initialized Job step by the url https://account.visualstudio.com/DefaultCollection/f7855e29-6f8d-429d-8c9b-41fd4d7e70a4/_apis/build/builds/2373/logs/2.

How to get reason for failure of a previous task in vsts CI build

Is there a way to get the reason for a previous task failing so I can use it in a later task. Right now I'm naming and shaming who ever breaks the build and linking to the commit that did it in slack. But you can't tell why it broke unless you go to vsts and look at the logs.
I didn't see any build variables that get set on failures. Right now the only thing I can think of is to route stderr to a file and the console and then on failure read that file into a variable. But that seems hacky.
After the task which you want to get the detail failed reason, you can add a PowerShell task to get the previous tasks’ details by Rest API.
Detail steps in the PowerShell task:
1. Execute the rest API to get all the previous build tasks’ details.
2. Search the failed task by task name or build result. An you can get the task’s detail as
{
"id": "b75e0dd2-9734-4e83-ab5b-dc6001ea037c",
"parentId": "78e591b0-98f5-4d8a-a46c-417fda2c36dc",
"type": "Task",
"name": "Run cd ",
"startTime": "2017-08-28T05:33:51.86Z",
"finishTime": "2017-08-28T05:33:53.053Z",
"currentOperation": null,
"percentComplete": null,
"state": "completed",
"result": "failed",
"resultCode": null,
"changeId": 12,
"lastModified": "0001-01-01T00:00:00",
"workerName": "machine",
"order": 4,
"details": null,
"errorCount": 1,
"warningCount": 0,
"url": null,
"log": {
"id": 3,
"type": "Container",
"url": "https://account.visualstudio.com/DefaultCollection/f7855e29-6f8d-429d-8c9b-41fd4d7e70a4/_apis/build/builds/1339/logs/3"
}
3. Then you can get the detail failed logs in the url option (as the
url https://account.visualstudio.com/DefaultCollection/f7855e29-6f8d-429d-8c9b-41fd4d7e70a4/_apis/build/builds/1339/logs/3 in above example).