0

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 }}
jessehouwing
  • 106,458
  • 22
  • 256
  • 341
Rod
  • 14,529
  • 31
  • 118
  • 230

3 Answers3

3

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

jdfa
  • 659
  • 4
  • 11
  • If this is a stand alone template, it works fine. However, if I'm calling it from another template, it doesn't like "jobs". Any ideas? – Rod Mar 30 '23 at 18:58
  • There are different types of templates. Most probably you need to create job template, check this: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/templates?view=azure-devops#job-stage-and-step-templates-with-parameters – jdfa Mar 31 '23 at 15:15
1

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.

NaveenBaliga
  • 449
  • 1
  • 2
  • 5
1

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) }}

Similarly you can run things in parallel rom Bash as well.

jessehouwing
  • 106,458
  • 22
  • 256
  • 341