how is it possible to parse yaml file to extract appVersion from it ?
I can read file content with file content to variable to get all content but I can not parse yaml file to extract appVersion from it.
powershell script:
# 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"
CodePudding user response:
Unfortunately, as of version 7.2, PowerShell has no built-in support for parsing YAML - see the relevant feature request in GitHub issue #3607.
However, third-party modules are available, such as powershell-yaml
, available from the PowerShell Gallery here, from where you can directly deploy it to Azure Automation. To install it locally, use Install-Module -Name powershell-yaml
.
Assuming the module is installed, here's how to parse the YAML in your question, using a here-string to define the input text:
$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
The above yields 1.0.2
, as intended.
ConvertFrom-Yaml
outputs an ordered hashtable that represent the YAML input (technically, an instance of type [System.Collections.Specialized.OrderedDictionary]
), whose entries PowerShell allows you to access as if they were properties (e.g. .appVersion
) or using index syntax (e.g. ['appVersion']
).