3

我有两个模板 (build-tsmain-project.ymlbuild-single-project.yml),我从另一个模板中调用它们,如下所示:

steps:
  - ${{ each project in parameters.projects }}:
      - ${{ if ne(project.disable, 'true') }}:
          - ${{ if eq(project.name, 'TSMain') }}:
              - template: build-tsmain-project.yml
                parameters:
                  name: ${{ project.name }}
                  shortName: ${{ project.shortName }}
                  path: ${{ project.name }}.sln
                  moreBuildArgs: ${{ parameters.restoreArgs }} ${{ parameters.moreBuildArgs }} ${{ project.moreBuildArgs }}
                  testFilter: ${{ project.testFilter }}
                  ${{ if parameters.withCoverage }}:
                    moreTestArgs: ${{ parameters.coverageArgs }}
                    publishTestResults: false
                  noTest: ${{ project.noTest }}
          - ${{ if ne(project.name, 'TSMain') }}:
              - template: build-single-project.yml
                parameters:
                  name: ${{ project.name }}
                  shortName: ${{ project.shortName }}
                  path: ${{ project.name }}.sln
                  moreBuildArgs: ${{ parameters.restoreArgs }} ${{ parameters.moreBuildArgs }} ${{ project.moreBuildArgs }}
                  testFilter: ${{ project.testFilter }}
                  ${{ if parameters.withCoverage }}:
                    moreTestArgs: ${{ parameters.coverageArgs }}
                    publishTestResults: false
                  noTest: ${{ project.noTest }}

这是一个问题,因为我复制了参数 - 而不是 DRY。

我能找到的唯一可行的 DRY 解决方案涉及引入一个新模板 ( build-single-project-or-tsmain.yml):

parameters:
  - name: template_name
    type: string
  - name: template_parameters
    type: object

steps:
  - template: ${{ parameters.template_name }}
    parameters: ${{ parameters.template_parameters }}

这使我可以像这样重写原始代码:

steps:
  - ${{ each project in parameters.projects }}:
      - ${{ if ne(project.disable, 'true') }}:
          - template: build-single-project-or-tsmain.yml
            parameters:
              ${{ if eq(project.name, 'TSMain') }}:
                template_name: build-tsmain-project.yml
              ${{ if ne(project.name, 'TSMain') }}:
                template_name: build-single-project.yml
              template_parameters:
                name: ${{ project.name }}
                shortName: ${{ project.shortName }}
                path: ${{ project.name }}.sln
                moreBuildArgs: ${{ parameters.restoreArgs }} ${{ parameters.moreBuildArgs }} ${{ project.moreBuildArgs }}
                testFilter: ${{ project.testFilter }}
                ${{ if parameters.withCoverage }}:
                  moreTestArgs: ${{ parameters.coverageArgs }}
                  publishTestResults: false
                noTest: ${{ project.noTest }}

现在我想知道是否有办法避免引入新模板并且仍然有 DRY 解决方案?

我们使用 Azure DevOps Server 2020(本地)。

4

0 回答 0