在 Azure DevOps 中运行 yaml-pipeline 时将存储库挂载到 docker 映像中

deh*_*hkp 4 docker mounted-volumes azure-devops

我正在使用容器步骤在 Azure Devops yaml-pipeline 中运行 docker 映像。但是,我在安装存储库内容时遇到问题,以便可以从 docker 映像内部访问它。

Azure Devops pipeline.yml 文件如下:

container:
  image: 'image-name'
  endpoint: 'foo'
  options: '-v $(Build.SourcesDirectory):/testing'

steps:
- script: echo Hello, world!
  displayName: 'Run a one-line script inside docker image'
Run Code Online (Sandbox Code Playgroud)

此操作失败并显示错误消息:

Error response from daemon: create $(Build.SourcesDirectory): "$(Build.SourcesDirectory)" includes 
invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intended 
to pass a host directory, use absolute path
Run Code Online (Sandbox Code Playgroud)

我还尝试替换$(..)$$[..](请参阅此处,但这会导致相同的错误。此外,${{..}}管道甚至不会启动(错误:UI 中的“在此上下文中不允许使用模板表达式”)

如果我删除options脚本运行,但存储库未安装。

对于非 yaml 管道,问题已在此处解决。

有什么想法如何实现这一点?或者我是否需要创建一个新的 docker 映像,其中已添加存储库文件:ed?

Lan*_*SFT 6

有什么想法如何实现这一点?或者我是否需要创建一个新的 docker 映像,其中已添加存储库文件:ed?

当直接指定Container使用Yaml Schema时,Azure DevOps服务将在任务和您的实际任务Initialize containers之前自动调用额外的任务。checkout source repo

container:
  image: 'image-name'

steps:
- script: echo Hello, world!
  displayName: 'Run a one-line script inside docker image'
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

在任务期间Initialize containers预定义变量Agent.BuildDirectoryBuild.Repository.LocalPathBuild.SourcesDirectory不会扩展(非定义变量)。

因此,您不能Build.SourcesDirectory以这种方式使用,因为该变量的值在任务完成Initialize containers会扩展。

1.关于为什么您上面共享的链接可以工作:它位于docker 任务/步骤中,因此它可以识别该$(Build.SourcesDirectory)变量。(真正的构建任务在定义构建变量后运行)

2.如果您使用特定的Micorosft 托管代理,您可以尝试对路径进行硬编码。你可以检查这个类似的问题

通常适用于 Windows 托管代理:$(Build.SourcesDirectory)=>D:\a\1\s

对于 Ubuntu 托管代理:$(Build.SourcesDirectory)=> /home/vsts/work/1/s