X
18Mar

Code Analysis & Code Metrics Integration in Azure Pipelines

This documentation shows the tutorial on how to integrate Visual Studio Code Analysis and Code Metrics in Azure Pipelines using YAML. Follow the steps:

  1. Open Visual Studio 2019 and open project or solution and select your project.1b
  2. Now you have to add a NuGet package named “Microsoft.CodeAnalysis.Metrics” to your solution. For this right click on the solution in Solution Explorer and click on Manage NuGet Packages for Solution.

2b

3. Browse for Microsoft.CodeAnalysis.Metrics in the search bar. Click on the package. Select all your projects in the solution and click on Install.

3b4. In the next 2 windows click on OK then Accept. Wait until the package gets added to all your projects.

5. Now save all your changes. Then in team explorer commit your changes with a commit message.

4b

6.  Sync the changes with the server in Azure Repos.

5b

 

7. Navigate to your project in Azure DevOps and go to Build section of Pipelines.

8. Click on New Pipeline to setup a pipeline in YAML. If you have a n existing YAML pipeline you can add the stage to it.

9. Click on Azure Repos Git and then select your repository.

6b

10. Click on Starter Pipeline for a new pipeline.

7b

 

11. Add the following stage to your existing pipeline for Code Analysis and Code Metrics .

 - stage: CodeScan
   dependsOn: []
   jobs:
     - job: CodeScan
       pool:
        vmImage: 'vs2017-win2016'
       steps:
       - task: NuGetCommand@2
	   displayName: NuGetRestore
         inputs:
           restoreSolution: '$(solution)'
       - task: VSBuild@1
         displayName: CodeAnalysis
         inputs:
           solution: '$(solution)'
           msbuildArgs: '/p:RunCodeAnalysis=true'
           platform: '$(buildPlatform)'
           configuration: '$(buildConfiguration)'
       - task: VSBuild@1
         displayName: CodeMetrics
         inputs:
           solution: '$(solution)'
           msbuildArgs: '/t:Metrics /p:MetricsOutputFile=$(Build.ArtifactStagingDirectory)/PartsUnlimitted.Metrics.xml' # Modify xml file name according to project
           platform: '$(buildPlatform)'
           configuration: '$(buildConfiguration)'
       - task: PublishBuildArtifacts@1
         displayName: 'Publish Metrics'
         inputs:
           PathtoPublish: '$(Build.ArtifactStagingDirectory)'
           ArtifactName: 'MetricsReport'
           publishLocation: 'Container'

Note: Add solution, buildPlatform and buildConfiguration in the variables section of the pipeline.

variables:
  solution: '**/*.sln' # Provide path of .sln file here
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

12. Add other stages and dependencies for build, deploy or any other functionality according to the needs of the project. If you only want the pipeline for Code Metrics and Code Analysis, you can go for the single stage complete script provided below. Remove everything in your Starter Pipeline and add the following script. 

trigger:
  - master
  
  variables:
    solution: '**/*.sln' # Provide path of .sln file here
    buildPlatform: 'Any CPU'
    buildConfiguration: 'Release'
  
  pool:
    vmImage: 'vs2017-win2016'
  
  steps:
  - task: NuGetCommand@2
    displayName: NuGetRestore
    inputs:
      restoreSolution: '$(solution)'
  - task: VSBuild@1
    displayName: CodeAnalysis
    inputs:
      solution: '$(solution)'
      msbuildArgs: '/p:RunCodeAnalysis=true'
      platform: '$(buildPlatform)'
      configuration: '$(buildConfiguration)'
  - task: VSBuild@1
    displayName: CodeMetrics
    inputs:
      solution: '$(solution)'
      msbuildArgs: '/t:Metrics /p:MetricsOutputFile=$(Build.ArtifactStagingDirectory)/PartsUnlimitted.Metrics.xml'
      platform: '$(buildPlatform)'
      configuration: '$(buildConfiguration)'
  - task: PublishBuildArtifacts@1
    displayName: 'Publish Metrics'
    inputs:
      PathtoPublish: '$(Build.ArtifactStagingDirectory)'
      ArtifactName: 'MetricsReport'
      publishLocation: 'Container'

13. Click on Save and run to run the pipeline.

save&run

14. Enter a commit message and click Save and run.

15. View the live logs of the pipeline to see Code Analysis. You can also download the logs if required.

codeAnalysis

16. The Code Metrics report will be published as an XML file inside the Artifact generated after the pipeline. For this navigate to the Runs tab of your pipeline, click on any Run you want.

CodeScan

17. You can find the report by clicking Artifacts button.

Artifacts

18. Download the XML file to view the Code Metrics report.

code

19. Congrats!!!!! You are Done.

 

Related

How to Prevent Users from Moving Task to Closed State with a non-Zero value Remaining Work field in VSTS/TFS

In this Blog I am going to explain how we can customize Task in VSTS/TFS to Prevent Users from Movin...

Read More >

ASP.NET: Audit Trail Implementation using Entity Framework

When you are working with certain projects which involves the Customer records, you might need to tr...

Read More >

Building an Outlook 2010 Add In Using C#.NET With Visual Studio 2010

How to build an outlook 2010 Add In using C#.Net and Visual Studio 2010?In this post, I’l...

Read More >

VSTS on Mobile

Thank you for the overwhelming response for our first release of VSTS on mobile app. More than thous...

Read More >

Microsoft Azure : Running Windows Virtual Machine

How easy it is to create an Azure VM in just a few minutes. We'll use Windows Server 2012 r2 Dat...

Read More >

Windows 8 Shortcut Keys -Useful

HotkeysPressing the Windows key and some specific keys can make you life easier. Here are some usefu...

Read More >

How to handle the exceptions using recovery scenario manager in QTP?

VB script Error HandlingDurning run time, if QTP encounters any erorr then it will display the error...

Read More >

Microsoft Azure : Running Linux Virtual Machine

Creating an Azure VM that runs Linux is easy to do. We will use ssh public key file to secure our&nb...

Read More >

Using Bootstrap Validation in Asp.net Applications

Bootstrap is a powerful mobile first front-end framework for faster and easier web development. It u...

Read More >

Implementing Google account authentication in ASP.NET MVC

Implementation of Google OAuth in MVC application: Step 1: Create a Google OAuth application &n...

Read More >

Share

Post a Comment

  • Recent
  • Popular
  • Tag
  • The Ansible Architecture

    Ansible is an open source, IT automation tool. It can configure systems, deploy software, and orchestrate more advanced IT tasks such as continuous deployments or zero downtime rolling updates. In thi...
  • Subversion (Centralized Version Control System)

    Version control system is a repository of files, often the files for the source code of computer programs with monitored access. They are essential for any form of distributed, collaborative developme...
  • Volume management in Kubernetes

    Since pods created in k8s are ephemeral, we are able to get the data as long as pods are alive, but if pods are terminated data stored in it completely lost and it cannot be get back, for that we need...
  • LDAP Integration with Sonatype Nexus

    Sonatype Nexus supports the Repository Manager integration with various active directory systems for authentication, such as Microsoft Exchange / Active Directory, OpenLDAP, ApacheDS, and others. In o...
Monthly Archive
Subscribe
Name

Contact Us
  • *
  • *