更改 Azure Pipelines YAML 中所有步骤的工作目录

sli*_*kts 9 yaml azure-devops azure-pipelines azure-pipelines-yaml

可以为单独的脚本设置工作目录:

- script: foo
  workingDirectory: bar
Run Code Online (Sandbox Code Playgroud)

但是,如果所有步骤都打算在特定目录中运行,则为每个步骤定义它就会变得重复。

使用cd不影响其他步骤:

- script: cd foo
- script: pwd # returns default working dir instead of foo
Run Code Online (Sandbox Code Playgroud)

这个问题很重要的两个具体例子是:

  • 当将多个存储库作为资源签出时,因此默认工作目录比签出的存储库高一级
  • 为位于子目录中的项目运行管道时(例如在 monorepo 中)

sli*_*kts 2

解决方法是将文件移动到默认工作目录中,而不是更改任务的工作目录,一种方便的方法是使用git-sparse-checkout如下所示:

git sparse-checkout set example && mv example/{*,.*} . || true
Run Code Online (Sandbox Code Playgroud)

{*,.*}部分还用于移动点文件,并且|| true是必需的,因为它也尝试移动...