Sar
Sar

Reputation: 299

Azure DevOps pass InputObject from One step to another step

I am trying to pass a variable "$a" from one step to another step in Azure DevOps. The $hash contains Name,IsReadOnly and Length as NoteProperty. I was successful in passing the variable from one step to next step as $b, but when i try to access NoteProperty of $b variable i am not able to access it

1.My question is it possible pass variable from one step to another step, How do i access the variables NoteProperty ??

2. Does Azure DevOps support passing complex objects from one step to another step ??

Upvotes: 0

Views: 1760

Answers (1)

Bright Ran-MSFT
Bright Ran-MSFT

Reputation: 14029

For your first question:

is it possible pass variable from one step to another step, How do i access the variables NoteProperty ??

Sure, you can pass variables from a step to the subsequent steps in the same job, and you even can pass the variables to the steps in the subsequent jobs.

To pass a variable value generated in a step to other subsequent steps, you can use the 'SetVariable' command in the step that generates the value.

  • If you want to share the variable value only to the subsequent steps in the same job, you can just set it as a general job-level variable using the following command.

    Bash

    echo "##vso[task.setvariable variable=<var_name>;]<var_value>"
    

    or PowerShell

    Write-Host "##vso[task.setvariable variable=<var_name>;]<var_value>"
    

    With this way, in the subsequent steps, you can use the expression '$(var_name)' to access this variable.

    After executing above command to set up the variable, like as other general pipeline variables, this variable will be automatically mapped as an environment variable. So, you also can use the expression '%VAR_NAME%' (CMD), '$env:VAR_NAME' (PowerShell) or '$VAR_NAME' (Bash) to access the variable.

  • If you want to share the variable value to the subsequent steps in the same job and the steps in the subsequent jobs, you can set it as an output variable using the following command.

    Bash

    echo "##vso[task.setvariable variable=<var_name>;isoutput=true]<var_value>"
    

    or PowerShell

    Write-Host "##vso[task.setvariable variable=<var_name>;isoutput=true]<var_value>"
    

    With this way, to access this output variable in the subsequent steps of the same job, you can use the expression '$(step_name.var_name)'.

    To access this output variable in the steps of the subsequent jobs, the subsequent jobs need to depend on (dependsOn) the job that contains the step to set up the output variable. And you also need to map this output variable as a job-level variable on each subsequent job, and then use this job-level variable to access the output value.

To view more details about variables in Azure Pipelines, you can see "Define variables".

A simple example as reference.

  • azure-pipelines.yml

    jobs:
    - job: JobA
      displayName: 'Job A'
      steps:
      - bash: echo "##vso[task.setvariable variable=MyVar;isoutput=true]This is an output variable!"
        name: set_output
        displayName: 'Set up the output variable'
    
      - bash: echo "set_output.MyVar = $(set_output.MyVar)"
        displayName: 'Show the value of output variable'
    
    - job: JobB
      displayName: 'Job B'
      dependsOn: JobA
      variables:
        output_from_JobA: $[ dependencies.JobA.outputs['set_output.MyVar'] ]
      steps:
      - bash: echo "output_from_JobA = $(output_from_JobA)"
        displayName: 'Show the value of output variable from JobA'
    
  • Result: enter image description here

For your second question:

Does Azure DevOps support passing complex objects from one step to another step ??

Normally, the value of the variable in Azure Pipelines should be string. To pass the content of an object as a variable in the pipeline, you can try to convert the object (such as JSON node, XML node, etc.) to be a string, then like as above example, pass this string as a variable. In the subsequent steps that need to access this object, after receiving the string variable, you can try to convert the string back to the object. This is the most common method I can think of.

Upvotes: 2

Related Questions