如何在 azure devops 中解析 yaml 文件
How to possible parse yaml file in azure devops
如何解析 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"])
chart.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"
不幸的是,从 7.2 版开始,PowerShell 没有 对解析 YAML 的内置支持 - 请参阅 GitHub issue #3607 中的相关功能请求。
但是,第三方模块 可用,例如 powershell-yaml
, available from the PowerShell Gallery here,您可以从那里直接将其部署到 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']
)。
如何解析 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"])
chart.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"
不幸的是,从 7.2 版开始,PowerShell 没有 对解析 YAML 的内置支持 - 请参阅 GitHub issue #3607 中的相关功能请求。
但是,第三方模块 可用,例如 powershell-yaml
, available from the PowerShell Gallery here,您可以从那里直接将其部署到 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']
)。