1

How to show all the parameter's names as comma-separated strings (concatenated) and assign them to One Variable in YAML Azure so that i can use this variable in several places

i tried using

parameters:  
  - name: Var1
    displayName: Testing
    type: string
  - name: Var2
    displayName: Coding
    type: string
  - name: Var3
    displayName: Marketing
    type: string

variables:
   - name: allParametersString
     ${{each item in parameters}}:
     value: $allParametersString + ','+ item.displayName
   

my desired output is upon using $allParametersString I should get Testing,Coding,Marketing

but this is leaving an error mentioning 'value' is already defined so can anyone help me? regarding this, I am searching for a solution for 2 weeks :(

ksk
  • 165
  • 14

2 Answers2

2

I found the way of using bash to assign values will work for this i did

    variables:
   - name: allParametersString
     value: ' '

   steps:
   - ${{ each parameter in parameters }}:
      # the below code will help us reassign the values to variables with bash so i am appending all parameters separated by comma
     - bash: echo '##vso[task.setvariable variable=allParametersString]$(allParametersString)${{ parameter.Key }}, '
                   
  - script:
      echo 'printing all parameters names separated by comma .->$(allParametersString)' 

Please let me know if I can improve it more this helps me understand that in order to reassign or assign twice or concatenate the string using YAML this is the one way of doing it

ksk
  • 165
  • 14
  • 1
    Cool and good question, there is only one place to pay attention, your variable seems only for same job situation. Use `isoutput=true` to make it visible for all tasks. :) – Bowman Zhu-MSFT Sep 21 '22 at 09:26
  • Check this: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/set-variables-scripts?view=azure-devops&tabs=bash#levels-of-output-variables – Bowman Zhu-MSFT Sep 21 '22 at 09:27
1

Your current thinking is not feasible.

There are several things that bind you.

1, The first is the processing logic of yml expression in DevOps.

See this official document:

https://learn.microsoft.com/en-us/azure/devops/pipelines/process/runs?view=azure-devops#process-the-pipeline

From the first sentence given, we know your yml will be expanded like this:

parameters:  
  - name: Var1
    displayName: Testing
    type: string
    default: value1
  - name: Var2
    displayName: Coding
    type: string
    default: value2
  - name: Var3
    displayName: Marketing
    type: string
    default: value3

variables:
   - name: allParametersString
     value: xxx
     value: xxx
     value: xxx

variable of yml concept doesn't allow the written method of the above. That's why you encountered error 'value' is already defined.

2, The second is the structure allowed by DevOps yml files.

Every section of yml definition has limited predefined key. This means that compile time cannot find other container to store the variable.

3, I am afraid the usage of yml expression does not support you to do so.

Refer to this:

Each Keyword This tell you the standard usage of 'each':

You can use the each keyword to loop through parameters with the object type.

JOIN Expression

This is the way the DevOps yml pipeline provides compile-time flattening of data, but it doesn't work in your case.

And there's no such thing as an index or subscript to navigate to the last run.


4, By the way, the item object doesn't have such information 'displayName':

trigger:
- none
parameters:  
  - name: Var1
    displayName: Testing
    type: string
    default: value1
  - name: Var2
    displayName: Coding
    type: string
    default: value2
  - name: Var3
    displayName: Marketing
    type: string
    default: value3

steps:
- ${{each item in parameters}}:
  - task: PowerShell@2
    inputs:
      targetType: 'inline'
      script: |
        # Write your PowerShell commands here.
        
        Write-Host "${{ convertToJson(item) }}"

Result:

enter image description here


The DevOps yml pipeline has no built-in features to implement your needs. If you have to do this, a feasible method is to call the API to get the content of the yml file, then parse and get the parameter part (this is actually a self-designed parser), and then combine the acquired parameters into the variable in the script And pass logging command set variable(isoutput=true). In this way, other tasks can use this combined variable.

This can be done, but is overly complicated and you need to consider whether it is necessary to do such a thing.

Bowman Zhu-MSFT
  • 4,776
  • 1
  • 9
  • 10
  • 1
    @Bowmen thanks for the explanation I just now i figured it out that we can use bash and do this I will post the answer and please suggest if you think even we can do it in a better way thank you – ksk Sep 21 '22 at 09:08
  • @ksk Sure, looking forward to your sharing. :) – Bowman Zhu-MSFT Sep 21 '22 at 09:14
  • 1
    Posted answer please help me with your opinion :) @Bowman i really appreciate for your time in looking into it, you rock – ksk Sep 21 '22 at 09:16