[英]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所以请注意我是如何为每个项目做的
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.使用
each
和parameters
来扩展模板,这样就可以实现循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.