0
votes

I am trying to reuse a variable output by one job in another (both within the same stage), as described in the documentation, but I can't get it to work. The same solution is also documented in this question.

I'm using a PowerShell script that is outputting the variable -

Write-Output "##vso[task.setvariable variable=WEBSITE_CONTENTSHARE;isOutput=true]some-value-dh8e"

And then I'm setting the variable in the next job like this. Note that Output_Required_App_Settings is the name of both the job and the task that is outputting the variable WEBSITE_CONTENTSHARE -

- deployment: Deploy_ARM_Template
  displayName: Deploy ARM Template
  dependsOn:
  - Output_Required_App_Settings
  variables:
  - name: parameters.functionAppSettings.value.WEBSITE_CONTENTSHARE
    value: $[dependencies.Output_Required_App_Settings.outputs['Output_Required_App_Settings.WEBSITE_CONTENTSHARE']]
  vmImage: ubuntu-latest

However, the value of parameters.functionAppSettings.value.WEBSITE_CONTENTSHARE is empty when I use it, meaning that my deployment fails.

In case anyone wonders, the reason for the long name is because I am using JSON path replacement in this job to insert the value of WEBSITE_CONTENTSHARE into an ARM template parameters file.

How can I share a variable between jobs?

1
Hi David, is there any update for this issue? Feel free to let me know whether my anwser helps.Jane Ma-MSFT

1 Answers

0
votes

I did a test with your code and have output the variable successfully.

Here is my complete YAML file:

trigger: none

pool:
  vmImage: 'ubuntu-latest'

jobs:
- job: Output_Required_App_Settings
  steps:
  - task: PowerShell@2
    name: Output_Required_App_Settings
    inputs:
      targetType: 'inline'
      script: |
        Write-Output "##vso[task.setvariable variable=WEBSITE_CONTENTSHARE;isOutput=true]some-value-dh8e"
- deployment: Deploy_ARM_Template
  displayName: Deploy ARM Template
  dependsOn:
  - Output_Required_App_Settings
  variables:
  - name: parameters.functionAppSettings.value.WEBSITE_CONTENTSHARE
    value: $[dependencies.Output_Required_App_Settings.outputs['Output_Required_App_Settings.WEBSITE_CONTENTSHARE']]
  environment: ubuntu-latest
  strategy:
    runOnce:
      deploy:
        steps:
        - script: echo $(parameters.functionAppSettings.value.WEBSITE_CONTENTSHARE)

And here is the running result of CmdLine task:

enter image description here

The strange behavior that your output value was empty could result from following factors.

  1. If you are using Json format variable, you need to convert it into one line first. You can refer to this question. How to pass Json variable to command line task in Azure DevOps pipeline?

  2. Jobs and tasks both support 'name' and 'displayName' inputs, please make sure you use 'name: Output_Required_App_Settings' instead of 'diaplayName: Output_Required_App_Settings'