I need help in retrieving a variable as list passed from one stage to another in Azure pipeline. I have tried multiple methods but I only managed to echo the first element of list.
In the stageA
below, I create a variable output foo
which returns a list of strings.
I then retrieve this variable in stageB
as bar
and try to print it for validating the format but only get the first element.
- stage: StageA
jobs:
- job: JobA
steps:
- script: |
...
myList=...
echo "Printing out my list: ${myList[@]}" # prints perfectly fine list
echo "##vso[task.setvariable variable=foo;isOutput=true]${myList[@]}" # prints perfectly fine list
name: getList
- stage: StageB
dependsOn: StageA
variables:
bar: $[ stageDependencies.StageA.JobA.outputs['getList.foo'] ]
jobs:
- job: JobB
steps:
- script: |
echo $(bar) # prints first element of list
for b in $(bar); do echo $b; done; # prints first element of list
echo ${bar[@]} # prints nothing
echo ${bar[*]} # # prints nothing
If there's anything I am missing over here, I'd appreciate some suggestions regarding how to fix it. Thanks in advance.
This is not possible because variables are just plain strings. You could try to pass JSON as a string and then in the subsequent stage use jq to process it.
Or any other way like a string with delimiter and then parse it, split and process.