Archimedes Trajano
Archimedes Trajano

Reputation: 41630

Is it possible to create additional pipeline steps based on a variable?

Is it possible in Azure Devops YAML pipelines to dynamically create additional steps based on some variable data (without creating our own plugin)

The thing is I see that I want to iterate through several directories, but I don't want to just lump it all in a single step since it makes it harder to scan through to find an error.

Upvotes: 3

Views: 2452

Answers (2)

LoLance
LoLance

Reputation: 28216

Is it possible in Azure Devops YAML pipelines to dynamically create additional steps based on some variable data (without creating our own plugin)

No, Yaml pipelines(azure-pipeline.yml) are under Version Control. So what you want (for your original title) is to dynamically commit changes to the azure-pipeline.yml file when executing the pipeline. That's not a recommended workflow.

1.Instead you can consider using Azure Devops Conditions to dynamically enable/disable the additional steps.

2.If you're not using Conditions, you can check conditional template as Simon suggests above.

Also, both #1 and #2 can work with new feature runtime parameters.

3.However, if the dynamic variable you mean comes from the result of components = result of ls -1 $(Pipeline.Workspace)/components command, above tips won't work for this situation. For this you can try something like this:

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      # Write your PowerShell commands here.
      # some logic to run `components = result of ls -1 $(Pipeline.Workspace)/components` and determine whether to set the WhetherToRun=true.

      'Write-Host "##vso[task.setvariable variable=WhetherToRun]True"'

- task: CmdLine@2
  inputs:
    script: |
      echo Hello world
  condition: eq(variables['WhetherToRun'], 'True')

Upvotes: 2

Simon Ness
Simon Ness

Reputation: 2550

It is possible to include steps conditionally with an if statement.

I think the example of extending a template on the same page will give you a good indication of how to iterate through a list parameter and create / run a step based on each value.

Upvotes: 0

Related Questions