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

徘徊边缘 提交于 2021-01-28 09:10:30

问题


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.


回答1:


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.




回答2:


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.

  • Use a template parameter as part of a condition

  • Use the output variable from a job in a condition in a subsequent job

  • Or Use some predefined variables:

    condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
    

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


来源:https://stackoverflow.com/questions/61947257/is-it-possible-to-create-additional-pipeline-steps-based-on-a-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!