getting json data into azure pipeline

30 Views Asked by At
jobs:
  # Set an output variable from job A
  - job: A
    steps:
    - powershell: |
        $image_infor=Invoke-WebRequest https://raw.githubusercontent.com/Sitecore/docker-images/master/tags/sitecore-tags.json ###json data
        Write-Host $image_infor
        Write-Host "##vso[task.setvariable variable=myOutput_currentDate;isoutput=true]$image_infor"
      name: setvarStep
    
  # Map the variable into job B
  - job: B
    dependsOn: A
    variables:
      myVarFromJobA: $[ dependencies.A.outputs['setvarStep.myOutput_currentDate'] ]  
    steps:
    - powershell: |
          Write-Host $myVarFromJobA.Name

this is the issue i have been facing. The content of the variable $image_infor does not not get passed to the second job. It appears blank without an error. So when i look at the logs for job A i can actuall see the value of the $image_infor but it does not get to job B . Any help on that please.

expecting to seet the json value in job B

2

There are 2 best solutions below

0
wenbo On

Several points need to be pay attentions.

  1. setVariable should pass the string data type.(but yours is an object)
  2. isoutput should be isOutput
  3. $myVarFromJobA.Name should be $(myVarFromJobA)
  4. pipeline agent has the limitate memory, so your setVariable size can not be too large. your json data size is too large here.

I have coded a sample with a tiny json data size.

jobs:
# Set an output variable from job A
- job: A
  steps:
  - powershell: |
      $image_infor = Invoke-WebRequest https://raw.githubusercontent.com/Azure/azure-quickstart-templates/a743b015cc57a8a98d92e9c2743e52715d020258/demos/backup-la-reporting/prereqs/prereq.azuredeploy.json ###json data

      Write-Host "sample input is: $image_infor"

      $iamge_info_string = $image_infor.Content | ConvertTo-Json -Depth 5

      Write-Host "sample input is: $iamge_info_string"
      Write-Host "##vso[task.setvariable variable=myOutput_currentDate;isOutput=true]$iamge_info_string"
    name: setvarStep
  
# Map the variable into job B
- job: B
  dependsOn: A
  variables:
    myVarFromJobA: $[ dependencies.A.outputs['setvarStep.myOutput_currentDate'] ]
  steps:
  - powershell: |
      Write-Host "sample result is: $(myVarFromJobA)"

0
Alvin Zhao - MSFT On

Assuming you would only expect to use the Content of the web request to define the output variable rather than to output the whole response, please reference the sample YAML pipeline below.

jobs:
  # Set an output variable from job A
  - job: A
    steps:
    - powershell: |
        $response = Invoke-WebRequest -Uri https://$(sampleStorage)/sample.json
        Write-Host "================1. Web request response:================"
        $response
        
        Write-Host "================2. Convert Web request response.Content as a one-line json string:================"
        $image_infor = $response.Content | ConvertFrom-Json | ConvertTo-Json -Depth 10 -Compress
        $image_infor

        Write-Host "================3. Set output variable myOutput_currentDate:================"
        Write-Host "##vso[task.setvariable variable=myOutput_currentDate;isoutput=true]$image_infor"
      name: setvarStep
    - powershell: |
        Write-Host "================4. Check output variable value in the same job:================"
        Write-Host "$(setvarStep.myOutput_currentDate)"
      displayName: Check output variable value in the same job
    
  # Map the variable into job B
  - job: B
    dependsOn: A
    variables:
      myVarFromJobA: $[ dependencies.A.outputs['setvarStep.myOutput_currentDate'] ]
    steps:
    - powershell: |
        Write-Host myVarFromJobA: '$(myVarFromJobA)'
        $object = '$(myVarFromJobA)' | ConvertFrom-Json
        Write-Host Ojbect: $object
        Write-Host name: $object.name
        Write-Host Array.Key6: $object.Array[1].Key6

enter image description here enter image description here In job A, we should first convert the json value retrived from the .json file to a one-line json string in order to pass it to downstream job; in job B we need to convert the output json string back to an object, so that we can extract the value referencing the object key name; in addition, we should add brackets $( variable ) to use the output variable in script.

See for information in Set variables in scripts - Azure Pipelines | Microsoft Learn