如何将此重复的Azure DevOps步骤拆分为可重复使用的模板 [英] How to split this duplicated Azure DevOps steps into a reusable template
问题描述
我有以下Azure DevOps模板,当前已硬编码到某些特定文件中.我希望使用循环将其重构为一个更简单的模板,这样就不必复制(和硬编码)特定的文件.
[简短的pseduo代码]
#tests.yml脚步:#预测试步骤.只需完成一次.-脚本:安装工具1-脚本:安装工具2-脚本:设置路径的内容.#测试-脚本:测试project1 |显示代码覆盖率以进行控制台输出-脚本:测试项目2 |显示代码覆盖率以进行控制台输出#后期测试-脚本:上传project1的覆盖率报告-脚本:上传project2的覆盖率报告-任务:PublishTestResults @ 2#将测试结果发布到Azure DevOps.
所以请注意我是如何在每个项目中执行
- 测试项目
- 将代码覆盖率结果显示出来(以便在这里看到结果)
- 将结果上传到第三方网站(其他人也可以查看)
我希望有一种方法可以使某人将信息传递到模板中,然后循环遍历输入数据数组.
像这样..
步骤:#预先测试....#测试项目中的foreach项目-脚本:测试项目|显示代码覆盖率报告-脚本:上传报告#后测...
这可以在Azure DevOps中完成吗?
- --
个人而言,也许是关键字
对于您的情况,只需在template.yml中放入 run test
/ display
/ upload
步骤.结合使用 each
和 parameters
扩展模板,以便可以实现循环.
I have the following Azure DevOps template, which is currently hardcoded to some specific files. 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 ?
-
-
Personally, maybe the keywords each
is what you are looking for?
For sample:
azure-pipelines.yml
extends:
template: template.yml
parameters:
buildArgs:
Arg1 : $(arg1-value)
Arg2 : $(arg2-value)
template.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. Use each
along with parameters
to extend the template, so that you can achieve looping.
这篇关于如何将此重复的Azure DevOps步骤拆分为可重复使用的模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!