Using Preview Versions of .NET Core with App Center Build

App Center is a great tool for building, testing, releasing and monitoring apps, but what if we want to use a preview version of .NET Core?

Today, App Center Build doesn't support the preview versions .NET Core. This means that when I use .NET Core 3.0, my build fails with this error:

The current .NET SDK does not support targeting .NET Core 3.0

(Here's the complete error message)

##[section]Starting: Restore Nuget
==============================================================================
Task         : Command Line
Description  : Run a command line with arguments
Version      : 1.1.3
Author       : Microsoft Corporation
Help         : [More Information](https://go.microsoft.com/fwlink/?LinkID=613735)
==============================================================================
[command]/bin/bash -c /usr/bin/find . -name '*.sln' -type f -print0 | /usr/bin/xargs -0 grep -l 'MondayPundayApp.Droid.csproj' | /usr/bin/xargs -I '{}' nuget restore '{}' -DisableParallelProcessing
MSBuild auto-detection: using msbuild version '15.0' from '/Library/Frameworks/Mono.framework/Versions/6.0.0/lib/mono/msbuild/15.0/bin'.
/Users/vsts/hostedtoolcache/dotnet/sdk/2.2.300/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.TargetFrameworkInference.targets(137,5): error NETSDK1045: The current .NET SDK does not support targeting .NET Core 3.0.  Either target .NET Core 2.2 or lower, or use a version of the .NET SDK that supports .NET Core 3.0. [/Users/vsts/agent/2.155.1/work/1/s/Src/MondayPundayApp.UnitTests/MondayPundayApp.UnitTests.csproj]

##[error]/bin/bash failed with return code: 1
##[error]/bin/bash failed with error: /bin/bash failed with return code: 1

To work-around this problem, let's write a script that installs the version of .NET Core we need.

1. Write a Post-Clone Script

App Center Build allows us to write a bash script that runs after the build machine has finished cloning our repo, called a Post-Clone script:

1. In the same directory as the Android .csproj, create a new file called appcenter-post-clone.sh

Place the scripts with the format specified below next to the project-level (.xcodeproj, .csproj, .sln or package.json) file

2. (Repeat for iOS) In the same directory as the iOS .csproj, create a new file called appcenter-post-clone.sh

3. Open both appcenter-post-clone.sh files

4. In appcenter-post-clone.sh, copy the following Bash script:

wget https://dot.net/v1/dotnet-install.sh
chmod +x dotnet-install.sh
./dotnet-install.sh --version $netcoreversion --install-dir "$AGENT_TOOLSDIRECTORY/dotnet"

5. Commit the new appcenter-post-clone.sh files and push the updates to your remote git repository

2. Update App Center Build Settings

1. In App Center, navigate to your app's Build configuration

2. On the Build Branches page, select the Settings Gear next to the branch you'd like to configure

3. In the Build Configuration menu, select More Options

4. In the More Options section of the Build Configuration menu, ensure Post-Clone is listed as a build script

Note: If Post-Clone is not listed, ensure that you've saved appcenter-post-clone.sh its project-level directory and have commited the file to your remote git repository

5. In the Build Configuration menu, in the Environment Variables section, add the following key-value par:

Note: You can specify any version of .NET Core as the Value

| Name           | Value                   |
| netcoreversion | 3.0.100-preview8-013656 |

6. In the Build Configuration menu, select Save & Build