具有多个存储库的vsts管道

时间:2020-09-07 22:19:16

标签: azure-devops azure-pipelines azure-yaml-pipelines

我正在尝试使用yaml语法创建具有多个存储库的管道。

# Deploy to Azure Kubernetes Service
# Build and push image to Azure Container Registry; Deploy to Azure Kubernetes Service
# https://docs.microsoft.com/azure/devops/pipelines/languages/docker

trigger:
- master

resources:      
  repositories:   
  - repository: Shared
    name: Shared.lib
    type: git 
    ref: master


variables:

  # Container registry service connection established during pipeline creation
  dockerRegistryServiceConnection: 'obfuscated'
  imageRepository: 'filepod'
  containerRegistry: 'obfuscated.azurecr.io'
  dockerfilePath: '**/Dockerfile'
  tag: '$(Build.BuildId)'
  imagePullSecret: 'obfuscated-auth'

  # Agent VM image name
  vmImageName: 'ubuntu-latest'
  
  # Name of the new namespace being created to deploy the PR changes.
  k8sNamespaceForPR: 'review-app-$(System.PullRequest.PullRequestId)'

stages:
- stage: Build
  displayName: Build stage
  jobs:  
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)
    steps:
    - checkout: Self
    - checkout: Shared
    - task: Docker@2
      displayName: Build and push an image to container registry
      inputs:
        command: buildAndPush
        repository: $(imageRepository)
        dockerfile: $(dockerfilePath)
        containerRegistry: $(dockerRegistryServiceConnection)
        tags: |
          $(tag)
          
    - upload: manifests
      artifact: manifests

- stage: Deploy
  displayName: Deploy stage
  dependsOn: Build

它失败并显示错误: 步骤3/15:复制[“ File.Pod / File.Pod.csproj”,“ File.Pod /”] 拷贝失败:stat /var/lib/docker/tmp/docker-builder593133002/File.Pod/File.Pod.csproj:没有这样的文件或目录 ## [错误]复制失败:stat /var/lib/docker/tmp/docker-builder593133002/File.Pod/File.Pod.csproj:无此类文件或目录 ## [错误] / usr / bin / docker失败,返回码:1

我绝对不知道为什么找不到csproj(它位于File.Pod的根目录中

编辑:
这是文件夹结构
资料库1:
C:\ git \ File.POD:包含dockerfile和File.POD.csproj
资料库2:
c:\ git \ Shared.Lib \ File.Service:包含File.Service.csproj
c:\ git \ Shared.Lib \ File.DAL:包含File.Dal.csproj

DockerFile:

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-alpine AS build
WORKDIR /src
COPY ["File.Pod.csproj", "File.Pod/"]

Note: I also tried the following but it throws an error saying : Forbidden path outside the build context
COPY ["../Shared.Lib/File.Service/File.Service.csproj", "Shared.Lib/File.Service/"]
COPY ["../Shared.Lib/File.DAL/File.DAL.csproj", "Shared.Lib/File.DAL/"]  

COPY ["nuget.config", "./"] 
RUN dotnet restore "File.Pod/File.Pod.csproj" --configfile nuget.config -nowarn:msb3202,nu1503 --verbosity diag
COPY . .
WORKDIR "/src/File.Pod"

FROM build AS publish
RUN dotnet publish "File.Pod.csproj" -c Release -o /app

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-bionic AS base
RUN apt update && apt install -y openssh-client
WORKDIR /app
COPY --from=publish /app .
EXPOSE 80
ENTRYPOINT ["dotnet", "File.Pod.dll"]

您会注意到dockerfile没有file.service的副本(我尝试了几次和语法)。 错误一直存在:
2020-09-09T21:51:40.1492619Z步骤9/24:RUN dotnet发布“ File.Pod.csproj” -c版本-o / app
2020-09-09T21:51:40.1744542Z --->在366a72721b48中运行
2020-09-09T21:51:40.7879735Z适用于.NET的Microsoft(R)Build Engine版本16.7.0 + 7fb82e5b2
2020-09-09T21:51:40.7880612Z版权所有(C)微软公司。保留所有权利。
2020-09-09T21:51:40.7881083Z
2020-09-09T21:51:41.2560630Z确定要还原的项目...
2020-09-09T21:51:41.2590027Z跳过项目
“ /src/Shared.Lib/File.Service/File.Service.csproj”,因为未找到。 2020-09-09T21:51:41.2607019Z跳过项目 2020-09-09T21:51:42.5389794Z
/usr/share/dotnet/sdk/3.1.402/Microsoft.Common.CurrentVersion.targets(1850,5):警告:引用的项目'../Shared.Lib/File.Service/File.Service.csproj'确实不存在。 [/src/File.Pod/File.Pod.csproj]

2 个答案:

答案 0 :(得分:1)

如果您有multiple checkout steps

每个指定的存储库都检出到以该存储库命名的文件夹中,除非在检出步骤中指定了其他路径。要将self检出为存储库之一,请使用checkout:self作为检出步骤之一。

因此,如果您的Fil.Pod.csproj位于共享存储库中,则可能位于文件夹Shared.lib中。您可以通过添加以下内容进行检查:

- script: |
    echo $(Build.SourcesDirectory)
    ls $(Build.SourcesDirectory) *

如果您的DOCKERFILE只需要一个仓库,则可以向您的Docker任务添加buildContext

    - task: Docker@2
      displayName: Build and push an image to container registry
      inputs:
        command: buildAndPush
        repository: $(imageRepository)
        dockerfile: $(dockerfilePath)
        containerRegistry: $(dockerRegistryServiceConnection)
        buildContext: '$(Build.SourcesDirectory)/Shared.lib'
        tags: |
          $(tag)

如果DOCKERFILE需要访问这两个目录,我需要查看它以进一步说明问题。

答案 1 :(得分:1)

1。要确保docker build操作可以访问这两个存储库,我们需要将源目录设置为buildContext。将您的步骤设置更改为:

    - task: Docker@2
      displayName: Build and push an image to container registry
      inputs:
        command: buildAndPush
        repository: $(imageRepository)
        dockerfile: $(dockerfilePath)
        containerRegistry: $(dockerRegistryServiceConnection)
        buildContext: '$(Build.SourcesDirectory)'
        tags: |
          $(tag)

buildContext: '$(Build.SourcesDirectory)'将解决Forbidden path outside the build context

2。由于我们将SourceDirectory设置为docker build步骤的buildContext,因此我们现在需要修改Dockerfile中定义的相关路径。

  • 基于buildContext: '$(Build.SourcesDirectory)',我们应该使用COPY ["File.Pod/File.Pod.csproj", "File.Pod/"]而不是COPY ["File.Pod.csproj", "File.Pod/"]

  • 然后在复制../File.Service.csproj时删除File.DAL.csproj

  • 如果问题仍然存在,请注意区分大小写!!!您在yml中定义了name: Shared.lib,因此该文件夹将是Shared.lib而不是Shared.Lib。因此,您不能使用COPY ["Shared.Lib/File.Service/File.Service.csproj", "xxx"],而必须使用COPY ["Shared.lib/File.Service/File.Service.csproj", "xxx"]。我重现了同样的问题,并发现Dockerfile中的COPY命令区分大小写!