1

I have JS file in my project with config:

export default {
  key: "water",
  title: "Shop",
  icon: "shop-icon",
};

I need get (inside Jenkins script pipeline) key value: water. I can doing this by readFile function: Jenkins Read a Specific Line of a File in Jenkinsfile with Groovy and use split method.

But I want to ask: is there any other solution to read value from JS file?

michal
  • 1,534
  • 5
  • 28
  • 62

1 Answers1

0

I don't think there is any out of the box solution for this but I can suggest using a regex for this. You can see the correct working of the regex on regex101.

(?<=key: ")(.*\n?)(?=",)

Use it like this:

def jsExport = readFile "<PathToFile>"
def regex = """(?<=key: ")(.*\n?)(?=",)"""
def result = (jsExport =~ regex)
echo "${result[0][0]}"

See a compileable example here.

Michael Kemmerzell
  • 4,802
  • 4
  • 27
  • 43