Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Devops - Output Json Object from one task and Consume in another task

Say I have a release pipeline in Azure DevOps written in yaml which has two tasks, one for reading json from a file and the second one for setting a key into a different json file using the json read in the first task. I have the following pipeline.yml -

trigger:
- master

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: PowerShell@2
  name: ReadMetadataJson
  inputs:
    filePath: 'MetadataReader.ps1'
    arguments: 'Run MetadataReader.ps1 -pathToMetadata metadata.json'
- task: PowerShell@2
  name: SetAppSetting
  inputs:
    filePath: 'AppSettingSetter.ps1'
    arguments: 'Run AppSettingSetter.ps1 -pathToAppSetting SomeApp/Data.json -appSettingKey testkey -appSettingValue $($(ReadMetadataJson)).testkey'
- script: echo $(ReadMetadataJson.metadata)

Below are the Powershell scripts being called from each tasks -

Powershell 1

# Read From the Metadata.json
param ($pathToMetadata)

echo $pathToMetadata
$metadata = Get-content $pathToMetadata | out-string | ConvertFrom-Json

Write-Output "Metadata Json from metadata reader ps script - $metadata"
echo "##vso[task.setvariable variable=metadata;]$metadata"

Powershell 2

# For now just accept the values in parameter and log them
param ($pathToAppSetting, $appSettingKey, $appSettingValue)

echo "pathToAppSetting : $pathToAppSetting"
echo "appSettingKey : $appSettingKey"
echo "appSettingValue : $appSettingValue"

# Code to set in another file. I have this working, so omitting for brevity

And these are the json files -

Metadata.json

{
  "testkey": "TestValueFromMetadata",
  "testkey1": "TestValueFromMetadata1"
}

appSetting.json

{
  "testkey": "TestValueInAppSetting",
  "testkey1": "TestValueInAppSetting1"
}

The problem is when I want to return the json data as output from the first task and use it in the second task to pass the parameter to the second powershell script. Below is a screenshot of the pipeline result after I run it.

enter image description here

As can be seen, it says ReadMetadataJson.metadata: command not found. I have been following the Microsoft document as a reference and have searched for other articles, but all I could find was handling values like string or integer, but not a json object. What is it that I am missing or doing wrong.

like image 841
gkb Avatar asked Oct 29 '25 10:10

gkb


1 Answers

You can convert your JSON object to string (ConvertTo-Json) and pass it as variable to the second script.

Then on the second script, you just parse the string to JSON object again, using the ConvertFrom-Json method.

like image 182
Hugo Barona Avatar answered Oct 31 '25 01:10

Hugo Barona



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!