Example creating a VSTS build extension using c# - azure-devops

I'd like to put together some VSTS build / release management extensions in C#, although I can find some generic examples using PowerShell it would be handy to have some examples in C#.
Could anyone point me at a C# VSTS extension example please?

Regarding build task extension, you can specify the C# application, such as console application:
"execution": {
//"PowerShell3": {
// "target": "Hello.ps1",
// "argumentFormat": ""
//}
"Process": {
"target": "..\\ConsoleApplication1.exe",
"argumentFormat": "$(ConnectedServiceName) $(currentDirectory) $(ApiPortalName)"
}
}
However the Task SDK are Typescript and PowerShell, so you can’t use the SDK in your application directly, also the newest schema has removed the extra schema info (check remove extra schema info #308), so NodeJS and PowerShell are the recommended way.

Look at the source of GitVersion. The authors have wrote a TFS/VSTS task associated with it. The core of the tool is pure C#.
Here's the code of their TFS Task
https://github.com/GitTools/GitVersion/tree/master/src/GitVersionTfsTask
Here is the task you'd use inside your builds
https://marketplace.visualstudio.com/items?itemName=gittools.gitversion#overview

Related

Set Assembly Info in VS2008 VCPROJ over BuildPipline

I need to integrate an older VS2008 project for WINCE6, without .NET, into an AzurePipline. This works very well with msbuild. Now I am looking for a way to set the version number in the version info.
Ready-made extensions did not work with the old project.
Probably the best way is to use a property of msbuild. I just don't know how to process the version number in the project.
Has anyone ever done something like this? Thanks for your suggestions.
In your build pipeline on Azure DevOps, you can add an Assembly Info task to set assembly information of your application.
Here I find some Assembly Info task extensions from the Marketplace. You can install one suitable extension from them to your organization and then call the related task in your build pipeline.
Below is a similar topic as reference:
https://stackoverflow.com/a/8452320/14697693

Is there a way to associate Automated tests to Test Cases in Azure DevOps using VS Code?

I've been looking for an extension for VS Code that will allow the "Associate Test Case" functionality provided with Visual Studio IDE and I have had no luck.
Is there an extension that provides this functionality?
Visual Studio steps:
Open Test Explorer
Right click on a Test Case
Click on Associate To Test Case
Screenshot:
As far as I know, currently there is no such extension provided in the visual studio code to support the "Associate Test Case" function.
You could add your request for this feature on vscode UserVoice site The product team would provide the updates if they view it.
Check this extension that I have created https://github.com/JanuszNowak/janono.ado.testcase.associate.cli
it allows associating in automatic manner.
For now this is CLI, that you can run as vs-code task. Later I will create also dedicated Azure DevOps task. Visual Studio is not needed to run automatic association.
Code sample:
namespace ExampleTestProject
{
[TestClass]
[janono.ado.testcase.associate.Organization("janono-pub")]
public class UnitTest1
{
[TestMethod]
[janono.ado.testcase.associate.TestCase(5)] //<---
public void TestMethod1()
{
//yours test method content
//...
//
}
}
}

.NET Core App - How to get build number at runtime

I've got a .NET Core MVC app which is built using TFS online, and published to Azure using the Release management stuff in TFS online. All very nice.
What I'd like to do is have my app display the build number somewhere. Doesn't matter where...but for example in a WebAPI endpoint like /api/buildversion.
Can anyone help with how I can get the TFS build number at runtime? When the app is packaged/published is there any file which contains the build number that I can access from the application?
The simple way is that you can store the build number in a file (e.g. appsettings.json), then get this data in app code.
Appsettings.json sample code:
{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-WebApplication1-ab933d83-8f4b-4024-9f3c-1aef5339a8f3;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
"CodeVersion": {
"Num": "#{MyBuildNumber}#"
}
}
Install Replace Tokens extension
Edit your build definition
Click Variables tab and add a variable. (Name: MyBuildNumber, Value:$(Build.BuildNumber))
Add Replace Tokens build step before build step
There is an easier way than adding more plugins and libraries. If you are using the web/app deploy method, expand the File Transforms & Variables
Assuming you want to update say a build number value in your appsettings.json
Then just update the variables setting in your release definition of VSTS
Super easy. This is more or less the same steps in Octopus IIRC.
You need to save the Build Number in your application at compile time.
You can access the build number with $Build.BuildNumber or $Env:BUILD_BUILDNUMBER depending on your environment. I usually write it as a variable into my ApplicationInfo.* with both the version and build number.

vNext TFS 2015 QueryBuildDefinition function in powershell not finding my build definition

Trying to call the following within a PowerShell script which is called from a new vNext Build step:
$buildDef = $buildServer.QueryBuildDefinitions($project)
Would have thought that this would return all the build definitions in my project. It only returns my XAML definitions, not my new vNext Build Definitions.
Do I need to use a different function to do this?
Want to get the list of my changesets in the last build since the last good build.
Previously I would have done something like the following:
$workspace = $buildDef.Workspace.Mappings[0].ServerItem
and then passed this $workspace into the QueryHistory function.
The XAML build system (and basically everything else in the "old" TFS object model) uses a SOAP API. The SOAP API is slowly being replaced with a REST API, at least for newer things.
Thus, the task-based build system does not have a SOAP API. It has a REST API. You can access it from C# code either by querying the REST API directly or by using the Team Foundation Server Client NuGet package.

Get Build Version in automated build deployment using TFS

I am deploying web application to azure using TFS CI automated build deployment.
In our config maintain build version like 2014.05.19.1 which is $(Date).$(rev) format.
All I want to update config each time build is deployed.For that I am passing value to 'BuildVersion' parameter in template to powershell script which actually performs publishing to azure.
I tried using $(Date:yyyyMMdd)$(Rev:.r) but it is considered string as it is.
I want to get current build version just like IBuildDetail.BuildNumber
within template.
My question is how to get the build version?
If you are using Invoke Process, instead of passing value for BuildVersion parameter you can directly use 'BuildDetail.BuildNumber' in parameters for process like
String.Format("-BuildNumber ""{0}""",BuildDetail.BuildNumber)
This would give the required build number.
If your PowerShell script is being executed from your TFS build, it should have access to the environment variables specific to the TFS context of the build. If that is the case, you actually don't need to pass the $(BuildVersion) parameter to the script, as it already is accessible to the PS script in the $env:TF_BUILD_BUILDNUMBER environment variable. Try testing something like $env:TF_BUILD_BUILDNUMBER | Out-File "D:\Dev\BuildNumber.txt" in your script. You should hopefully see the file containing your build number after running your build.
(I am assuming you are using a relatively new build process template...one that contains the "Post-Build script path" parameter, such as TfvcTemplate.12.xaml)
Hope this is helpful.
I would recommend that you use the right tool for the right job. The build system, is really only for building (compile & test). We have been using it for other things for years coz we did not have another integrated solution. However Microsoft recently bought InRelease and rebranded as Release Management for Visual Studio 2013. I have successfully integrated this with TFS 2012 as well.