使用 "extends"的 Azure yaml 管道

标签 azure azure-devops continuous-integration yaml continuous-deployment

我正在尝试使用扩展作为我的管道的一部分。我正在尝试从 Azure 文档中迈出第一个基本步骤,即

    # pythonparameter-template.yml
    parameters:
    - name: usersteps
      type: stepList
      default: []
    steps:
    - ${{ each step in parameters.usersteps }}
      - ${{ step }}

# azure-pipelines.yml
trigger: none

resources:
  repositories:
  - repository: CI-CD-Templates
    type: git
    name: /CI-CD-Templates
    ref: refs/heads/master

extends:
  template: /pythonparameter-template.yml@CI-CD-Templates
  parameters:
    usersteps:
    - script: echo This is my first step
    - script: echo This is my second step

我不断收到以下错误:

The directive 'each' is not allowed in this context. Directives are not supported for expressions that are embedded within a string. Directives are only supported when the entire value is an expression Unexpected value '${{ each step in parameters.usersteps }} - ${{ step }}'

此外,在我从模板扩展之后,azure-pipelines.yml 也可以有它自己的自定义步骤,即

# azure-pipelines.yml
resources:
  repositories:
  - repository: templates
    type: git
    name: MyProject/MyTemplates
    ref: tags/v1

extends:
  template: template.yml@templates
  parameters:
    usersteps:
    - script: echo This is my first step
    - script: echo This is my second step
steps:
- template: /validation-template.yml@CI-CD-Templates
 parameters:
  commitMessage: $(commitMessage)

- template: /shared-template.yml@CI-CD-Templates
 parameters:
 buildArtifactDir: $(buildArtifactDir)

最佳答案

更新

请引用此DC链接中的回复--Yaml extends feature erroring out when looping through steps.

​YAML 在 #pythonparameter-template.yml 中有一个验证任务 注释掉这两行,您的 YAML 就会成功。此处显示的模板阻止使用任何任务。对于有特定安全要求的人来说,这可能是一个场景。

  ${{ if eq(pair.key, 'task') }}:
    '${{ pair.value }}': error
<小时/>

您是否尝试在同一个文件中组合两个 yml pythonparameter-template.yml azure-pipelines.yml ?不支持。

    parameters:
    - name: usersteps
      type: stepList
      default: []
    steps:
    - ${{ each step in parameters.usersteps }}
      - ${{ step }}
  

根据错误嵌入在字符串中的表达式不支持指令。仅当整个值是表达式时才支持指令意外值 '${{parameters.usersteps 中的每个步骤}} - ${{step }}'

您可能使用了错误的格式。关于格式可以引用我们的官方文档—— Template types & usage

此外,您还可以使 azure-pipelines.yml 也有自己的自定义步骤。但你需要将它们放在管道中的参数下,而不是像你使用的方式。

azure-pipelines.yml

trigger:
- master

extends:
  template: pythonparameter-template.yml
  parameters:
    buildSteps:  
      - bash: echo Test #Passes
        displayName: succeed
      - bash: echo "Test"
        displayName: succeed
      - script: echo "Script Test" 
        displayName: Fail

关于使用 "extends"的 Azure yaml 管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62392376/

相关文章:

azure - Azure FormRecognizer 的 postman 发布请求不起作用

Azure 网站应用程序见解 - 切换配置

Azure Powershell 作业抛出错误 : Operation returned an invalid status code 'Forbidden'

docker - 在 Dockerfile 中运行 npm run test?

c# - 验证解决方案中的 .csproj 文件

php - 从 git repo 自动化环境设置/更新的最佳方法?

azure - 限制 Azure Function App 中的 Azure 存储队列处理

azure - 带有 azureFile 的 Kubernetes 持久卷

azure - SqlPackage.exe 使用始终加密和 key 保管库部署 DACPAC

rest - 通过 REST API 将更新推送到 VSTS