Shaun Hevey
Fixing .NET SDK error when using GitHub actions
December 12, 2021
By Shaun Hevey

I have just started exploring GitHub actions, and in this post, I will talk about an error I came across trying to build a .NET application and how I fixed it.

I am not going to detail what GitHub actions are, but at a high level, they are a feature of GitHub that allows you to run other applications or scripts over a GitHub repo. These actions can be triggered manually by a user, or they can set up to start when a specific event occurs, an example being when you commit code and push it up to GitHub on a specific branch of your repo.

If you would like to deep dive into GitHub Actions, you can read all about them over at the GitHub docs website.

The error

The error that I was getting when running any dotnet command was the following.

	1>/usr/share/dotnet/sdk/5.0.403/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.TargetFrameworkInference.targets(141,5): error NETSDK1045: The current .NET SDK does not support targeting .NET 6.0.  Either target .NET 5.0 or lower, or use a version of the .NET SDK that supports .NET 6.0.

As you can see, the error is that the runner that my action was being executed on did not contain the correct version of .NET. The application I was trying to build was targeting .NET 6, and this is not currently installed by default on the hosted runners provided out of the box by GitHub.

It turns out that GitHub has documented what is installed on the hosted runners that they provide, and you can find them here and as an example, the list of .NET SDKs that come preinstalled on the Ubuntu 20.04 runner here and at the time of writing it only goes up to .NET 5.0.403.

How to fix this error?

Reading through the documentation, GitHub actually recommend the use of actions to interact with preinstalled software so that you can make sure the version you are expecting to be on the runner is available for you to use and that it won't change when GitHub update the runner images.

With that in mind, there is an action that you can add to your action YAML definition that will install and select the version of .NET that will be used with the dotnet command. You can add it to your own GitHub actions by using the following snippet. This snippet will need to be added to your action definition before using the dotnet command to set up and select the correct version.

 - name: Setup dotnet
        uses: actions/setup-dotnet@v1
          dotnet-version: '6.0'

As you can see from the snippet above that it will make sure the .NET 6 SDK is available on the runner. This action can do a lot more than install a version of .NET, so be sure to check out its GitHub repo for more information.