

How use Visual Studio, MSBuild or Roslyn previews in GitHub or DevOps CI
source link: https://www.cazzulino.com/vs-preview-ci.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

If you want to leverage the many awesome C# 9 features, including roslyn source generators like ThisAssembly, all of which require the latest and greatest .NET 5.0 preview, it would be a pity to have to give up the safety net of your CI builds (whether GitHub Workflows or Azure DevOps pipelines) just because they don’t provide hosted images with the relevant bits.
This post shows how to install and use the latest Visual Studio preview from your build script.
Yes, it might just be enough to install the .NET Core RC and use
dotnet build
anddotnet test
. In some cases you do need a full Visual Studio depending on your project.
The key to enabling this scenario is a little awesome (if I might say so) dotnet global tool called dotnet-vs: “A global tool for running, managing and querying Visual Studio installations”. It’s a cool little thing Adrian Alonso and myself created to more easily manage multiple versions of Visual Studio installed side by side. It can get quite crazy at times.
The tool allows, among other things, to query installed VS versions and install new ones, including adding/removing components. It internally uses vswhere as well as the Visual Studio installer command line to achieve a seamless experience.
So, on to the actual scripts that are really quite simple.
GitHub Workflow
The whole build workflow (which you can see in action too) is:
name: build
on: push
jobs:
build:
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.x
- run: dotnet tool update -g dotnet-vs
- run: echo "::set-env name=MSB::$(vs where preview --prop=InstallationPath)"
- run: vs install preview --quiet +Microsoft.VisualStudio.Component.ManagedDesktop.Core +Microsoft.NetCore.Component.DevelopmentTools
if: env.MSB == ''
- run: echo "::add-path::$(vs where preview --prop=InstallationPath)\MSBuild\Current\Bin"
- run: msbuild -r
- run: msbuild -t:test
Relevant steps:
-
Install/update to latest & greatest dotnet-vs by simply using
dotnet tool update -g
. That will install the tool if it’s not there, and ensure it’s the latest otherwise. I do this because if VS preview requires some newer command args in the future, the latestdotnet-vs
tool will likely support that too. -
The syntax for setting an environment from a GH action is a bit weird, but the notable thing here is that the
run
command will actually run Powershell Core by default, unlike on DevOps where it runs cmd.exe (on Windows agents, in both cases):So we take advantage of that fact and just run the
vs where
command inline to set the value of the installation directory for a preview version of VS. Thedotnet-vs
tool where command will return the raw value from that execution, or an empty string if no such version is found. -
We use that as the condition for the
vs install
so that we only do so if the preview isn’t there already. Note how you can add any supported workload or component ID to the installation with the simple+[ID]
syntax. There are also shorter aliases for common workloads like+core +desktop +azure +mobile
, say. The ones I’m installing in this case are just the minium I need, so I can get the install in just about ~5 minutes! -
We finally use the same “trick” as step 2 for adding the MSBuild path to the
%PATH%
so that we can finally just runmsbuild
.
All in all, pretty straightforward and concise. I love it how GitHub run
actions are rendered by default using the frst line of the command. I wish Azure DevOps did the same, instead of showing just CmdLine
and forcing you to always annotate steps with displayName
.
Azure DevOps
The whole build pipeline (which you can see in action too) is:
pool:
vmImage: 'windows-2019'
steps:
- checkout: self
- task: UseDotNet@2
inputs:
packageType: sdk
version: 3.1.x
performMultiLevelLookup: true
- script: dotnet tool update -g dotnet-vs
- pwsh: echo "##vso[task.setvariable variable=MSB]$(vs where preview --prop=InstallationPath)"
- script: vs install preview --quiet +Microsoft.VisualStudio.Component.ManagedDesktop.Core +Microsoft.NetCore.Component.DevelopmentTools
condition: eq(variables['MSB'], '')
- pwsh: echo "##vso[task.prependpath]$(vs where preview --prop=InstallationPath)\MSBuild\Current\Bin"
- script: msbuild -r
- script: msbuild -t:test
(I removed all the displayName
for conciseness).
You can see that the structure is pretty much the same as for GitHub workflows. Note that we need to explicitly choose to run with powershell by using pwsh
instead of script
, so that the inline execution of vs
commands when expanding the string for the variables works the same way. We use the ##vso[task.XXX]
syntax in this case instead.
The condition syntax in GitHub workflows is also so much nicer :).
And that is all you need to install quickly (both in ~5’ in this combination of components) and build in CI using the latest and greatest C# features!
Recommend
-
55
Windows平台的C++开发人员应该会注意到Visual Studio 2017 15.9 Preview 2提供的一项 新功能 ,这项功能让调试变得...
-
17
Update an existing application for MSBuild 15 11/04/2016 3 minutes to read In this article In versions of MSBuild prior to 15.0, MSBuild was loaded fro...
-
9
Developing ASP.NET IIS Applications: Triggering LiveReload on a successful Visual Studio or MSBuild build I’m all about getting rid of the friction in development. All that ‘ceremony’ that ends up built into your muscle memory to...
-
9
0:00 / 38:56 ...
-
9
MSBuild and 64-bit Visual Studio 2022 Rainer November 11th, 2021 Visual Studio’s
-
3
本文来告诉大家 msbuild Roslyn 的行为,本文非新手友好 项目文件中的已知属性(知道了这些,就不会随便在 csproj 中写死常量啦) - walterlv
-
8
Microsoft previews Edge Developer Tools for Visual Studio The extension preview allows developers to preview ASP.NET and ASP.NET Core web projects, debug and edit HT...
-
6
基于 Roslyn 同时为 Visual Studio 插件和 NuGet 包开发 .NET/C# 源代码分析器 Analyzer 和修改器 CodeFixProvider 吕毅 发表于 2019-07-06 更新于 1 小时前 Roslyn 是 .NET 平台下十分强大的编译器,其提供...
-
2
吕毅 发表于 2018-07-04 更新于 6 小时前 MSBuild 不愧是强大的编译器,它提供的扩展机制让你几乎可以编译任何类型的文件或项目;Roslyn 是全新编写的一套编译器,不过它保留了 MSBuild 的大部分机制;NuGet 是 .NET...
-
5
Visual Studio Code 1.77 previews GitHub Copilot chat Preview of deeper Copilot integration allows VS Code users to view Copilot suggestions inline and as...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK