Using .NET 7 With Code Build

Tom Moore's photo
Tom Moore
·Oct 29, 2022·

4 min read

Subscribe to my newsletter and never miss my upcoming articles

Introduction

AWS CodeBuild is one of AWS’s core development services for creating CI/CD Pipelines. CodeBuild is a fully managed service that helps developers to create automated builds of their development projects. With CodeBuild there is no development infrastructure to set up or maintain, and you only pay for the service when you are actually running builds.

CodeBuild provides a default build image contains the current Long Term Support (LTS) version of the .NET SDK. As of this blog post the currently packaged runtime is .NET 6.

Customizing the Code Build Environment

The CodeBuild environment is customizeable in one of two ways.

The first way that you can use to customize your environment is to use CodeBuild’s BuildSpec.yml file. This method allows you to run commands that alter the environment prior to the execution of your actual build commands. Using the build spec method allows you to take advantage of the managed build image that is maintained by the CodeBuild service, while still giving you the customized environment that you need to build your applications.

The second method of customizing the CodeBuild environment is by providing CodeBuild with a container image that is then used as the build environment instead of the managed image. Because you are providing the service with a container image, you have complete flexability to create and build the container any way that you need. Using this method you install what ever packages you need, and set apply any configuration settings you need. However with this method, you are completely responsible for maintinging your build image, including making sure all packages are up to date, software is patched, etc.

I highly recommend using the BuildSpec method of customizing the CodeBuild environment where ever possible. Using this method allows you to take advantage of the most benifits of the managed runtime, and avoides the operational overhead of keeping your build image up to date.

Full documentation on the BuildSpec file can be found here

Setting up your buildspec.yml file

Your busildspec.yml file is placed in the root forder of your project. This will normally be at the same level as the solution file. For a project that is targetting .NET 6, the build spec file will look like this:

version: 0.2  
phases:
  build:
    commands:
      - dotnet restore MySolutionFile.sln
      - dotnet build MySolutionFile.sln

This Build Spec file has only the commands necessary to build the .NET application. You will normally have additional commands to packae up the bild artifaces after the Build phase completes. Adding .NET 7 support

Adding .NET support to Code Build is quite easy. By adding the following commands to your Build Spec file you can add the .NET 7 RC2 SDK to your build environment and start building your .NET 7 applications today.

  install:
    commands:
      - curl -O https://download.visualstudio.microsoft.com/download/pr/f5c74056-330b-452b-915e-d98fda75024e/18076ca3b89cd362162bbd0cbf9b2ca5/dotnet-sdk-7.0.100-rc.2.22477.23-linux-x64.tar.gz
      - sudo tar -zxvf dotnet-sdk-7.0.100-rc.2.22477.23-linux-x64.tar.gz -C /root/.dotnet

The CURL Command downloads the .NET 7 RC2 Tar Zip file from Microsoft and stores it locally.

The Tar command extracts the .NET 7 SDK into the location that Code Build uses to store the .NET SDKs. In this case /root/.dotnet.

Updated buildspec file

Your Updated BuildSpec file will look like this:

version: 0.2
phases:
  install:
    commands:
      - curl -O https://download.visualstudio.microsoft.com/download/pr/f5c74056-330b-452b-915e-d98fda75024e/18076ca3b89cd362162bbd0cbf9b2ca5/dotnet-sdk-7.0.100-rc.2.22477.23-linux-x64.tar.gz
      - sudo tar -zxvf dotnet-sdk-7.0.100-rc.2.22477.23-linux-x64.tar.gz -C /root/.dotnet
  build:
    commands:
      - dotnet restore MySolutionFile.sln
      - dotnet build MySolutionFile.sln

With the Updated BuildSpec.YML file checked in, Code Build will download and install the .NET 7 RC2 SDK. Once downloaded, the build commands will target the new version of the SDK and allow you to build using the .NET 7 SDK. Summary

It’s very easy to incorporate new, or different versions of the .NET SDK into your build process. By using the BuildSpec method of customizing your build environment you can use the latest versions of the .NET, including Pre-Release versions.

I have posted video guidance on both setting up your CodeBuild project, and incorporating the .NET 7 SDK on the Basement Programmer youtube channel here

You can also get a copy of the source code used in the video here

Did you find this article valuable?

Support Tom Moore by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
 
Share this