如何在azure devops中解析yaml文件

问题描述 投票:0回答:1

如何解析 yaml 文件以从中提取 appVersion ?

我可以将文件内容读取到变量以获取所有内容 但我无法解析 yaml 文件以从中提取 appVersion。

powershell脚本:

# get chart.yaml file content to yamlfilecontent variable.
write-host($env:yamlfilecontent)

# how to parse content ?
write-host($env:yamlfilecontent["appVersion"]) 

图表.yaml

apiVersion: v2
name: asset-api
description: Helm Chart for Kubernetes

# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 1.5.2

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "1.0.2"
powershell azure-devops yaml azure-pipelines pipeline
1个回答
1
投票

不幸的是,从版本 7.2 开始,PowerShell 没有对解析 YAML 的内置支持 - 请参阅 GitHub 问题 #3607 中的相关功能请求。

但是,第三方模块是可用的,例如

powershell-yaml
,可从PowerShell库此处获得,您可以从那里直接将其部署到Azure自动化。要在本地安装它,请使用
Install-Module -Name powershell-yaml

假设已安装模块,以下是如何解析问题中的 YAML,使用 here-string 定义输入文本:

$yamlText = @'
apiVersion: v2
name: asset-api
description: Helm Chart for Kubernetes

# A chart can be either an 'application' or a 'library' chart.
# ...
version: 1.5.2

# This is the version number of the application being deployed. This version number should be
# ...
appVersion: "1.0.2"
'@

($yamlText | ConvertFrom-Yaml).appVersion

上面的结果是

1.0.2
,正如预期的那样。

ConvertFrom-Yaml
输出一个有序哈希表,表示 YAML 输入(从技术上讲,是
[System.Collections.Specialized.OrderedDictionary]
类型的实例),PowerShell 允许您像属性一样访问其条目(例如
.appVersion
)或使用 index 语法(例如
['appVersion']
)。

© www.soinside.com 2019 - 2024. All rights reserved.