Require build to Contain Unit tests VSTS - azure-devops

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.

Related

Azure Devops / Kudusync deploys wrong package to Azure Function

We have two Azure functions, let's call them A and B, that we deploy using Azure Devops. Looking at the deployment logs, first the right package is deployed followed by a deploy where the contents of Function App A are deployed to Function app B. How this happens is a mystery.
The strange things is, looking at the logs and what is inside Kudu, they don't match up. For example, the latest deploy logs will say this:
[
{
"log_time": "2021-09-17T10:57:17.3180234Z",
"id": "",
"message": "Command: \"D:\\home\\site\\deployments\\tools\\deploy.cmd\"",
"type": 0,
"details_url": null
},
{
"log_time": "2021-09-17T10:57:18.5999063Z",
"id": "",
"message": "Handling Basic Web Site deployment.",
"type": 0,
"details_url": null
},
{
"log_time": "2021-09-17T10:57:24.8723981Z",
"id": "",
"message": "Creating app_offline.htm",
"type": 0,
"details_url": null
},
{
"log_time": "2021-09-17T10:57:24.9036384Z",
"id": "",
"message": "KuduSync.NET from: 'D:\\local\\Temp\\zipdeploy\\extracted' to: 'D:\\home\\site\\wwwroot'",
"type": 0,
"details_url": null
},
{
"log_time": "2021-09-17T10:57:25.0941627Z",
"id": "",
"message": "Deleting file: 'OurCompany.Api.B.dll'",
"type": 0,
"details_url": null
},
"log_time": "2021-09-17T10:57:25.1557977Z",
"id": "",
"message": "Copying file: 'extensions.json'",
"type": 0,
"details_url": null
},
{
"log_time": "2021-09-17T10:57:25.1718472Z",
"id": "",
"message": "Copying file: 'OurCompany.Api.A.dll'",
"type": 0,
"details_url": null
},
{
"log_time": "2021-09-17T10:57:27.4441118Z",
"id": "",
"message": "Deleting app_offline.htm",
"type": 0,
"details_url": null
},
{
"log_time": "2021-09-17T10:57:27.5223301Z",
"id": "",
"message": "Finished successfully.",
"type": 0,
"details_url": null
}
]
But when I go to Kudu on the server of the OurCompany.Api.B function app and I look at the actual contents in D:\\local\\Temp\\zipdeploy\\extracted I see no OurCompany.Api.A.dll - only OurCompany.Api.B.dll. Note that the timestamp of the files in the extracted folder predate the second deployment, so they are from the first and CORRECT deploy.
If I look at the contents inside D:\\home\\site\\wwwroot I DO see the incorrect OurCompany.Api.A.dll but where this is coming from I have no idea.
So... how can the deploy logs mention the copying of incorrect dll files while they are not in the folder on the server, and never were there because the timestamps don't add up? We've been working on this issue for days now without succes.
Apparantly this is happening because both Functions are hosted on the same App Service plan and thus using the same Azure File Share. See this thread https://github.com/projectkudu/kudu/issues/3333.

Where can I find System.TeamProjectId for a project in Azure Devops

I need this Id upfront but I can't seem to find it anywhere online?
The documentation only refers to it as a system variable.
Context:
I have multiple projects and I want to identify a project during CI so it can run a powershell script hosted in another repository.
You can get the all team projects id with REST Api:
https://dev.azure.com/{organization}/_apis/projects?api-version=5.0-preview.3
Results:
{
"count": 3,
"value": [
{
"id": "eb6e4656-77fc-42a1-9181-4c6d8e9da5d1",
"name": "Fabrikam-Fiber-TFVC",
"description": "Team Foundation Version Control projects.",
"url": "https://dev.azure.com/fabrikam/_apis/projects/eb6e4656-77fc-42a1-9181-4c6d8e9da5d1",
"state": "wellFormed"
},
{
"id": "6ce954b1-ce1f-45d1-b94d-e6bf2464ba2c",
"name": "Fabrikam-Fiber-Git",
"description": "Git projects",
"url": "https://dev.azure.com/fabrikam/_apis/projects/6ce954b1-ce1f-45d1-b94d-e6bf2464ba2c",
"state": "wellFormed"
},
{
"id": "281f9a5b-af0d-49b4-a1df-fe6f5e5f84d0",
"name": "TestGit",
"url": "https://dev.azure.com/fabrikam/_apis/projects/281f9a5b-af0d-49b4-a1df-fe6f5e5f84d0",
"state": "wellFormed"
}
]
}
You don't even need use Postman or create Http request, just enter the API url above in the browser.
You can examine the HTML in the UI of the dashboard on your organisation projects page:

How to find a TFS release in progress?

I need to fail a build on TFS 2018 if its pipeline is not fully complete. Batching just the build is not enough; the linked release must be finished as well before another build can begin. My idea is to do this in a PowerShell script via the REST API.
I see in the official documentation here that there's a property called TaskStatus. It provides a value of inProgress, presumably for releases that are in progress. This might do the trick, but there's no indication of how to actually use it.
Using the REST API, how can I get the TaskStatus of a given release?
The in process and some other values such as succeeded, canceled just stand for the status of a task in release pipeline.
You could simply use the Rest API to get a release
GET https://fabrikam.vsrm.visualstudio.com/MyFirstProject/_apis/release/releases/{releaseId}?api-version=4.1-preview.6
There should be a value called status:
"id": 18,
"name": "Release-18",
"status": "abandoned",
"createdOn": "2017-06-16T01:36:20.397Z",
"modifiedOn": "2017-06-16T01:36:21.07Z",
"modifiedBy": {
"id": "4adb1680-0eac-6149-b5ee-fc8b4f6ca227",
"displayName": "Chuck Reinhart",
"uniqueName": "fabfiber#outlook.com",
"url": "https://app.vssps.visualstudio.com/A168224e4-29ff-4081-9954-c8780ce81117/_apis/Identities/4adb1680-0eac-6149-b5ee-fc8b4f6ca227",
"imageUrl": "https://fabfiber-inc.visualstudio.com/_api/_common/identityImage?id=4adb1680-0eac-6149-b5ee-fc8b4f6ca227"
},
"createdBy": {
"id": "4adb1680-0eac-6149-b5ee-fc8b4f6ca227",
"displayName": "Chuck Reinhart",
"uniqueName": "fabfiber#outlook.com",
"url": "https://app.vssps.visualstudio.com/A168224e4-29ff-4081-9954-c8780ce81117/_apis/Identities/4adb1680-0eac-6149-b5ee-fc8b4f6ca227",
"imageUrl": "https://fabfiber-inc.visualstudio.com/_api/_common/identityImage?id=4adb1680-0eac-6149-b5ee-fc8b4f6ca227"
},
"environments": [
{
"id": 69,
"releaseId": 18,
"name": "Dev",
"status": "notStarted",
"variables": {},
"preDeployApprovals": [],
"postDeployApprovals": [],
"preApprovalsSnapshot": {
"approvals": [
{
"rank": 1,
"isAutomated": false,
"isNotificationOn": false,
"approver": {
"id": "4adb1680-0eac-6149-b5ee-fc8b4f6ca227",
"displayName": "Chuck Reinhart",
"uniqueName": "fabfiber#outlook.com",
"url": "https://app.vssps.visualstudio.com/A168224e4-29ff-4081-9954-c8780ce81117/_apis/Identities/4adb1680-0eac-6149-b5ee-fc8b4f6ca227",
"imageUrl": "https://fabfiber-inc.visualstudio.com/_api/_common/identityImage?id=4adb1680-0eac-6149-b5ee-fc8b4f6ca227"
},
"id": 0
}
You could fetch the value status in your return json file, and judge if the release succeed or failed. Finally according to this status to trigger another build or not.
Update
A sample of the returned json with task's status info:
"deploymentJobs": [
{
"job": {
"id": 5,
"timelineRecordId": "855ea6d6-9ed0-442d-b921-0c4add8bb068",
"name": "Release",
"dateStarted": "2018-07-04T08:53:05.9133333Z",
"dateEnded": "2018-07-04T08:53:21.34Z",
"startTime": "2018-07-04T08:53:05.9133333Z",
"finishTime": "2018-07-04T08:53:21.34Z",
"status": "succeeded",
"rank": 1,
"issues": [],
"agentName": "DFA00"
},
"tasks": [
{
"id": 1,
"timelineRecordId": "fa3bb635-eab4-4c1b-9cc0-fdccd7ced33f",
"name": "Initialize Job",
"dateStarted": "2018-07-04T08:53:06.5833333Z",
"dateEnded": "2018-07-04T08:53:06.8033333Z",
"startTime": "2018-07-04T08:53:06.5833333Z",
"finishTime": "2018-07-04T08:53:06.8033333Z",
"status": "succeeded",
"rank": 1,
"issues": [],
"agentName": "DFA00",
"logUrl": "http://xxxx:8080/tfs/DefaultCollection/7658559e-6e61-422a-952b-a5fce0b6ca1d/_apis/Release/releases/49/environments/49/tasks/1/logs?releaseDeployPhaseId=54"
},
There should be timelinerecord, starttime, finishtime, status for a task deployment result in a single release.

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).