具有多个存储库的 vsts 管道

vsts pipeline with multiple repositories

我正在尝试使用 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/"] COPY 失败:stat /var/lib/docker/tmp/docker-builder593133002/File.Pod/File.Pod.csproj:没有那个文件或目录 ##[error]COPY 失败:stat /var/lib/docker/tmp/docker-builder593133002/File.Pod/File.Pod.csproj: 没有这样的文件或目录 ##[错误]/usr/bin/docker 失败,代码为 return:1

我完全不知道为什么它找不到 csproj(它在 File.Pod

的根目录中

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

Docker文件:

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:运行 dotnet publish "File.Pod.csproj" -c Release -o /app
2020-09-09T21:51:40.1744542Z ---> 运行 在 366a72721b48
2020-09-09T21:51:40.7879735Z Microsoft (R) 构建引擎版本 16.7.0+7fb82e5b2 for .NET
2020-09-09T21:51:40.7880612Z 版权所有 (C) Microsoft Corporation。保留所有权利。
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]

如果你有 multiple checkout steps:

Each designated repository is checked out to a folder named after the repository, unless a different path is specified in the checkout step. To check out self as one of the repositories, use checkout: self as one of the checkout steps.

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

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

如果你的 DOCKERFILE 只需要一个 repo 你可以添加 buildContext 给你 Docker task:

    - 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.To 确保docker build 操作可以访问这两个repos,我们需要将源目录设置为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.Since 我们将 SourceDirectory 设置为 docker 构建步骤的 buildContext,我们现在需要修改 Dockerfile 中定义的相关路径。

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

  • 然后在复制File.Service.csprojFile.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 命令区分大小写!