Setting up Jenkins is always hard, especially for UWP Project which is still lacking in term of toolings. In this article, I will share my (in)experience on integrating Jenkins within a UWP Application Development Project.
I am using a demo application project to demonstrate the setup. You can get it from here. The project (or rather the solution) contains four projects:
- AnagramCheckerApp, is a UWP application project. I use Template 10 as the starting point. The App it self is a simple application to check whether two words are anagram.
- AnagramCheckerAppTest, is an Application Unit Test project. It contains testing for AnagramCheckerApp’s MainPageViewModel in MainPageViewModelTest class.
- Library, is a portable class library project containing the business logic (AnagramChecker class) used by AnagramCheckerApp.
- LibraryTest, is a normal MSTest project to test the Library project, specifically testing implemenation of AnagramChecker in AnagramCheckerTest class.
The build actions included in Jenkins job are: building the AnagramCheckerApp project, running both AnagramcheckerAppTest and LibraryTest, and then publishing the result to a Slack channel. To achieve these following steps are done:
Install & Prepare Jenkins
Download Jenkins from here. Complete the setup but don’t start Jenkins directly. In Windows machine, by default Jenkins will be started as a Windows Service, we don’t want that because it doesn’t have the capability to launch UI application (or any process that has interactive mode) which is required when running test in AnagramCheckerAppTest.
I stopped and disabled the Jenkins’s Service from Windows’s “Services” manager application.
And then launch Jenkins from the command line.
java -jar "C:\Program Files (x86)\Jenkins\jenkins.war"
Access Jenkins from localhost and complete the initial setup.
Install & Configure Required Plugins
There are 5 plugins used in this setup:
This plugin is used to integrate Jenkins with MSBuild for building the project/solution.
This plugin is used to integrate Jenkins with VSTest runner which is used to run tests in both AnagramCheckerAppTest project and LibraryTest. You might need to use other test runner plugin for other testing frameworks such as xUnit or NUnit.
This plugin is used to convert the test result generated from VSTest Runner (usually it’s a *.trx file) into JUnit XML reports so it can be read/published by Jenkins.
This plugin allows posting build notifications to a Slack channel which is useful to keep team member up to date with the status of repository.
Install and configure all of these plugins. Additionally, I use Nuget command line application for restoring the project’s dependency. Instead of using Jenkin’s Nuget Plugin (which I haven’t configured successfully), I call the Nuget executable directly as a batch command in one of the build action.
Create Build Job & Setup Build Actions
Create a new build job using Freestyle Project template. The build job contains following main build actions:
- Fetch code from the repository.
- Restore project’s dependencies.
- Create AnagramCheckerApp’s App Package.
- Run test from AnagramCheckerAppTest & LibraryTest project.
- Publish Test Result & Send Notification to a Slack channel.
Let’s dive in!
1. Fetch code from repository
Specify the source control management that you use, configure the URL of your repository and credential to access it. If necessary, you can also set which branch to be used.
2. Restore Project’s Dependencies
NuGet is used to restore project’s dependency. In my case, there are 2 build actions for this. The first action is to set the proxy used by NuGet (since I sit behind company’s firewall)
[folder containing nuget.exe]/nuget.exe config -set http_proxy=[proxy]
and second action is the dependency restore process. Note that you can use %WORKSPACE% Jenkins’s environment variable to location your solution file.
[folder containing nuget.exe]/nuget.exe restore [path to solution file]
3. Create AnagramCheckerApp’s App Package
This build action is to ensure that we could create a UWP Application package (*.appx/*appxbundle) from the source code in the repository. The build action comes from MSBuild plugin and you need to configure the App’s project location and the build arguments.
4. Run test from AnagramCheckerAppTest & LibraryTest project.
There are several build action to achieve this step. First we have to build both AnagramCheckerApp and LibraryTest project. For AnagramCheckerAppTest project, the build process will produce an App package which contains the Application Unit tests, on the other hand, LibraryTest project produces a *.dll file containing the test.
Running test from AnagramCheckerAppTest is slightly more complicated. You’ll have to install the code signing certificate of the test app into the local machine’s TrustedPeople certificate storage. This is due to the fact that AnagramCheckerAppTest itself is a UWP Apps and running the test inside it requires the application to be installed. The certificate file is located in the same folder as the app package. In my case it is located in “%WORKSPACE%\ApplicationTest\AppPackages\AnagramCheckerAppTest_184.108.40.206_x86_Test\AnagramCheckerAppTest_220.127.116.11_x86.cer”
I use certmgr tool to install the certificate. Note that the certmgr tool must be run as Administrator since we are trying to install the certificate on the local machine.
"C:\Program Files (x86)\Windows Kits\10\bin\x64\certmgr.exe" /add "%WORKSPACE%\ApplicationTest\AppPackages\AnagramCheckerAppTest_18.104.22.168_x86_Test\AnagramCheckerAppTest_22.214.171.124_x86.cer" /s /r localMachine trustedPeople
Once done, we are ready to run both tests from AnagramCheckerAppTest and LibraryTest (we don’t need to configure extra stuff for LibraryTest project). The build action for running the test is available from VSTest Runner plugin. Create a separate build action for each test and specify the “Test Files” target accordingly. For AnagramCheckerAppTest, the test file is the application package located in “ApplicationTest\AppPackages\AnagramCheckerAppTest_126.96.36.199_x86_Test\AnagramCheckerAppTest_188.8.131.52_x86.appx” and for LibraryTest the test file is “LibraryTest\bin\Release\LibraryTest.dll” (name and path may differ based on their corresponding build parameter you specified in previous build action).
5. Publish Test Result & Send Notification to a Slack channel.
The last step is to publish the test result using MSTest plugin. This plugin will convert the *.trx file produced by test runner into *.xml file that is compatible with Jenkins. The only thing that you need to configure is the location of *.trx file.
Additionally, I use Slack to receive notification of the build result. See this for the integration guide.
Everything is ready now and you could try the configuration by triggering it manually to see the result. Once completed, the project’s dashboard will display build & test result.
And you’ll get a concise information in Slack.
Feel free to try and add suggestion!