从json文件导入Azure Devops管道变量 [英] import Azure Devops pipeline variables from json file

查看:167
本文介绍了从json文件导入Azure Devops管道变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用json文件存储Powershell脚本使用的变量.在azure devops中,我已经配置了管道,管道包含powershell脚本以及其中的变量列表.我想使用json文件并将所有变量存储在此文件中.我想将json文件存储在Azure存储库中,并在管道启动后使用它.我不想使用变量组.我找到了此解决方案[ Json to Variable ]

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:

推荐答案

为什么不直接使用Powershell内置cmdlet:ConvertFrom-Json.请参阅有关此cmdlet描述的官方文档: Powershell:ConvertFrom-Json .

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

您可以添加脚本:

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

Get-Content会将JSON文件读入字符串数组,然后ConvertFrom-Json将这些字符串转换为PSCustomObject对象.然后,您可以使用简单的杠杆点表示法(.)调用完全相同的对象,然后以这种格式将其用于任务脚本中.

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

在这里,我在本地Powershell-ISE中进行了尝试,这与Powershell任务中的用法相同. ts是我的JSON对象之一.在azure devops中,如果将文件存储在Repos中,则可以使用$(Build.SourcesDirectory)\..\..\...json之类的路径来指定文件位置.

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.

这篇关于从json文件导入Azure Devops管道变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆