How do I make the - one
and - two
work in parallel?
parameters:
- name: listOfStrings
type: object
default:
- one
- two
steps:
- ${{ each value in parameters.listOfStrings }}:
- script: echo ${{ value }}
How do I make the - one
and - two
work in parallel?
parameters:
- name: listOfStrings
type: object
default:
- one
- two
steps:
- ${{ each value in parameters.listOfStrings }}:
- script: echo ${{ value }}
There is no way to run tasks in parallel now. You can vote for it here: https://developercommunity.visualstudio.com/t/ability-to-run-tasks-in-parallel/365659
But you can have parallel jobs, that might be good enough solution in some cases:
parameters:
- name: listOfStrings
type: object
default:
- one
- two
jobs:
- ${{ each value in parameters.listOfStrings }}:
- job:
steps:
- checkout: none
- script: echo ${{ value }}
For Microsoft hosted agents this feature can require extra payment: https://learn.microsoft.com/en-us/azure/devops/pipelines/licensing/concurrent-jobs?view=azure-devops&tabs=ms-hosted
To run the jobs in parallel in your Devops pipeline, you can refer this article.
Syntax:
jobs:
- job: Test
strategy:
parallel: # parallel strategy
matrix: # matrix strategy
maxParallel: 2 # maximum number simultaneous matrix legs to run
# note: `parallel` and `matrix` are mutually exclusive
# you may specify one or the other; including both is an error
# `maxParallel` is only valid with `matrix`
I am providing you the below sample if that helps:
parameters:
- name: listOfStrings
type: object
default:
- one
- two
steps:
- script: echo "Starting pipeline"
- strategy:
matrix:
listOfStrings: ${{ parameters.listOfStrings }}
maxParallel: 2
steps:
- script: echo ${{ matrix.listOfStrings }}
- script: echo "Pipeline finished"
When pipeline is executed, the Azure Pipelines will create separate agent for each job in the matrix and run jobs in parallel, up to maximum number specified by maxParallel. Therefore, your "one" and "two" jobs will be executed in parallel.
If you want to do the parallel testing for your jobs refer this article. For the pricing of parallel jobs refer this. Hope this helps.
As others have mentioned, Azure Pipelines doesn't offer a way to run steps in parallel within the same job. The unit of parallelization is the job. Others have already provided examples on how to spin up multiple jobs using the matrix strategy and the yaml-transformation using ${{ each value in ... }}
.
But PowerShell and other scripting hosts can run code in parallel, there is nothing preventing you from processing things in parallel in a single powershell step or to run code in parallel in a custom task:
- steps
- powershell: |
$strings = ConvertFrom-Json -raw -value $env:listOfStrings
$strings | ForEach-Object -Parallel {
$value = $_
}
env:
listOfStrings = ${{ convertToJson(parameters.listOfStrings) }}