Home > Enterprise >  How to convert string variable to object and pass it to another pipeline?
How to convert string variable to object and pass it to another pipeline?

Time:12-08

I have two Azure Devops pipelines: 'Starter' and 'Processing'. 'Starter' triggers 'Processing' and passes some parameters to it.

Starter:

trigger: none

pool:
  vmImage: 'windows-2019'

stages:
- stage: A
  jobs:
- template: Processing.yml
    parameters:
      products: $(Products)
      creds: $(Creds)

Processing:

parameters:
  - name: products
    type: object
    default: []
  - name: creds
    default: ''

jobs:
- ${{ each product in parameters.products }}:
    - task: PowerShell@2
      displayName: Importing ${{ product }} solution
      inputs:
        targetType: 'inline'
        script: |
          #Code

Key detail here is opportunity to loop through 'products' variable (each product in parameters.products), which must be setted in 'Starter' variables:

enter image description here

In other words, starting my pipeline I must pass list of products as 'string' and then loop through this list in second pipeline. 'Is it generally possible? Maybe products should be another type? I tried some work around but didn't get appropriate solution:

- job: Prepare_Products_Array
    steps:
      - task: PowerShell@2
        inputs:
          targetType: 'inline'
          script: |
            # Write your PowerShell commands here.
            $productsArray = []
            $productsArray = $(Products)
            $productsArray = $productsArray.Split(',')
            Write-Host ("##vso[task.setvariable variable=productsArray;]$productsArray")


- template: Processing.yml
    parameters:
      products: $env:productsArray

Exception: enter image description here

CodePudding user response:

From your yaml sample, you are defining the variable in YAML Pipeline UI and using parameters in YAML Template.

In this case, the variables defined on the UI will be assigned at runtime, but the parameters and expressions in the YAML template will be expanded at compile time.

Therefore, YAML UI variables cannot be passed to the Pipeline YAML Template.

And it will show the error:

Expected a...... Actual value $(Product)

This means that the pipeline variable not expanded at compile time.

I am afraid that there is no such method can pass the UI Pipeline variable to YAML Template.

Here are the workarounds:

Method 1 : You can use parameters in Starter yaml to pass the Object type parameters to YAML template.

Starter:

trigger: none
parameters:
  - name: products
    type: object
    default: []
  - name: creds
    default: ''

pool:
  vmImage: 'windows-2019'
 

stages:
- stage: A
  jobs:
  - template: Processing.yml
    parameters:
      products: ${{ parameters.products }}
      creds: ${{ parameters.creds }}

Processing:

parameters:
  - name: products
    type: object
    default: []
  - name: creds
    default: ''

jobs:
 - job: test
   steps:
   - ${{ each product in parameters.products }}:
     - task: PowerShell@2
       displayName: Importing ${{ product }} solution
       inputs:
        targetType: 'inline'
        script: |
          echo ${{ product }} 

Result: You can input the value when you run the pipeline.

enter image description here

Method2: You need to define the variable in Starter pipeline and change the products parameters as String type. Then you can use the expression - ${{ each product in split(parameters.products, ',')}}: to split the string.

Starter:

pool:
  vmImage: 'windows-2019'
 

variables:
  products: '1,2,3'
  creds: test
stages:
- stage: A
  jobs:
  - template: Processing.yml
    parameters:
      products: ${{ variables.products }}
      creds: ${{ variables.creds }}

Processing:

parameters:
  - name: products
    type: string
    default: ''
  - name: creds
    default: ''

jobs:
 - job: test
   steps:
   - ${{ each product in split(parameters.products, ',')}}:
     - task: PowerShell@2
       displayName: Importing ${{ product }} solution
       inputs:
        targetType: 'inline'
        script: |
          echo ${{ product }}
  • Related