【发布时间】:2021-12-31 03:40:09
【问题描述】:
如何解析 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"
【问题讨论】:
-
顺便说一句:
Write-Hostis typically the wrong tool to use,除非意图是仅写入显示器,绕过成功输出流,并能够将输出发送到其他命令,将其捕获在变量中,将其重定向到文件。要输出一个值,请单独使用它;例如,$value而不是Write-Host $value(或使用Write-Output $value,尽管这很少需要)。另见:stackoverflow.com/a/50416448/45375的底部 -
顺便说一句:必须调用 PowerShell 函数、cmdlet、脚本和外部程序像 shell 命令 -
foo arg1 arg2- 不像 C#方法 -foo('arg1', 'arg2')。如果您使用,分隔参数,您将构造一个命令将其视为单个参数 的数组。为防止意外使用方法语法,请使用Set-StrictMode -Version 2或更高版本,但请注意其其他影响。请参阅this answer 了解更多信息。
标签: powershell azure-devops yaml azure-pipelines pipeline