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

时间:2019-11-04 18:16:09

标签: azure-devops yaml azure-pipelines

是否可以将Yaml模板嵌套在另一个Yaml模板中?

我在不同的Git存储库中有多个NuGet项目,并且我正在尝试对在nuget.org上发布NuGets的过程进行模板化。

因此,我创建了一个名为“ devops-templates”的git存储库,做了一个第一个yaml模板,请确保它可以工作,然后将其划分为4个yaml模板(构建解决方案,生成包,运行单元测试,发布),并进行引用将它们放入全局yaml模板。

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

/Net/Utilities/BuildSolution.yml@templates (Line: 33, Col: 18): A template expression is not allowed in this context,/Net/Utilities/BuildSolution.yml@templates (Line: 36, Col: 21): A template expression is not allowed in this context,/Net/Utilities/BuildSolution.yml@templates (Line: 48, Col: 24): A template expression is not allowed in this context,/Net/Utilities/BuildSolution.yml@templates (Line: 53, Col: 28): A template expression is not allowed in this context,/Net/Utilities/BuildSolution.yml@templates (Line: 54, Col: 26): A template expression is not allowed in this context,/Net/Utilities/BuildSolution.yml@templates (Line: 59, Col: 21): A template expression is not allowed in this context,/Net/Utilities/BuildSolution.yml@templates (Line: 60, Col: 22): A template expression is not allowed in this context,/Net/Utilities/BuildSolution.yml@templates (Line: 61, Col: 32): A template expression is not allowed in this context,/Net/Utilities/BuildSolution.yml@templates (Line: 63, Col: 21): A template expression is not allowed in this context,/Net/Utilities/BuildSolution.yml@templates (Line: 64, Col: 26): A template expression is not allowed in this context

我在Microsoft文档https://docs.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模板存储库):

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 个答案:

答案 0 :(得分:1)

您可以在Azure管道中引用四种模板:阶段,作业,步骤和变量。

从“ Template References”文档中摘录的示例(我的评论略有修改)如下:

# 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

test.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 }}

答案 1 :(得分:0)

您的BuildSolution.yml中似乎存在缩进错误。 参数和作业应具有相同的缩进。请参阅以下内容:

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}}

答案 2 :(得分:0)

制作复杂的Yaml模板时,将内置宏扩展转换为专用宏语言有时是个好主意。

例如,ERB在模板中为您提供了Ruby的全部功能。

SMX和yml友好的软件包为您提供了Python的所有功能。

这样,您就不受范围限制,并且可以使用函数,类,循环等之类的东西。

您可以使用预提交钩子和短绒来确保yaml已从模板派生。

有关最简单的预提交框架,请参见http://pre-commit.com