c# - aws codebuild dotnet :5. 0 - 没有可用的 sdk 命令

标签 c# amazon-web-services docker .net-core aws-codebuild

我正在将我的后端升级到 dotnet 5.0,但现在我的 aws buildpiplines 在 aws codebuild 步骤上失败了。我运行的 docker 镜像是 Ubuntu 20.04 aws/codebuild/standard:5.0 绝对支持 dotnet5.0。

这是在 codebuild 上看到的错误:

Step 6/11 : RUN dotnet publish -c Release -o out
 ---> Running in f2d4da28b101
        5.0.301 [/usr/share/dotnet/sdk]
Could not execute because the application was not found or a compatible .NET SDK is not installed.
Possible reasons for this include:
  * You intended to execute a .NET program:
      The application 'publish' does not exist.
  * You intended to execute a .NET SDK command:
      A compatible installed .NET SDK for global.json version [5.0.202] from [/app/global.json] was not found.
      Install the [5.0.202] .NET SDK or update [/app/global.json] with an installed .NET SDK:
The command '/bin/sh -c dotnet publish -c Release -o out' returned a non-zero code: 145

发现这真的很奇怪,因为我下面的 dockerfile 清楚地提取了 sdk 版本

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build-env
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out

# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:5.0
WORKDIR /app
COPY --from=build-env /app/out .

ENV ASPNETCORE_URLS http://*:443
ENTRYPOINT ["dotnet",  "user-bff.dll"]
version: 0.2

phases:
  install:
    runtime-versions:
      dotnet: 5.0
  pre_build:
    commands:
      - echo Logging in to Amazon ECR
      - aws ecr get-login-password | docker login --username AWS --password-stdin $REPOSITORY_URI
      - IMAGE_URI="${REPOSITORY_URI}:latest"
  build:
    commands:
      - echo Build started on `date`
      - echo Building the Docker image...
      - docker build --tag "$IMAGE_URI" .
      - docker tag $REPOSITORY_URI:latest $REPOSITORY_URI:latest

  post_build:
    commands:
      - echo Build completed on `date`
      - echo Push the latest Docker Image...
      - docker push "$IMAGE_URI"
      - printf '[{"name":"user-bff","imageUri":"%s"}]' "$IMAGE_URI" > images.json
artifacts:
  files:
    - buildspec.yml
    - images.json

还有我的构建规范文件

寻求任何可能的帮助!认为它可能与 ubuntu 有关,因为它在我的本地 Windows 机器上运行良好。我唯一改变的是运行时图像、codebuild 图像和将我的应用程序更新到 dotnet 5.0

最佳答案

同样的问题 - 结果是构建规范安装阶段的结果导致了问题。安装 dotnet 5.x 时,会生成 global.json 文件。此文件规定需要/支持哪个 SDK(更多信息请参见:https://learn.microsoft.com/en-us/dotnet/core/tools/global-json)。

enter image description here

buildspec 阶段安装的 SDK 版本是 5.0.202.,但是正如您所指出的,您使用的 docker 镜像已经有相关的 SDK(目前是 5.0.301)。 当 Docker 构建开始时,您将该 global.json 文件(声明 COPY ../)复制到您的项目结构中,有效地添加了对应该使用什么 SDK 来构建项目的限制。这会导致构建失败,因为所需的 SDK(global.json)不存在。

要修复此问题,请确保在运行 SDK 相关语句(构建、发布...)之前删除 global.json 文件。您可以修改预构建阶段并删除指定的文件,Docker 构建应该会顺利进行。

  pre_build:
    commands:
      - echo Removing global.json file
      - rm ./global.json -f
      - ...

关于c# - aws codebuild dotnet :5. 0 - 没有可用的 sdk 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68065502/

相关文章:

bash -/bin/bash : Command not found in alpine docker

c# - 返回列表 1 和列表 2 中的项目匹配的列表

c# - MonoGame 3.5 模板不会与 Visual Studio 一起安装

python - 通过 AWS PrivateLink 从不同的 AWS 账户访问 AWS ElastiCache(Redis CLUSTER 模式)

c# - 我应该创建并重用 AmazonSimpleEmailServiceClient 作为单例吗?

Maven:找不到前缀 'dockerfile' 的插件

django - 公开之前如何预热Django Web服务?

c# - RhinoMock 中带有 stub 的可选参数

c# - 在后台线程中释放 COM 对象

mysql - 确定 RDS 中断的原因