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

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

问题描述

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

解决方案

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

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

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:

解决方案

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

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天全站免登陆