问题
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 ?
-
-
回答1:
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.
来源:https://stackoverflow.com/questions/61081717/how-to-split-this-duplicated-azure-devops-steps-into-a-reusable-template