Why am I getting the error: "Error while executing command: dotnet publish"?

Why am I getting the error: "Error while executing command: dotnet publish"?

我创建了要在 AWS CodeBuild 中使用的 buildspec.yml 文件,当我 运行它:

COMMAND_EXECUTION_ERROR Message: Error while executing command: dotnet publish -c release -o build/outputs/Host. Reason: exit status 1

我已经检查了我在“-o”参数中传递的路径,我什至删除了它,但我一直收到错误。

我正在使用 dotnet 5.0。我需要在构建之前安装它吗?如果是,我该怎么做?

下面是我 buildspec.yml 中重要的片段:

version: 0.2

phases:
  install:
    runtime-versions:
      docker: 18
  
  pre_build:
    commands:
      - echo pre-build started on `date`
      - echo Logging in to Amazon ECR...
      - aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com
      
  build:
    commands:
      - echo Building dotnet API...
      - cd aspnet-core/src/SITR.Web.Host
      - echo Executing dotnet restore...
      - dotnet restore
      - echo Executing dotnet publish...
      - dotnet publish -c release -o ../../../docker/build/outputs/Host
  
  post_build:
    commands:
      - echo Pushing the Docker image...
      - docker-compose --file=docker-compose-qa.yml push
      - echo Build completed on `date`

dotnet: 5.0 仅在 dotnet 运行 时间支持。但是你正在使用 docker 运行 时间。所以要么你必须改变你的运行时间,要么在你的运行时间手动setup/install dotnet。

Marcin 的回复帮助我想出了解决方案。我在另一个答案中记录了我需要做的事情,也许它会对其他人有所帮助。

对于我的构建,我需要使用 dotnet 5nodejs (npm)docker .

问题是我使用的是 Amazon Linux 2 并且根据 this documentation dotnet 5 是目前仅在 Ubuntu 5.0 中可用.

一开始我还是有点疑惑,因为我以为我需要安装docker,但实际上它已经安装在ubuntu 5.0 镜像中了。为了测试这一点,我 编辑了我的 CodeBuild 并将版本更改为 Ubuntu 5.0 和 运行 我的 buildspec 使用 docker --version 命令如下所示,并检查了日志。

version: 0.2

phases:
  install:
    runtime-versions:
      dotnet: 5.0
      nodejs: 14
    commands:
      - docker --version

基本上我需要切换到 Ubuntu 5.0 版本,我的 buildspec.yml 看起来像这样:

version: 0.2

phases:
  install:
    runtime-versions:
      dotnet: 5.0
      nodejs: 14

  pre_build:
    commands:
      - echo pre-build started on `date`
      - echo Logging in to Amazon ECR...
      - aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com
      
  build:
    commands:
      - echo Building dotnet API...
      - cd aspnet-core/src/SITR.Web.Host
      - echo Executing dotnet restore...
      - dotnet restore
      - echo Executing dotnet publish...
      - dotnet publish -c release -o ../../../docker/build/outputs/Host
  
  post_build:
    commands:
      - echo Pushing the Docker image...
      - docker-compose --file=docker-compose-qa.yml push
      - echo Build completed on `date`