The QA team requested the ability to run pipeline jobs in a loop with a parameter that would specify the number of iterations.
My solution:
trigger:
- master
parameters:
- name: myarray
type: object
default:
- '1'
- '2'
- '3'
- '4'
- '5'
- name: counter
type: string
default: 3
pool:
name: AWS_DEV_Linux
jobs:
- ${{ each value in parameters.myarray }}:
- ${{ if ge(parameters.counter, value) }}:
- job: job_${{ value }}
steps:
- script: echo ${{ value }}
The output:
The problems of my solution:
- Is expected to have more than 20 iterations (in my example only 5)
- The array can be modified when someone run the pipeline manually and that can broke the pipeline (like in image)
An array as a variable could solve a part of this but is not allowed for my version (Version Azure DevOps Server 2022.2)
Question: How to create a loop more elegant and safe?
The QA team requested the ability to run pipeline jobs in a loop with a parameter that would specify the number of iterations.
My solution:
trigger:
- master
parameters:
- name: myarray
type: object
default:
- '1'
- '2'
- '3'
- '4'
- '5'
- name: counter
type: string
default: 3
pool:
name: AWS_DEV_Linux
jobs:
- ${{ each value in parameters.myarray }}:
- ${{ if ge(parameters.counter, value) }}:
- job: job_${{ value }}
steps:
- script: echo ${{ value }}
The output:
The problems of my solution:
- Is expected to have more than 20 iterations (in my example only 5)
- The array can be modified when someone run the pipeline manually and that can broke the pipeline (like in image)
An array as a variable could solve a part of this but is not allowed for my version (Version Azure DevOps Server 2022.2)
https://developercommunity.visualstudio/t/variables-in-yaml-pipeline-are-not-allowing-to-def/812728
Question: How to create a loop more elegant and safe?
Share asked Mar 7 at 14:08 MacLeonMacLeon 11 1- Hi @MacLeon, please check whether the answers below are helpful for you. If they don't work, provide more details so that others can help you. – Ziyang Liu-MSFT Commented Mar 25 at 6:20
2 Answers
Reset to default 1I don't fully understand the intention behind your example, but if you convert the jobs
section into a template
, you could iterate over an array that isn't sourced from a pipeline parameter, as shown in the example below:
# example-pipeline.yaml
parameters:
- name: counter
type: number
default: 3
jobs:
- template: example-template.yaml
parameters:
myarray: [1,2,3,4,5]
counter: ${{ parameters.counter }}
# example-template.yaml
parameters:
- name: myarray
type: object
- name: counter
type: number
jobs:
- ${{ each value in parameters.myarray }}:
- ${{ if ge(parameters.counter, value) }}:
- job: job_${{ value }}
steps:
- script: echo ${{ value }}
Run with counter 3
Run with counter 7
Hope this helps! Good luck.
Edit: Changed your parameter type of counter
to type number
(integer) otherwise it would parse incorrectly for 2-digit values >10
with the ge
expression
If the elements for myarray
are fixed and not changed often, you can set myarray
as a static variable in the YAML pipeline, and its value is a string which is a comma-separated list of the elements.
And then you can use the split
function to the variable value (a string) to be into substrings based on the comma character (,
). This function returns an array of the substrings. You can loop the array to access each substring.
See below example as reference:
parameters:
- name: counter
type: string
default: '3'
variables:
myArray: '1,2,3,4,5'
jobs:
- ${{ each value in split(variables.myArray, ',') }}:
- ${{ if ge(parameters.counter, value) }}:
- job: job_${{ value }}
steps:
- script: echo ${{ value }}
With above configuration, when users manually run the pipeline, they can only change the value of counter
parameter, can cannot change the value of myArray
. If you want to change value of myArray
, you need to edit in the pipeline YAML file.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744924718a4601360.html
评论列表(0条)