简体   繁体   中英

Why is this ASP.NET Core Docker build failing in Azure Pipelines?

When building an ASP.NET Core project in Azure Pipelines, the YAML file below only works when placing the solution and project in the same directory. What can be done to the YAML file below without the need of placing the solution and project in the same directory? This matters in case there are multiple projects in a single solution .

Here are the steps taken:

  1. Created new ASP.NET Core 3.1 project with support for Linux Docker container using Visual Studio 2019.
  2. Pushed to Azure repository "DockerTest".
  3. Created pipeline using template "Docker: Build and push an image to Azure Container Registry", which essentially generated the YAML below.

Which resulted in the following error during the build:

COPY failed: stat /var/lib/docker/tmp/docker-builder701699653/DockerTest/DockerTest.csproj: no such file or directory

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)

This has happened to me before.

The pipeline is configured to look for your .csproj file in the build agent itself - "docker-builder701699653".

I resolved this by setting the Build context of the pipeline to use the variable $(Build.Repository.LocalPath) .

It will then use the repository to build the image. In terms of YAML I don't know what you will have to set as I use the classic editor.

EDIT

See yaml with build context

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM