azureazure-pipelinesazure-pipelines-release-pipelineazure-pipelines-tasksazure-yaml-pipelines

Access variable between pipeline jobs


I have a yaml which calls a template to perform a few tasks one of which is to deploy azure infrastructure and another is to run a powershell script to parse output variables.

How do I access these variables in a different job within the yaml?

jobs:
  - deployment: Dep1
    displayName: MyDep1
    pool:
      name:
    variables:
      //some variables
    strategy:
      runOnce:
        deploy:
          steps:
          - download: current
            artifact: drop
          - task: //ignore
           
          - template: mytemplatelocation
            parameters:
              //all template parameters that also runs powershell script to parse output variables

          - task: Powershell@2
            inputs:
              targetType: 'inline'
              script: 'Write-Host "$(myVariable)'

Powershell task is able to print the variable value

But I have another job in this pipeline as below:

- deployment: Dep2
    displayName: MyDep2
    dependsOn: [Dep1]
    pool:
      name: 
    variables: 
      actualVarToUse: $[ dependencies.Dep1.outputs['myVariable'] ]
      actualVarToUseDup: $[dependencies.Dep1.outputs['TaskNameFromTemplate.myVariable']]    
    strategy:
     runOnce:
       deploy:
        steps:
        - download: current
          artifact: check
        - script: echo $(actualVarToUse) //does not work
        - task: Powershell@2
          inputs:
            targetType: 'inline'
            script: 'Write-Host "$(actualVarToUse)"' //does not work
        - task: Powershell@2
          inputs:
            targetType: 'inline'
            script: 'Write-Host "$(actualVarToUseDup)"' //does not work
        - task: Powershell@2
          inputs:
            targetType: 'inline'
            script: 'Write-Host $[dependencies.Dep1.outputs[myVariable]]' //does not work

Please suggest?

Edit 1:

Template yml

parameters:
  - name: outputVariableName
    displayName: ''
    type: string
    default: 'outputvar'
  
steps:
- checkout: git://Scripts
  path: 'Scripts'
- task: AzureResourceManagerTemplateDeployment@3
  displayName: 'Deploy Azure Infra'
  inputs:
    azureResourceManagerConnection: 
    subscriptionId: 
    resourceGroupName: 
    location: 
    csmFile: 
    csmParametersFile: 
    deploymentMode: 
    deploymentOutputs: ${{ parameters.outputVariableName}}

- task: PowerShell@2
  name: 'Create_${{ parameters.outputVariableName}}'
  displayName: ''  
  inputs:
    targetType: 
    filePath: 
    arguments: 

Script which is called above


param(    
    [Parameter(Mandatory=$true, Position=0)][string]$ARMOutput
)

Add-Type -AssemblyName System.Web.Extensions
$JS = New-Object System.Web.Script.Serialization.JavaScriptSerializer

$data = $JS.DeserializeObject($ARMOutput)

$data.GetEnumerator() | foreach {
    Write-Host "##vso[task.setvariable variable=myvar-$($_.key);]$($_.value.value)"   
    Write-Host "##vso[task.setvariable variable=myvar-$($_.key);isOutput=true;]$($_.value.value)"   
    Write-Host "Added variable: myvar-$($_.key)"
}

The above script creates multiple properties assume myvar-varone, myvar-vartwo

- task: Powershell@2
            inputs:
              targetType: 'inline'
              script: 'Write-Host "$(myvar-varone) $(myvar-vartwo)"'

The above successfully prints the values now can you please tell me how do I get these in another job?


Solution

  • As you would like to access variable across jobs, please fix as below:

    1. Make sure to use isoutput=true when you set the variable under steps. In my sample, i set variable in template. enter image description here
    parameters:
    - name: 'var'  # defaults for any parameters that aren't specified
      type: string
    
    steps:
    - pwsh: Write-Host "##vso[task.setvariable variable=myVariable;isoutput=true]${{ parameters.var }}"
      name: setvarStep
    

    PS: if you don't set variable in template, in Dep1 job, you need to set variable with isoutput=true so the variable can be used in next job.

    1. In the main yaml, to invoke the variable in same job, you need to add stepname before variable, like $(setvarStep.myVariable).

    2. In the Next Dep2 job, be sure to prefix the job name to the output variables of a deployment job. In this case, the job name is Dep1.

    The Main Yaml:

    pool:
      vmImage: ubuntu-latest
    
    jobs:
      - deployment: Dep1
        displayName: MyDep1
        variables:
          - name: var1
            value: var1value
        environment: Dep1
        strategy:
          runOnce:
            deploy:
              steps:
              - template: template/azure-devops-template1.yml
                parameters:
                  var: "testvar value"
              
              - task: PowerShell@2
                inputs:
                  targetType: 'inline'
                  script: 'Write-Host $(setvarStep.myVariable)'
    
    
      - deployment: Dep2
        displayName: MyDep2
        dependsOn: [Dep1]
        environment: Dep2
        variables: 
          VartoUse: $[ dependencies.Dep1.outputs['Dep1.setvarStep.myVariable'] ]
        strategy:
          runOnce:
            deploy:
              steps:
              - task: Powershell@2
                inputs:
                  targetType: 'inline'
                  script: 'Write-Host "$(VartoUse)"' 
    

    Check value in pipeline: enter image description here

    More details you can refer to the official doc below:

    enter image description here