2

We have two deployment jobs that run in the same stage. The first job creates an output variable and the second job uses that output variable (code borrowed from here and implemented the same way in our pipeline).

jobs:
- deployment: producer
  environment:
    name: ${{ parameters.environment }}
    resourceType: VirtualMachine
    tags: ${{ parameters.tags }}
  strategy:
    runOnce:
      deploy:
        steps:
          - script: echo "##vso[task.setvariable variable=myOutputVar;isOutput=true]this is the deployment variable value"
            name: setvarStep
          - script: echo $(setvarStep.myOutputVar)
            name: echovar

- deployment: consumer_deploy
  dependsOn: producer
  variables:
    myVarFromDeploymentJob: $[ dependencies.producer.outputs['deploy_Vm1.setvarStep.myOutputVar'] ]
  environment: 
    name: ${{ parameters.environment }}
    resourceType: VirtualMachine
    tags: ${{ parameters.tags }}
  strategy:
    runOnce:
      deploy:
        steps:
          - script: "echo $(myVarFromDeploymentJob)"
            name: echovar

This works because we reference the virtual machine (hardcoded) that the producer deployment job runs on. However, not every stage will run on the same virtual machine.

I've tried regular variables ($(Agent.MachineName)), as well as expression syntax, passing the variable from a template file and changing the scope of the variable template, but none of them work and the 'myVarFromDeploymentJob' variable stays empty.

Is there a way to make the virtual machine name in the expression variable or more flexible? So going from this:

$[ dependencies.producer.outputs['deploy_Vm1.setvarStep.myOutputVar'] ]

To something like this:

$[ dependencies.producer.outputs['deploy_$(Agent.MachineName).setvarStep.myOutputVar'] ]
StuartvdLee
  • 75
  • 1
  • 8
  • According to docs it shoudl be like `$[ dependencies.producer.outputs['deploy_${{ parameters.environment }}.setvarStep.myOutputVar']` if you use just env name. If you have it with resource then it should be `$[ dependencies.producer.outputs['deploy_RESOURCE-NAME-HERE.setvarStep.myOutputVar']`, so I assume that `Vm1` is your resource name and `parameters.environment` is in format `envname.Vm1`, right? – Krzysztof Madej Sep 21 '21 at 08:51
  • We don't pass the resource name because we have multiple webservers for some stages, so it's easier to pass a tag to run a deployment on webservers. – StuartvdLee Sep 21 '21 at 09:31
  • So can you try this `$[ dependencies.producer.outputs['deploy_${{ parameters.environment }}.setvarStep.myOutputVar']`? – Krzysztof Madej Sep 21 '21 at 09:32
  • I just tried `$[ dependencies.producer.outputs['deploy_${{ parameters.environment }}.setvarStep.myOutputVar']` and the 'myVarFromDeploymentJob' variable is empty. – StuartvdLee Sep 21 '21 at 10:01

1 Answers1

1

Adding a solution for others.

here missing link to docs: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/deployment-jobs?view=azure-devops#support-for-output-variables

for runOnce deployment depends if you are addressing whole deployment (repeat Job name) or resource deployment (use deploy_resourceName):

variables:
  myVarFromDeploymentJob: $[ dependencies.A2.outputs['A2.setvarStepTwo.myOutputVar'] ]
  myOutputVarTwo: $[ dependencies.A2.outputs['Deploy_vmsfortesting.setvarStepTwo.myOutputVarTwo'] ]