【问题标题】:Use variables in Azure DevOps Pipeline templates在 Azure DevOps Pipeline 模板中使用变量
【发布时间】:2021-01-03 03:34:21
【问题描述】:

我们有一组 Azure DevOps 管道模板,我们可以在多个存储库中重复使用这些模板。因此,我们希望有一个包含所有模板变量的文件。

repo 结构如下所示

template repo
  ├── template-1.yml
  ├── template-2.yml
  └── variables.yml

project repo
  ├── ...
  └── azure-pipelines.yml

variables.yml 看起来像这样

...
variables:
  foo: bar

template-1.yml 中,我们正在导入variables.yml,如here 中所述

variables:
- template: variables.yml

azure-pipelines.yml 中我们使用这样的模板

resources:
  repositories:
    - repository: build-scripts
      type: git
      name: project-name/build-scripts

steps:
  ...
  - template: template-1.yml@build-scripts
    

当我们现在尝试运行管道时,我们会收到以下错误消息:

template-1.yml@build-scripts (Line: 10, Col: 1): Unexpected value 'variables'

【问题讨论】:

    标签: azure azure-devops yaml azure-pipelines


    【解决方案1】:

    问题是因为您在步骤范围内使用了变量模板。而variables 在那个级别根本不存在。这应该适合你:

    resources:
      repositories:
        - repository: build-scripts
          type: git
          name: project-name/build-scripts
    
    variables:
      - template: template-1.yml@build-scripts
    
    steps:
      ...
    

    这可以在任何可以使用变量的地方使用。因此,例如,您可以以这种方式使用它:

    jobs:
    - job: myJob
      timeoutInMinutes: 10
      variables:
      - template: template-1.yml  # Template reference
      pool:
        vmImage: 'ubuntu-16.04'
      steps:
      - script: echo My favorite vegetable is ${{ variables.favoriteVeggie }}.
    

    【讨论】:

    • 不幸的是它不起作用。我需要步骤模板中的变量,因为我想在另一个管道的 job.steps 部分中包含步骤。所以要求是我可以访问这样一个步骤模板中的变量,这些变量是在变量模板中定义的。
    • 您可以在变量可用的任何级别使用变量模板。我的编辑对你有帮助吗?
    【解决方案2】:

    这种方法可以帮助到别人,所以我决定把它贴在这里。

    对于这种情况还有另一种“解决方法”,可能有点“肮脏”,因为您需要在每次执行模板时明确指定参数,如果您有很多参数要传递,这不是一个好主意。 (其实是有办法的,看下面的改进版)但是,如果你真的想要或者你没有那么多参数,这应该可以:

    逻辑是:变量模板中包含所有参数,例如templates/vars.yml

    variables:
      - name: myVar1
        value: MyValue1
      - name: myVar2
        value: MyValue2
    

    并且由于您在变量中拥有所需的一切,因此可能不需要将变量导入模板本身,因为模板将在管道中执行,这将导入您的变量并且您可以显式替换它,就像在下面的例子:

    templates/my-template-setup-env.yml的内容(里面没有变量):

    steps:
      - script: |
          echo "$(myVar3)"
    

    my-azure-pipeline.yml的内容(带导入变量模板):

    name: my-cute-CI-name
    
    trigger:
    - main
    
    pool:
      vmImage: ubuntu-18.04
      
    variables:
      - template: templates/vars.yml # importing your variables from templates
    
        stages:
          - stage: RunTheStage
            displayName: 'Run first stage'
            jobs:
            - job: RunTheJob
              displayName: 'Run your job'
              steps:
                - template: templates/my-template-setup-env.yml # your template
                   parameters:
                     myVar3: $(myVar1) # substitute value from vars.yml, so myVar1 will be used in templated and printed
    

    改进版

    但是,如果您在所有管道和模板中对参数和变量进行唯一命名,则在模板使用期间不明确指定它是安全的,这也可以:

    my-azure-pipeline.yml 的编辑和缩短版本(如果您在模板中具有相同的变量和参数名称):

    variables:
      - template: templates/vars.yml
    ...
          steps:
            - template: templates/my-template-setup-env.yml # env preparation
              # parameters:
              #   myVar2: $(myVar2) # you don't need to pass any parameters explicitly to the template since you have same name of variable
    

    templates/my-template-setup-env.yml 那么应该是这样的:

    steps:
      - script: |
          echo "$(myVar2)" # not myVar3, since myVar3 is not in initial variables file templates/vars.yml
    

    或者您还需要将剩余的变量(在我们的第一种情况下为 myVar3)添加到 templates/vars.yml 文件中。

    【讨论】:

      【解决方案3】:

      如果你的模板文件只有variables,可以参考Krzysztof Madej的回答。

      如果你的模板文件同时有variablessteps如下图,则只能被extends使用。

      # File: template-1.yml
      variables: ...
      
      steps: ...
      

      也可以分阶段写,如下图。

      # File: template-1.yml
      stages:
      - stage: {stage}
        variables: ...
        jobs:
        - job: {job}
          steps: ...
      

      然后将其作为单独的阶段插入。

      # azure-pipelines.yml
      stages:
      - stage: ...
      - template: template-1.yml
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-10
        • 2021-02-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多