Reputation: 2629
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://learn.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:
Even if I use both jobs as normal job, it still doesn't work for my case..
Upvotes: 5
Views: 7032
Reputation: 18958
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:
If yes, remove 2 space before name
, so that let it at the same level with inputs
:
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.
Upvotes: 4