在 Azure DevOps 中 运行 yaml-pipeline 时将 repo 装载到 docker 图像中

Mount repo into docker image when running yaml-pipeline in Azure DevOps

我是 运行 Azure Devops yaml 管道中的 docker 图像,使用 container step。但是,我在安装 repo 的内容时遇到问题,因此可以从 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'

失败并显示错误消息:

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

我也尝试用 $[..] 替换 $(..)$(参见 here 但这会导致同样的错误。另外 ${{..}} 管道甚至不会启动(错误: "A template expression is not allowed in this context" 在 UI)

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

对于非 yaml 管道,问题已得到解决

有什么想法可以实现吗?或者我是否需要创建一个新的 docker 图像,其中 repo 文件已经 add:ed?

Any ideas how to accomplish this? Or do I need to create a new docker image where the repo files have been add:ed?

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

container:
  image: 'image-name'

steps:
- script: echo Hello, world!
  displayName: 'Run a one-line script inside docker image'

Initialize containers任务中,Agent.BuildDirectoryBuild.Repository.LocalPathBuild.SourcesDirectorypredefined variables不展开(非定义变量)

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

1.About 为什么 you shared above can work: It's in a docker task/step 所以它可以识别 $(Build.SourcesDirectory) 变量。 (真正的构建任务运行 定义构建变量之后)

2.If 您正在使用特定的 Micorosft-hosted agent, you can try hard-coding the path. You can check this similar issue.

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

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