top of page

CI/CD & Test Automation for Dynamics 365 in Azure DevOps/VSTS -Part 4 – Gated Check-in

In my previous blog, I wrote about how to set up a VSTS Release definition. In this blog, I am goint to explain the gated-check-in but before heading to it. We must know why we need gated check-in

Gated check-in helps to restrict developers from checking in a broken code into a source control system and thus helps to avoid blocking your team. With gated check-in when check-in is initiated by a developer, it will build the project and will check-in the code only if the build is successful. Gated check-in is suitable for projects whose overall build time is less than a few minutes.

C# we have used StyleCop and FxCop and for JavaScript and Jquery we have used JSHint

StyleCop for the custom code such as Plugins, Workflows, Actions, and WebApi.

Consider there is a group of developers working together and each one writes the code in the exact same way.

More often than not, one isn’t better than the other and it’s just a matter of taste. In a team or in a single project, it’s more important to be consistent than it is to choose the right style.

Agreeing on a style can be hard enough, but enforcing it shouldn’t be something you do manually. It will be tedious and error-prone.

StyleCop is a tool that can automate this. Let’s have a look at how to set it up.

What is StyleCop?

StyleCop analyzes C# source code to enforce a set of style and consistency rules.

StyleCop used to be a Visual Studio plugin and a NuGet package. You can still use this in Visual Studio 2019, 2017 etc.

Installing StyleCop

To add StyleCop to your project, right-click your project in Visual Studio’s Solution Explorer, and choose “Manage NuGet Packages?”:

https://i0.wp.com/blog.submain.com/wp-content/uploads/2018/04/ManageNugetPackages.png?w=328&ssl=1

Search for “StyleCop.Analyzers” and install the latest stable version:

Installing-NuGet-package

Once it is installed, you build the project solution. You might get the below StyleCop warnings.

  1. Add XML comments

  2. Generate an XML documentation file (this can be set in the project properties)

  3. Add a file header (e.g., copyright information)

  4. Put the “using” statements inside the “namespace” block

  5. Put braces on a new line

  6. Add an empty line between the two method definitions (Output2 and Output3)

Setup

The first step in integrating StyleCop into an MSBuild system is to obtain the default StyleCop MSBuild targets file. To do so, run the StyleCop installer, and select the MSBuild files option on the Custom Setup page. This will install the StyleCop MSBuild files into the {Program Files}\MSBuild\StyleCop folder.

Adding the Import Tag

Once the StyleCop MSBuild files are installed, the next step is to import the StyleCop targets file into your C# projects. This is done by adding an Import tag to each C# project file.

For example, to integrate StyleCop to the project SampleProject, open the project file SampleProject.csproj within your favorite text editor. Scroll down to the bottom of the file and add a new tag to import the StyleCop.targets file. This import tag should be added just below the import of Microsoft.CSharp.targets:

Save the modified .csproj file. The next time you build this project either within Visual Studio or on the command line, StyleCop will run automatically against all of the C# source files within the project.

Build Warnings Vs Errors

By default, StyleCop violations will show up as build warnings. To turn StyleCop violations into build errors, the flag StyleCopTreatErrorsAsWarnings must be set to false. This flag can be set as an environment variable on the machine, or within the build environment command window. Setting the flag this way will cause StyleCop violations to appear as build errors automatically for all projects where StyleCop build integration is enabled.

Alternately, this flag can be set within the project file for a particular project. Open the .csproj file for your project again, and find the first PropertyGroup section within the file. Add a new tag to set the StyleCopTreatErrorsAsWarnings flag to false. For example:

The configuration described above will suffice to enable StyleCop build integration on an individual development machine. However, development teams working within a well-defined development environment can set up the build integration in a more global way, so that each developer does not have to manually install StyleCop on his machine.

To do this, copy all of the files from {Program Files}\MSBuild\StyleCop into a custom folder within your build environment, and check all of these files into your source control system. Next, define an environment variable within your development environment which points to the location of the StyleCop targets file. For example:

set StyleCopTargets=%enlistmentroot%\ExternalTools\StyleCop\v4.4\StyleCop.targets

With this configuration in place, it is simply a matter of adding the following import tag to each .csproj file within your development environment:

StyleCop will automatically run each time this project is built, no matter which developer is building the project. There is no need for each developer to install StyleCop manually, since the StyleCop binaries are checked directly into your source control system and are centrally integrated into your build environment.

What is CodeAnalysis?

To integrate Code Analysis in build, unload and edit project and add following tags. Note that paths might be different depending on Solution configuration.

<ItemGroup>
    <CodeAnalysisDictionary Include="$(SolutionDir)\CodeAnalysisDictionary.xml" />
</ItemGroup>
<Import Project="$(SolutionDir)\ExternalDlls\StyleCop 4.7\StyleCop.targets" /> 
 

Code Analysis configuration

Configure project Debug configuration to use Code Analysis rules in Solution root. Do the same for the Release configuration. Only difference is that in debug mode Code Analysis should not be run because it slows down the build. We keep CA running in Release to get error report from continues integration and to allow easily turning CA on by altering solution mode from Debug to Release.

Once the above step is done, please commit the project solution files in Azure DevOps/VSTS repository.

How to enable Gated Check-in VSTS build definition.

Go to Build definition -> Triggers-> you can see the gated check-in as follows:

Check the gated check-in checkbox. Now gated check-in is enabled for this particular build definition.

In my next blog, we will see how to move the master data from source to target instance using our CI/CD pipeline.

If you are interested in this topic and would like to do some further self-study I encourage you to check out my blog on this.

Comentarios


1688905823827.jpg

Hi, I'm Dharani

I am a Principal Consultant at Capgemini Australia, specializing in Microsoft Business Applications. With a passion for knowledge sharing and community engagement, I hold the esteemed title of MVP in Business Applications and am a Microsoft Learn Expert.

  • Facebook
  • Twitter
  • LinkedIn
  • Instagram

Subscribe

Thanks for submitting!

bottom of page