Azure Devops: 是否可以在一个 yaml 模板中嵌套另一个 yaml 模板?

9

能否在另一个yaml模板中嵌套yaml模板?

我有多个NuGet项目分布在不同的Git仓库中,我试图将发布NuGet到nuget.org的过程进行模板化。

因此,我创建了一个名为“devops-templates”的git仓库,编写了第一个yaml模板,并确保它可以正常工作,然后将其分成4个yaml模板(构建解决方案、生成包、运行单元测试、发布),并将它们引用到全局yaml模板中。

问题是,当我尝试在我的管道中使用这个全局模板时,出现了错误。

/Net/Utilities/BuildSolution.yml@templates (Line: 33, Col: 18): 模板表达式不允许在此上下文中使用,/Net/Utilities/BuildSolution.yml@templates (Line: 36, Col: 21): 模板表达式不允许在此上下文中使用,/Net/Utilities/BuildSolution.yml@templates (Line: 48, Col: 24): 模板表达式不允许在此上下文中使用,/Net/Utilities/BuildSolution.yml@templates (Line: 53, Col: 28): 模板表达式不允许在此上下文中使用,/Net/Utilities/BuildSolution.yml@templates (Line: 54, Col: 26): 模板表达式不允许在此上下文中使用,/Net/Utilities/BuildSolution.yml@templates (Line: 59, Col: 21): 模板表达式不允许在此上下文中使用,/Net/Utilities/BuildSolution.yml@templates (Line: 60, Col: 22): 模板表达式不允许在此上下文中使用,/Net/Utilities/BuildSolution.yml@templates (Line: 61, Col: 32): 模板表达式不允许在此上下文中使用,/Net/Utilities/BuildSolution.yml@templates (Line: 63, Col: 21): 模板表达式不允许在此上下文中使用,/Net/Utilities/BuildSolution.yml@templates (Line: 64, Col: 26): 模板表达式不允许在此上下文中使用
我是一名有用的助手,可以为您翻译文本。

我在Microsoft文档中进行了搜索: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/templates?view=azure-devops,但没有找到相关信息。

以下是我的代码的一些部分:

azure-pipelines.yml (主要存储库):

resources:
  repositories:
    - repository: templates
      type: github
      name: (...)/devops-templates
      ref: refs/tags/v1.1.0
      endpoint: (...)

stages:
- template: Net/Pipeline/NuGetsPipeline.yml@templates
  parameters:
    solution: $(solution)
    nuGetsArtifactName: $(nuGetsArtifactName)
    buildArtifactName : $(buildArtifactName)
    (...)

NuGetsPipeline.yml(devops-templates存储库):

parameters:
 nuGetsArtifactName: 'NuGets'
 buildArtifactName : 'Build'
 nuGetSource: https://api.nuget.org/v3/index.json
 solution: ''
 (...)

stages:

- stage: Build
  jobs:
  - template: ${{variables['System.DefaultWorkingDirectory']}}/Net/Utilities/BuildSolution.yml
    parameters:
      buildArtifactName : ${{ parameters.buildArtifactName }}
      (...)

  - template: ${{variables['System.DefaultWorkingDirectory']}}/Net/Utilities/GenerateNuGets.yml
    parameters:
      nuGetsArtifactName: ${{ parameters.nuGetsArtifactName }}
      buildArtifactName : ${{ parameters.buildArtifactName }}
      (...)

- stage: 'UnitTests'
  jobs:
  - template: ${{variables['System.DefaultWorkingDirectory']}}/Net/Utilities/RunUnitTests.yml
    parameters:
      buildArtifactName : ${{ parameters.buildArtifactName }}
      (...)

- stage: 'Publish'
  jobs:
  - template: ${{variables['System.DefaultWorkingDirectory']}}/Net/Utilities/PublishNuGets.yml
    parameters:
      nuGetsArtifactName: ${{ parameters.nuGetsArtifactName }}
      (...)

BuildSolution.yml(devops-template存储库):

parameters:
  buildArtifactName: 'Build'
  solution: ''
  (...)

  jobs:
  - job: 'BuildSolution'
    pool:
        vmImage: ${{ parameters.vmImage }}
    continueOnError: false
    variables:
      artifactName: ${{ parameters.buildArtifactName }}
    steps:
      - task: NuGetCommand@2
        displayName: 'Restore NuGet packages'
        inputs:
          restoreSolution: ${{ parameters.solutionDir }}/${{ parameters.solution }}
          configuration: ${{ parameters.buildConfiguration}}

      - task: VSBuild@1
        (...)

编辑:我添加了一些代码部分。


3
可以的,您能展示一下您的模板以及如何调用它们吗? - 4c74356b41
如错误所示,您的模板可能存在错误。能否请您发表一下您的模板? - Levi Lu-MSFT
是的,当然。我编辑了我的帖子。 - Vianney Doleans
1
问题已解决。是的,可以在其他yaml模板中嵌套yaml模板,我的错误在于缩进。感谢你们的回复! - Vianney Doleans
2个回答

6
您的 BuildSolution.yml 文件中似乎存在缩进错误。 parameters 和 jobs 应该有相同的缩进。请参考下面的示例:
parameters:
  buildArtifactName: "build"
  solution: ""

jobs:
- job: 'BuildSolution'
  pool:
    vmImage: ${{parameters.vmImage}}
    continueOnError: false
  variables:
    artifactName: ${{ parameters.buildArtifactName}}
  steps:
    - task: NuGetCommand@2
      displayName: 'Restore NuGet packages'
      inputs:
        restoreSolution: ${{ parameters.solutionDir }}/${{ parameters.solution }}
        configuration: ${{parameters.buildConfiguration}}

谢谢!它有效。我一直在考虑取消上次提交并继续使用我的先前模板文件,但是你的答案解开了我的困境。非常抱歉犯了这么大的错误,我还是Azure DevOps的新手。 - Vianney Doleans

3

在Azure Pipelines中,您可以引用四种模板:阶段(Stage)、作业(Job)、步骤(Step)和变量(Variable)。

以下是来自“模板引用”文档的示例(我稍微修改了一下注释):

# File: azure-pipelines.yml which references another YAML file (test.yml)

stages:
- template: stages/test.yml  # Template reference
  parameters:
    name: Mini
    testFile: tests/miniSuite.js

- template: stages/test.yml  # Template reference
  parameters:
    name: Full
    testFile: tests/fullSuite.js

测试.yml文件内容如下:

# File: test.yml file to be referenced by the azure-pipelines.yml file

parameters:
  name: ''
  testFile: ''

stages:
- stage: Test_${{ parameters.name }}
  jobs:
  - job: ${{ parameters.name }}_Windows
    pool:
      vmImage: vs2017-win2016
    steps:
    - script: npm install
    - script: npm test -- --file=${{ parameters.testFile }}
  - job: ${{ parameters.name }}_Mac
    pool:
      vmImage: macos-10.13
    steps:
    - script: npm install
    - script: npm test -- --file=${{ parameters.testFile }}

我最初也做了类似的事情,但现在我正在尝试将我的第一个模板拆分成小的作业模板,而无需更改我的管道,因为这会增加我必须实现的每个管道中的行数。通过拆分我所意识到的第一个模板,我可以在未来重复使用作业。我编辑了我的帖子以添加一些代码。 - Vianney Doleans
@VianneyDoleans 哦,好的。在你添加额外细节之前,我已经写了这个答案。看起来Levi Lu-MSFT已经回答了你的问题。 - Ben Smith
1
是的,他的回答解决了我的问题。很抱歉我在第一篇帖子中没有添加代码,如果需要重新发布,下次我会添加的。 - Vianney Doleans

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