Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

import Azure Devops pipeline variables from json file

I want to use json file to store variables used by powershell script. In azure devops I have configured pipeline, pipeline contains powershell script with a list of variables in it. I would like to use json file and store in this file all variables. I want to store json file in Azure repo, and use it once pipeline is started. I dont want to use variable groups. I found this solution [Json to Variable]https://marketplace.visualstudio.com/items?itemName=OneLuckiDev.json2variable&targetId=02fbae5c-cc01-4c8b-a90f-7393a3156347 but I read some uncomplimentary opinions about this task. Is there any method to use json file as a place to store variables, or maybe I can use different file format? UPDATE: enter image description here

like image 239
tester81 Avatar asked Oct 28 '19 20:10

tester81


1 Answers

Why not directly to use the Powershell built-in cmdlet: ConvertFrom-Json. See this official doc about this cmdlet description: Powershell: ConvertFrom-Json.

You can add the script:

Get-Content "{JSON file path}" | out-string | ConvertFrom-Json

Get-Content would read the JSON file into an array of strings, and ConvertFrom-Json convert these strings into the PSCustomObject object. Then you can call the exactly object with simple leverage dot notation(.), then use it with this format into the task script.

Here is the simple sample can for you refer:

$data = Get-content {your JSON file path}| out-string | ConvertFrom-Json
Write-Output $data.ts

enter image description here

Here I tried in my local Powershell-ISE which is same usage in Powershell task. ts is one of my JSON object. In azure devops, you can use the path like $(Build.SourcesDirectory)\..\..\...json to specified your file location if you store it in Repos.

like image 126
Mengdi Liang Avatar answered Oct 11 '22 16:10

Mengdi Liang