Azure构建流程的YAML模板参考

9
我有一个步骤定义模板,打算在构建流水线中使用。 步骤定义的位置与构建流水线本身所在的文件夹不同。
在流水线验证期间,AzureDevops将构建流水线的位置视为根位置。这个位置被添加到引用路径中。
考虑以下代码层次结构示例。
 azure
   |----products
           |----resource-type1
                        |----step-def.yaml
           |----resource-type2
                        |----step-def.yaml
   |----solutions
           |----solution1
                    |----local-step-def.yaml
                    |----build.yaml
           |----solution2
                    |----build.yaml

以下适用于build.yaml如下所示:
jobs:
- job: Linux
  pool:
    vmImage: 'ubuntu-16.04'
  steps:
  - template: solution1/local-step-def.yml

如果您按照以下方式更改模板引用,将不起作用。
  - template: ../products/resource-type1/step-def.yml

在管道上进行验证时,Azure DevOps 会映射到

# <path-of-the-build-pipeline>/<template-ref>
azure/solutions/solution1/<template-reference>

这里是文档,https://learn.microsoft.com/en-us/azure/devops/pipelines/process/templates?view=azure-devops#step-re-use 那么我该如何映射到存储在产品文件夹层次结构中的 step-def.yaml 文件?

1
我知道回答有点晚了,但也许对某些人有帮助。你需要向上导航两个文件夹才能使相对路径起作用,例如:.../../products/resource-type1/step-def.yml。 - Dhruv Patel
@Dhruv..抱歉,当在管道的开头声明宏语法变量时,这种方法不起作用。 - koushik
我认为 @Nuurek 的回答应该有效。 - Dhruv Patel
2个回答

18

我只找到了一种实际执行的解决方案。您可以使用绝对路径引用父目录。关键是使用系统变量填充根路径。针对您的示例的解决方案:

jobs:
- job: Linux
  pool:
    vmImage: 'ubuntu-16.04'
  steps:
  - template: ${{variables['System.DefaultWorkingDirectory']}}/products/resource-type1/step-def.yml

这是在使用具有相同流程的多个存储库时的唯一可行方案,谢谢。 - BearOakheart

7

尽管它是相同的仓库,但你可以尝试使用repositoryResource语法。

resources:
  repositories:
    - repository: pckgtemplates #resource name to be used in the build pipeline
      type: git #Azure git 
      name: packages/packagesRepo

jobs:
- template: templates\version.file.yml@pckgtemplates  
  parameters:
    versionFile: versionFile

- 模板: templates\version.file.yml@pckgtemplates 这个引用了 pckgtemplates 资源中 templates 文件夹下的 version.file.yml 模板。

pckgtemplates 资源则是引用当前组织中 packages 项目和 packagesRepo 存储库。

通过这种方式,您可以定义一个引用相同项目和 products/resource-type1/step-def.yml 模板的资源。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接