Search code examples
azurepowershellazure-devopsyamlazure-devops-pipelines

How can I use a variable of one yaml in another yaml using powershell?


As I am new to yaml and powershell,Can someone please help me in understanding the way of using a variable of one Yaml in another one?

Example Scenario:

I have variable- $a in abc.yml under the folder-Test1,now I have to use the same $a in bcd.yaml under the folder -Test2.

How can I achieve this in powershell?


Solution

  • To pass params between yml files in Azure DevOps you have to specify a path to the template (the file you want to pass the param across to) and give the parameter a value. Then in the second file also declare the parameter.

    To make use of the param it in the second yaml you can use the ${{parameters.name}} construction:

    Let's assume $a represents a directory path...

    Example contents of abc.yml:

    stages:
    - stage: 'Passing param example'
      jobs:
      - template: ${{variables['System.DefaultWorkingDirectory']}}/bcd.yml
        parameters:
          param1_butuseanynameyoulike: $a
    

    The in the bcd.yml:

    parameters:
      - name: 'param1_butuseanynameyoulike'
        displayName: 'Parameter passed from original yaml file'
        type: string
        default: ''
    
    jobs:
      - job: somename
    
        steps:
            - task: PowerShell@2
              inputs:
                targetType: 'inline'
                script: |
                  Get-ChildItem -Path ${{ parameters.param1_butuseanynameyoulike }}"