简体   繁体   English

如何将此重复的 Azure DevOps 步骤拆分为可重用模板

[英]How to split this duplicated Azure DevOps steps into a reusable template

I have the following Azure DevOps template, which is currently hardcoded to some specific files.我有以下 Azure DevOps 模板,目前已硬编码为某些特定文件。 I'm hoping to refactor it into a simpler template using loops so I don't need to duplicate (and hardcode) specific files.我希望使用循环将其重构为更简单的模板,因此我不需要复制(和硬编码)特定文件。

[ pseduo code for brevity ] [为简洁起见的伪代码]

#tests.yml

steps:

  # pre-test steps. This is done once.

  - script: install tool1
  - script: install tool2
  - script: setup path stuff.

  # Tests
  - script: test project1 |
            display codecoverage to console-out
  - script: test project 2 |
            display codecoverage to console-out

  # Post-tests
  - script: upload coverage report for project1
  - script: upload coverage report for project2
  - task: PublishTestResults@2 # publish test results to Azure DevOps.

So notice how i'm doing this per project所以请注意我是如何为每个项目做的

  • test project测试项目
  • display codecoverage result to console-out (so i can see the results here)将代码覆盖结果显示到控制台输出(所以我可以在这里看到结果)
  • upload result to 3rd party website (for others to see also)将结果上传到第三方网站(供其他人查看)

I was hoping there might be a way I can someone pass in the info into the template and then just loop through the array of input data.我希望有一种方法可以让某人将信息传递到模板中,然后循环遍历输入数据数组。

like this..像这样..

steps:

  # pre tests.
  ...

  # tests
  foreach project in projects
    - script: test project |
              display code coverage report
    - script: upload report

  # post-test
  ...

can this be done in Azure DevOps?这可以在 Azure DevOps 中完成吗?

    - -

Personally, maybe the keywords each is what you are looking for?就个人而言,也许each关键字都是您正在寻找的?

For sample:样品:

azure-pipelines.yml azure-pipelines.yml

extends:
  template: template.yml
  parameters:
    buildArgs:  
      Arg1 : $(arg1-value)
      Arg2 : $(arg2-value)

template.yml模板.yml

parameters:
- name: buildArgs 
  type: object
  default: [] 
stages:
  - stage: EachLoop
    displayName: Run Each extends
    jobs:
    - job: looping
      steps:
      - ${{ each arg in parameters.buildArgs }}:
        - bash: |
            echo ${{ arg.key }}
            echo ${{ arg.value }}
            echo "##vso[task.setvariable variable=buildOther]${{ arg.value }}"
          displayName: ${{ arg.key }}
        - bash: |
            echo "buildstring=$(buildOther)"
          displayName: ECHO-${{ arg.key }}

在此处输入图像描述


For your scenario, just need put run test / display / upload steps in template.yml.对于您的场景,只需将run test / display / upload步骤放在 template.yml 中。 Use each along with parameters to extend the template, so that you can achieve looping.使用eachparameters来扩展模板,这样就可以实现循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM