Groovy 如何从Jenkins文件中读取package.json中的命令

Groovy 如何从Jenkins文件中读取package.json中的命令,groovy,jenkins-pipeline,jenkins-groovy,Groovy,Jenkins Pipeline,Jenkins Groovy,我有一个package.json "scripts": { "pact:tests": <script> } 但是上面的代码返回pactExists=null 尝试2: def pactExists2 = sh "${grep pact:tests package.json}" 但我认为这直接运行命令 尝试3: def packageJSON = readJSON file: 'package.json' def pactExists = packageJSON.scripts.

我有一个package.json

"scripts": {
"pact:tests": <script>
}
但是上面的代码返回pactExists=null

尝试2:

def pactExists2 = sh "${grep pact:tests package.json}"
但我认为这直接运行命令

尝试3:

def packageJSON = readJSON file: 'package.json'
def pactExists = packageJSON.scripts.pact:tests
但在约定之后使用“:”会出现错误:


是否有办法将输出保存在某个变量中?

如果要捕获
sh
步骤的结果,需要将
returnStdout
选项设置为
true
,例如

def pactExists=sh(脚本:“grep\”pact:tests\“package.json | wc-l”,returnStdout:true)
或者,如果要使用
readJSON
步骤,则需要使用引号定义键,例如

def pactExists=packageJSON.scripts.'pact:tests'
def packageJSON = readJSON file: 'package.json'
def pactExists = packageJSON.scripts.pact:tests