为什么此ASP.NET核心Docker构建在Azure管道中失败?

为什么此ASP.NET核心Docker构建在Azure管道中失败?,docker,azure-devops,azure-pipelines,Docker,Azure Devops,Azure Pipelines,在Azure Pipelines中构建ASP.NET核心项目时,下面的YAML文件仅在将解决方案和项目放置在同一目录中时有效。如果不需要将解决方案和项目放在同一目录中,可以对下面的YAML文件执行什么操作?如果有,这很重要 以下是采取的步骤: 使用Visual Studio 2019创建了支持Linux Docker容器的新ASP.NET Core 3.1项目 推送到Azure存储库“DockerTest” 使用模板“Docker:Build and push a image to Azure

在Azure Pipelines中构建ASP.NET核心项目时,下面的YAML文件仅在将解决方案和项目放置在同一目录中时有效。如果不需要将解决方案和项目放在同一目录中,可以对下面的YAML文件执行什么操作?如果有,这很重要

以下是采取的步骤:

  • 使用Visual Studio 2019创建了支持Linux Docker容器的新ASP.NET Core 3.1项目
  • 推送到Azure存储库“DockerTest”
  • 使用模板“Docker:Build and push a 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)
    

    我以前也遇到过这种情况

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

    我通过将管道的构建上下文设置为使用变量
    $(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)
    
    阶段:
    -阶段:建造
    displayName:构建和推送阶段
    工作:
    -工作:建造
    显示名称:构建
    游泳池:
    vmImage:$(vmImageName)
    步骤:
    -任务:Docker@2
    displayName:生成图像并将其推送到容器注册表
    投入:
    命令:buildAndPush
    存储库:$(imageRepository)
    dockerfile:$(dockerfilePath)
    
    buildContext:$(Build.Repository.LocalPath)这在经典编辑器中起作用,但最好有YAML解决方案。@crayden更新了我的答案以包含构建上下文