2
votes

As it is not possible to pass a variable between stages in AzurePipeline, I'm trying to pass a variable between two jobs instead as it is explained in Azure documentation (https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch), however it wasn't successful and it returns an empty variable.

here is what I'm trying:

stages:
- stage: Deploy
  jobs:
  - deployment: A
    displayName: TerraformDeploy
    pool:
      vmImage: 'ubuntu-latest'
    environment: 'test'
    strategy:
      runOnce:
       deploy:
        steps:
        - checkout: self
        - task: Bash@3
          displayName: 'Deploying Terraform'
          inputs:
            targetType: 'inline'
            script: |
              cd environments/test
              terraform init
              terraform apply -var 'client_id=$(client-id)' -var 'client_secret=$(client-secret)' -var 'key_data=$(LinuxSSHPubKey)' -var 'tenant_id=$(tenant-id)' -auto-approve
              ip=$(terraform output public_ip_address)
              echo $ip   ###1.2.3.4
              echo "##vso[task.setvariable variable=myPubIP;isOutput=true]$ip"
            name: setvarStep

  - job: B
    dependsOn: A
    pool:
      vmImage: 'ubuntu-latest'
    variables:
      myIP: $[ dependencies.A.outputs['A.setvarStep.myPubIP'] ]
    steps:
    - task: AzureCLI@2
      inputs:
        azureSubscription: $(azureSubscription)
        scriptType: 'bash'
        scriptLocation: 'inlineScript'
        inlineScript: |
          echo "ip:"$(myIP)   ### Empty variable!!

I've tried so many different things for this case but it hasn't been fixed so far, can anyone help with this?

Updated:

It seems someone else is also reported this issue and they are planning to fix it:

https://developercommunity.visualstudio.com/content/problem/769690/sharing-variables-between-jobs-not-working-for-dep.html

Even if I use both jobs as normal job, it still doesn't work for my case..

1
I have copied and adapted your yaml, and instead of AzureCLI I have used Bash task, variable was passed between jobs successfully. I suppose that something is wrong with AzureCLI@2 task(maybe cannot access variables) or with your'environment test'. Please check using Bash@3 in job B and check if variable is passed. My yaml and result is in attached picture. Seems that it is not problem with using 'deployment' or 'job'. i.stack.imgur.com/2rTmk.pngKontekst

1 Answers

2
votes

Not very sure it is a paste format issue in the above YAML pipeline definition, and I saw the ip value has generated very successfully, it seems cause by the syntax of name in your YAML definition is not correct, so it is not correctly compiled as reference name.

Just go and confirm whether there has a yellow line under the name, like this:

enter image description here

If yes, remove 2 space before name, so that let it at the same level with inputs:

enter image description here


If I use the same definition with you, I also faced the issue of empty output variable. But if I did the change on space before name, then output variable can successfully get in job B.

So, you can have try with this on your side.