【发布时间】:2020-03-01 02:21:27
【问题描述】:
是否可以将 yaml 模板嵌套在另一个 yaml 模板中?
我在不同的 Git 存储库中有多个 NuGet 项目,我正在尝试在 nuget.org 上对发布 NuGet 的过程进行模板化。
所以我创建了一个名为“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 模板存储库):
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
(...)
编辑:我添加了部分代码。
【问题讨论】:
-
是的,您能展示您的模板以及如何称呼它们吗?
-
如错误所示,您的模板中可能存在错误。能否请您发布您的模板?
-
是的,当然。我编辑了我的帖子。
-
问题已解决。所以是的,可以将 yaml 模板嵌套在其他 yaml 模板中,我的错误是缩进。感谢您的回复!
标签: azure-devops yaml azure-pipelines