Home > database >  Jenkins Shell Scripting issue
Jenkins Shell Scripting issue

Time:03-04

I try to get an information out of a helm file in Jenkins. For this I do:

helm history adt-development -n adt-development --max 1 --output yaml

to get the value ".chart" inside the helm yaml output I use yq

yq ".chart" 

Because there is a bad yaml file reported, a "-" is too much at the beginning I try to cut it.

However, how to write this command in jenkins shell tofill a variable?

I tried:

helmvalue=$(helm history pro-development -n pro-development --max 1 --output yaml) | cut -c2- | yq '-chart' 

But yq will read the output with the "-" so it ignore the cut output. Any idea how to

  • read helm information -> cut first 2 char -> read .chart from output before.

On request additional information: reported by helm:

- app_version: 0.6.0 
  chart: prof-1.0.1-2022-03-02-1616-46eb101842db6e367f3cd9ab42636ee9bf7d4912 
  description: Upgrade complete 
  revision: 355 
  status: deployed 
  updated: '"2022-03-02T16:42:52.04760689 01:00"'

Output from yq:

Error: bad file '-': yaml: mapping values are not allowed in this context

CodePudding user response:

In YAML syntax, the dash sign - introduces a list of key-value objects, which by yq is converted into an array. Access it using .[].

With mikefarah/yq, use

yq '.[].chart'

With kislyuk/yq, add the -r option to generate raw text

yq -r '.[].chart'

CodePudding user response:

If I'm reading the intent and syntax correctly,

helmvalue=$(helm history pro-development -n pro-development --max 1 --output yaml) is stored in a variable; there is no output to stdout. Which means <some cmd output> | cut -c2- has no input from stdin to the cut command and then no input to yq.

helmvalue=$(helm history pro-development -n pro-development --max 1 --output yaml| cut -c2-| yq '-chart')

Move closing bracket ")" to end and:

  • helm history ... -output yaml will send to stdout
  • cut -c2- will process stdin, cut 2 chars and send to stdout
  • yq '-chart should process from stdin a series of colon separated values and output a helmchart (?) to stdout helmvalue=$(...) will contain the output from above helmval
  • Related