CI / CD Pipeline – Part 5

Photo by NASA on Unsplash

This will be my last article in this series dedicated to building a complete CI / CD pipeline for automatically delivering a NUGET package whenever a change is pushed to GITHUB.

What we have at this point is actually pretty impressive, given how little work it took to achieve. For that we can all thank the folks who brought us GITHUB, APPVEYOR, and Visual Studio Community Edition, and made those tools available to open source software developers, for free.

This time around, I intend to make a change to out CI / CD pipeline that will automatically collection and publish code coverage statistics. Those stats will be available to anyone who wants to see then through a website called COVERALLS.IO. With them, you, as the author of a NUGET package, will have a valuable tool for ensuring your code will do what you think it’s supposed to. For anyone wanting to use your package, those stats might give them the confidence necessary to pull your package into their project. In any case, adding code coverage stats to the build isn’t terribly difficult and this is how I do it for my projects.

The first step is to add a couple of additional NUGET packages to our CG.Walkthrough.Tests project. So, open up the solution in Visual Studio, right click on the CG.Walkthrough.Tests project itself (not either one of the two projects), then choose the “Manage Nuget Packages” menu choice.

managepackages

Then, once the dialog show up, go ahead and add two NUGET packages to the CG.Walkthrough.Tests project: coveralls.io, and OpenCover.

Coveralls.io is a package that sends code coverage statistics to the coveralls.io website. OpenCover is a package that generates those code coverage statistics as part of an automated build – like ours!

Before we move ahead, go back and make sure that the two additional NUGET packages were added to the CG.Walkthrough.Tests project, and NOT the CG.Walkthrough project. That’s important because we need those two packages to interact with our unit test project and they won’t be able to do that if they’re associated with the wrong project.

Our next step is to go to a website called coveralls.io, which is where we will be publishing our code coverage statistics. Open your browser of choice and go their now (http://www.coveralls.io). You’ll need an account for this next step so either login, if you already have an account, or open an account now.

Once you’re in the coveralls site, click the menu then choose the “Add Repos” option.

coverallsmenu

That will take you to a page for adding repositories to your account. Choose the CG.Walkthrough option, or, if it’s not there, hit the “Sync Repos” button and then choose the CG.Walkthrough option.

Capture

After you choose your new repo (click the on/off button so it turns green and says on). Click the “details” button and it will take you to a page that shows various good-to-know things about your new repo. Once of things is a “repo_token” key. Just like we did with the APPVEYOR key, we’ll want to highlight this key and copy the text to our clipboard. Be sure you ONLY select the actual key itself, and not the “repo_token:” part.

Once you have that key on your clipboard, log into your APPVEYOR account, using another browser window (you’ll still the COVERALLS window, later on). Once you’re in the APPVEYOR site, click on the top right hand part of the window, where your name is, then choose the “Encrypt Data” option. It should then take you to a page into which you can paste the key that’s on your clipboard.

appveyor-encrypt

Once you paste the text, hit the big blue “Encrypt” button and you’ll see your encrypted key in the fields below. This is the text that’s safe now to paste into the appveyor.yaml file, in our Visual Studio project. Before we go there though, let’s select the encrypted key and copy it to our clipboard now.

So, let’s go to Visual Studio and edit the appveyor.yaml file, in the “Solution Items” folder. We’ll need to edit that yaml script so that it looks like this:

version: ‘{build}’

image: Visual Studio 2017

branches:

only:

– master

environment:

COVERALLS_REPO_TOKEN:

secure: Noug/t4+Hds0iMyaWp5mEySqYB3YrZBvEsg09UTFL0NmtXCf1DURAW/+Ery0u3Lk

configuration: Release

before_build:

– cmd: before_build.cmd

build:

project: CG.Walkthrough.sln

verbosity: minimal

artifacts:

– path: .\CG.Walkthrough\bin\$(Configuration)\*.nupkg

name: nuget-packages

deploy:

– provider: NuGet

api_key:

secure: UNA9vyx7AGLzFtfStVv/Kk6FWGRp5esSHkH7FY044JAlmlRui68rZgvdBVHVPd9K

artifact: nuget-packages

We added the bit about the COVERALLS_REPO_TOKEN, and the secure key under that. Where you see the big, ugly, encrypted key, replace that with your key, from the clipboard.

Next, we’ll need to add another small change to this file by adding a section called: “after_test”, with a cmd under that. The final yaml file should look like this:

version: ‘{build}’

image: Visual Studio 2017

branches:

only:

– master

environment:

COVERALLS_REPO_TOKEN:

secure: Noug/t4+Hds0iMyaWp5mEySqYB3YrZBvEsg09UTFL0NmtXCf1DURAW/+Ery0u3Lk

configuration: Release

before_build:

– cmd: before_build.cmd

build:

project: CG.Walkthrough.sln

verbosity: minimal

after_test:

– cmd: after_test.cmd

artifacts:

– path: .\CG.Walkthrough\bin\$(Configuration)\*.nupkg

name: nuget-packages

deploy:

– provider: NuGet

api_key:

secure: UNA9vyx7AGLzFtfStVv/Kk6FWGRp5esSHkH7FY044JAlmlRui68rZgvdBVHVPd9K

artifact: nuget-packages

Now that we’ve modified our yaml file, we’ll need to add another cmd file to our soluction. To do that, select the “Solution Items” folder, in Visual Studio, then hit the Ctrl+Shift+A key combination, select the “Text File” type in the dialog, name the file after_test.cmd, then hit the “OK” button.

In our new file, enter the following text:

%userprofile%\.nuget\packages\OpenCover\4.6.519\tools\OpenCover.Console.exe -register:user -filter:”+[*]*” -target:”C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\MSTest.exe” -targetargs:”/testcontainer:.\CG.Walkthrough.Tests\bin\%configuration%\CG.Walkthrough.Tests.dll” -output:coverage.xml

%userprofile%\.nuget\packages\coveralls.io\1.4.2\tools\coveralls.net.exe –opencover coverage.xml -r %COVERALLS_REPO_TOKEN%

The first line executes OpenCover.Console.exe, which is part of the OpenCover NUGET package that we added to our solution earlier. The result of that line is the creation of an XML file containing the code coverage for our project.

The second line executes coveralls.net.exe, which is part of the coveralls.io package that we added to our solution earlier. The result of this line is that we’ll add our code coverage to COVERALLS using the encrypted token that we got from the COVERALLS site, encrypted in the APPVEYOR site, and added to our appveyor.yaml file.

The end result of all of this is that we’ll now collect and publish code coverage stats to COVERALLS, every time we build our project. To test that, let’s check in all our changes and go watch the build happen.

You should see a line in your build now that looks like this:

statisfrombuild

That demonstrates that OpenCover and coveralls are running now, as part of your build process.

So, let’s go look at the code coverage for our project on COVERALLS. To do that, go find the browser window for that website (remember I said you’d need that?). You might need to refresh the page, but after you do, you should see loads of statistics about your last build. It should look something like this:

coveralls

Final Thoughts

This article demonstrated how to quickly and easily add code coverage statistics to your CI /CD pipeline process. There are probably tons of other things that can also be added but this is what I know how to do, since it’s what I do for all of my NUGET projects.

I hope everyone enjoyed these articles. I hope they generated a few ideas for your next project.

Thanks for reading!