0

I have a pipeline with multiple jobs. I would like to send the output that I am setting in a powershell script to another job in the pipeline.

I have followed several examples to do this, like: 'https://medium.com/microsoftazure/how-to-pass-variables-in-azure-pipelines-yaml-tasks-5c81c5d31763' and 'https://devops.stackexchange.com/questions/17604/how-do-i-refer-in-azure-pipelines-to-an-output-variable-defined-in-a-previous-st'.

I can access the output variables that I am setting if the tasks are in the same job, but in different jobs the variables are empty.

This is my current pipeline:

- stage: DeploytoDev
  displayName: Deploy to Dev
  condition: ${{ eq(variables['Build.SourceBranch'], 'refs/heads/develop') }}
  jobs:     
    - deployment: DeployInfraDev
      environment: env
      strategy:
        runOnce:
          deploy:
            steps:
            - task: AzureResourceManagerTemplateDeployment@3
              displayName: 'Infra Deployment: Storage Account, Function App Develop'
          inputs:
            deploymentScope: 'Subscription'
            azureResourceManagerConnection: '$(serviceConnectionDev)'
            action: 'Create Or Update Resource Group'
            subscriptionName: '$(subscriptionDev)'
            resourceGroupName: '$(resourceGroup)'
            location: '$(location)'
            templateLocation: 'Linked artifact'
            csmFile: '$(Pipeline.Workspace)/$(infraArtifactName)/templates/main.bicep'
            overrideParameters: >-
              -appName $(appName) 
              -stage dev 
              -location $(location)
            deploymentMode: 'Incremental'
            deploymentName: 'DeployPipelineTemplate'
            deploymentOutputs: 'deploymentOutputs'
          name: 'deployment'

        - task: PowerShell@2
          name: 'outputVariables'
          displayName: 'Set Deployment Output Variables'
          inputs:
            targetType: inline
            script: |
              $armOutputObj = '$(deploymentOutputs)' | ConvertFrom-Json

              $armOutputObj.PSObject.Properties | ForEach-Object {
                $keyname = $_.Name
                $value = $_.Value.value

                # Creates a standard pipeline variable
                Write-Output "##vso[task.setvariable variable=$keyName;isOutput=true]$value"

                # Display keys in pipeline
                Write-Output "output variable: $keyName $value"
              }
              Write-Output "##vso[task.setvariable variable=test;isOutput=true]test"

            pwsh: true

        - bash: echo "$(outputVariables.test) | $(outputVariables.storageAccountName)"

- job: BuildWebsite
  dependsOn: DeployInfraDev
  variables:
    storageAccountName: $[ dependencies.DeployInfraDev.outputs['outputVariables.storageAccountName'] ]
    storageAccountNameFoo: $[ dependencies.DeployInfraDev.outputs['outputVariables.test'] ]
  steps:
    - bash: echo "$(storageAccountName) | $(storageAccountNameFoo)"
    - bash: echo "$storageAccountName | $storageAccountNameFoo"
      env: 
        storageAccountName: $(storageAccountName)
        storageAccountNameFoo: $(storageAccountNameFoo)

Image of the Bash inline scripts of the jobs:

  • Same job bash script:

    enter image description here

  • Different Job without setting env variables in task

    enter image description here

  • Different Job while setting env variables in task

    enter image description here

I was expecting that in both bash scripts in the 'BuildWebsite' Job would print the same values as the bash script from the 'DeployInfraDev' Job.

Pierre.Vriens
  • 7,225
  • 14
  • 39
  • 84
krakergig
  • 1
  • 1

2 Answers2

0

For deployment jobs check this format $[dependencies.<job-name>.outputs['<job-name>.<step-name>.<variable-name>']]. Documentation: Support for output variables

0

While executing deployment strategies, you can access output variables across jobs using the following syntax.

For runOnce strategy: $[dependencies..outputs['..']] (for example, $[dependencies.JobA.outputs['JobA.StepA.VariableA']]) For runOnce strategy plus a resourceType: $[dependencies..outputs['..']]. (for example, $[dependencies.JobA.outputs['Deploy_VM1.StepA.VariableA']]) For canary strategy: $[dependencies..outputs['..']] For rolling strategy: $[dependencies..outputs['_..']]

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

geeky_girl
  • 81
  • 3