CI [Build Test] for Windows Form App (.NET Framework) using GitLab’s Windows VM

2 minute read

Introduction

Thanks! I am an engineer in the production engineering department. According to GitLab official blog 2020.01.21, Windows Shared Runners Beta is now available So I actually tried it. I think that it will be improved because it is a beta version, but first of all, I am glad that the build and test passed. The result of CI is as follows.

  • Build result
    image.png

  • test results
    image.png

environment

  • GitLab Enterprise Edition : 13.3.0-pre
  • Microsoft Visual Studio Community 2019 : 16.7.1

Prerequisites

Visual Studio has been installed and the project has been created.

GitLab project creation

Create with GitLab-> New project-> Create From template-> .NET Core template.
image.png

Add the created Visual Studio project to the repository

  • For the file structure, I pasted the Visual Studio project as it is into the template cloned from GitLab.
  • For testing, I created a Tests directory and added a project for testing below.
  • .gitignore has been replaced with the template for visual studio on gitignore.io.

image.png

Added .gitlab-ci.yml

  • Added the script part to the reference example of the official blog.
  • Tools such as .Net Framework and MS Build is pre-installed on the Windows VM, so you can run it without installation if you don’t make a mistake in the path or execution method.
  • Since the path contains spaces, it is enclosed in double quotes and prefixed with & to execute the EXE in PowerShell.
  • The test script adds, builds, and runs the tests used in the test.
  • Note: vstest.console.exe is the path of Microsoft Official I tried, but I can’t find vstest.console.exe in CommonExtensions on my Windows VM. So I changed the path to use the one from Extensions.

yml:.gitlab-ci.yml


.shared_windows_runners:
  tags:
  - shared-windows
  - windows
  - windows-1809

stages:
  - build
  - test

before_script:
 - Set-Variable -Name "time" -Value (date -Format "%H:%m")
 - echo ${time}
 - echo "started by ${GITLAB_USER_NAME}"

build:
  extends:
  - .shared_windows_runners
  stage: build
  script:
  - '& "C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Current\Bin\MSBuild.exe" UVWCalibrator.csproj'

test:
  extends:
  - .shared_windows_runners
  stage: test
  script:
  - '& "C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Current\Bin\MSBuild.exe" -t:restore'
  - '& "C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Current\Bin\MSBuild.exe" Tests\UVWCalibratorTests\UVWCalibratorTests.csproj'
  - '& "C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\Common7\IDE\Extensions\TestPlatform\vstest.console.exe" Tests\UVWCalibratorTests\bin\Debug\UVWCalibratorTests.dll'

Note: Package restoration

In a Windows Form project, the above ci does not work properly. The test uses two packages, MSTest.TestAdapter and MSTest.TestFramework, so I’m running MSBuild.exe -t: restore, but it exits without finding the package that needs to be installed. It will work well if you set according to Restore NuGet package with MSBuild for VisualStudio2017 or later project. If set correctly, it can be added to the Solution Explorer reference.

image.png

Finally

This completes the setup. Thank you for your hard work. CI / CD for Windows had many difficult parts to do, but I think that such a Windows VM will greatly contribute to future development efficiency.