【问题标题】:Azure yaml pipeline using "extends"使用“扩展”的 Azure yaml 管道
【发布时间】:2020-10-05 02:38:45
【问题描述】:

我正在尝试使用扩展作为我管道的一部分。我正在尝试从 Azure docs 开始工作的第一个基本步骤,即

    # 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

我不断收到以下错误:

在此上下文中不允许使用指令“每个”。嵌入在字符串中的表达式不支持指令。仅当整个值是表达式时才支持指令 意外值'${{ 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)

【问题讨论】:

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


    【解决方案1】:

    更新

    请参考此直流链接中的回复--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 }}
      
    

    根据错误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 }}'

    您可能使用了错误的格式。关于格式,您可以在此处参考我们的官方文档—— 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
    

    【讨论】:

    • 不,我没有合并两个 yml 文件。 pythonparameter-template.yml 在一个单独的仓库中。
    • 当我尝试扩展 start.yml 时,使用您发送的链接 (docs.microsoft.com/en-us/azure/devops/pipelines/process/…) 我收到一个错误,即 Unexpected value 'CmdLine@2' 但至少 foreach 迭代正在发生。当我尝试将 @ 符号转义为 CmdLine\@2 时,验证通过但 if 条件 ${{ if ne(pair.value, 'CmdLine@2') }} 不满足。所以我不确定逃跑是否也有效。
    • 您好 BP_QA,此处无需使用 \,这应该是文档问题。这只是一个示例如何用于每次迭代和扩展。您也可以只使用每个表达式,但是您使用了错误的格式。每个的详细用法,你可以参考这个博客:mattvsts.github.io/2019/05/04/…stackoverflow.com/questions/59387555/…。希望这会有所帮助。
    • 问题不在于循环,我可以轻松地遍历 azure-pipeline.yml 中的所有步骤,如下所示。但问题是当我们想要跳过如下键时 - ${{ parameters.buildSteps 中的每个步骤}}: - ${{ 步骤中的每个对}}:${{ if ne(pair.value, 'CmdLine@ 2') }}: ${{ pair.key }}: ${{ pair.value }} ${{ if eq(pair.value, 'CmdLine@2') }}: '${{ pair.value } }': 错误或者如果我想根据它的键跳过一个步骤,说它是否是一个脚本,即键 = 脚本。
    • 我为此提出了 Microsoft 开发人员社区票,他们承认他们能够重现该问题。票是developercommunity.visualstudio.com/content/problem/1080415/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    • 2020-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-23
    • 1970-01-01
    相关资源
    最近更新 更多