Dan Friedman
Dan Friedman

Reputation: 5228

Retrieving an output variable from a deployment job

In the following example, I am able to access output variables of jobA, but not of deploymentA. The main difference being that jobA is a "normal" job and deploymentA is a deployment job.

jobs:
- deployment: deploymentA
  environment: Test
  strategy:
    runOnce:
      deploy:
        steps:
        - checkout: none
        - pwsh: |
            echo "##vso[task.setvariable variable=someVar;isOutput=true]someValue"
          name: someName

- job: jobA
  steps:
  - checkout: none
  - pwsh: |
      echo "##vso[task.setvariable variable=someVarA;isOutput=true]someValueA"
    name: someNameA

- job: jobB
  dependsOn: 
  - deploymentA
  - jobA

  variables:
    deployJobVar: $[dependencies.deploymentA.outputs['someName.someVar']]
    jobVar: $[dependencies.jobA.outputs['someNameA.someVarA']]

  steps:
  - checkout: none
  - pwsh: |
      echo "$(deployJobVar)"    # will display empty string
      echo "$(jobVar)"          # will display 'someValueA'

The best hint I've found is:

If you're setting a variable from a matrix or slice, then to reference the variable, you have to include the name of the job as well as the step when you access it from a downstream job.

Not exactly the same, but I tried prepending similar identifiers, but none of the following worked:

$[dependencies.deploymentA.outputs['runOnce.someName.someVar']]
$[dependencies.deploymentA.outputs['deploy.someName.someVar']]
$[dependencies.deploymentA.outputs['runOnce.deploy.someName.someVar']]

Does anyone know the correct syntax for accessing specific variables outputted from a deployment job? Alternately, does anyone know how to display all outputted variables?

Upvotes: 5

Views: 2619

Answers (2)

Victor Savu
Victor Savu

Reputation: 981

The docs are currently incomplete, but with a bit of imagination, we can notice that the second part of Set a multi-job output variable applies.

all you need to do is reference your variable with the job name as a prefix: deploymentA.someName.someVar instead of someName.someVar:

deployJobVar: $[dependencies.deploymentA.outputs['deploymentA.someName.someVar']]

Yes, deploymentA appears twice.

Upvotes: 7

Mengdi Liang
Mengdi Liang

Reputation: 19026

The correct syntax for accessing specific variables outputted from a deployment job?

It is correct you used about the syntax variable. For the caused reason about there's no value displayed in the next "normal" job, in fact you have mentioned previously, it is a deployment job.

In your YAML script, the variable someVar in the deployment job is created in job of release. This variable could not be passed out of stage, even though you set isOutput=true.(Note: This is only occurred in Release pipeline). It only exist in it current job if you just define it with ##vso[task.setvariable].

In release pipeline, the deployment job independent between each other. It means that if you define the variable in the task(eq. step), it could not be accessed out of the job. You can also test this with Classic editor.

If you want to use it in the other job, the work around is that you'd better store it in some storage like Key Vault. And then use it in the next job.

Upvotes: 0

Related Questions