为什么此 ASP.NET Core Docker 构建在 Azure Pipelines 中失败?

cra*_*den 3 docker azure-devops azure-pipelines

在 Azure Pipelines 中构建 ASP.NET Core 项目时,下面的 YAML 文件仅在将解决方案和项目放在同一目录中时才有效。不需要将解决方案和项目放在同一目录下,可以对下面的YAML文件做什么?这在单个解决方案中多个项目的情况下很重要。

以下是采取的步骤:

  1. 使用 Visual Studio 2019 创建了支持 Linux Docker 容器的新 ASP.NET Core 3.1 项目。
  2. 推送到 Azure 存储库“DockerTest”。
  3. 使用模板“Docker: Build and push an image to Azure Container Registry”创建了管道,它基本上生成了下面的 YAML。

这导致在构建过程中出现以下错误:

复制失败:stat /var/lib/docker/tmp/docker-builder701699653/DockerTest/DockerTest.csproj:没有这样的文件或目录

azure-pipelines.yml

# Docker
# Build and push an image to Azure Container Registry
# https://docs.microsoft.com/azure/devops/pipelines/languages/docker

trigger:
- master

resources:
- repo: self

variables:
  # Container registry service connection established during pipeline creation
  dockerRegistryServiceConnection: 'example-connection-string'
  imageRepository: 'dockertest'
  containerRegistry: 'example.azurecr.io'
  dockerfilePath: '$(Build.SourcesDirectory)/DockerTest/Dockerfile'
  tag: '$(Build.BuildId)'

  # Agent VM image name
  vmImageName: 'ubuntu-latest'

stages:
- stage: Build
  displayName: Build and push stage
  jobs:  
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)
    steps:
    - task: Docker@2
      displayName: Build and push an image to container registry
      inputs:
        command: buildAndPush
        repository: $(imageRepository)
        dockerfile: $(dockerfilePath)
        containerRegistry: $(dockerRegistryServiceConnection)
        tags: |
          $(tag)
Run Code Online (Sandbox Code Playgroud)

cl0*_*0ud 6

我以前也遇到过这种情况。

管道配置为在构建代理本身中查找您的 .csproj 文件 - “docker-builder701699653”。

我通过将管道的 Build 上下文设置为使用变量来解决这个问题$(Build.Repository.LocalPath)

然后它将使用存储库来构建映像。就 YAML 而言,我不知道您在使用经典编辑器时必须设置什么。

编辑

使用构建上下文查看 yaml

stages:
- stage: Build
  displayName: Build and push stage
  jobs:  
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)
    steps:
    - task: Docker@2
      displayName: Build and push an image to container registry
      inputs:
        command: buildAndPush
        repository: $(imageRepository)
        dockerfile: $(dockerfilePath)
        buildContext : $(Build.Repository.LocalPath) <----
        containerRegistry: $(dockerRegistryServiceConnection)
        tags: |
          $(tag)
Run Code Online (Sandbox Code Playgroud)