0
votes

When I use this CI config file get build error from gitlab pipeline :

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

image: microsoft/dotnet:latest


variables:
    # 1) Name of directory where restore and build objects are stored.
    OBJECTS_DIRECTORY: 'obj'
    # 2) Name of directory used for keeping restored dependencies.
    NUGET_PACKAGES_DIRECTORY: '.nuget'
    # 3) A relative path to the source code from project repository root.

    SOURCE_CODE_PATH: '*/*/'

# ### Define stage list

stages:
    - build
    - test

# ### Define global cache rule
cache:
    # Per-stage and per-branch caching.
    key: "$CI_JOB_STAGE-$CI_COMMIT_REF_SLUG"
    paths:
    # Specify three paths that should be cached:
    #
    # 1) Main JSON file holding information about package dependency tree, packages versions,
    # frameworks etc. It also holds information where to the dependencies were restored.
    - '$SOURCE_CODE_PATH$OBJECTS_DIRECTORY/project.assets.json'
    # 2) Other NuGet and MSBuild related files. Also needed.
    - '$SOURCE_CODE_PATH$OBJECTS_DIRECTORY/*.csproj.nuget.*'
    # 3) Path to the directory where restored dependencies are kept.
    - '$NUGET_PACKAGES_DIRECTORY'
    
    # 'pull-push' policy is the default cache policy, you do not have to specify it explicitly.
    policy: pull-push

# ### Restore project dependencies
before_script:
    - 'dotnet restore --packages $NUGET_PACKAGES_DIRECTORY'

build:
    stage: build

    script:
    - 'dotnet build --no-restore'

tests:
    stage: test
    # ### Run the tests
    
    script:
    - 'dotnet test --no-restore'
2

2 Answers

0
votes

We should write dotnet version exactly:

use

image: mcr.microsoft.com/dotnet/core/sdk:5.0

instead of

image: microsoft/dotnet:latest
0
votes

It sounds like your CI/CD machine needs the latest version of the .NET Core SDK installed on it. Or peg you project to the version of .NET Core installed on your build machine, which appears to be v2.1 based on the error.