Jenkins 管道中的 jq 不将输出保存到变量

jq in a Jenkins pipeline not saving output to variable

所以在我的 Jenkins 管道中,我 运行 跨不同阶段的几个 curl 命令。我将 Stage1 的输出存储到一个文件中,对于该列表中的每个项目,我 运行 另一个 curl 命令并使用它的输出使用 jq.

提取一些值

但是从第二阶段开始,我似乎无法将 jq 提取的值存储到变量中以便稍后回显它们。我做错了什么?

{Stage1}
.
.
.
{Stage2}
def lines = stageOneList.readLines()
lines.each { line -> println line
                        
stageTwoList = sh (script: "curl -u $apptoken" + " -X GET --url " + '"' + "$appurl" + "components/tree?component=" + line + '"', returnStdout: true)                                
pfName = sh (script: "jq -r '.component.name' <<< '${stageTwoList}' ")
pfKey = sh (script: "jq -r '.component.key' <<< '${stageTwoList}' ")
echo "Component Names and Keys\n | $pfName | $pfKey |"
}

returns最后为Stage2

[Pipeline] sh
+ jq -r .component.name
digital-hot-wallet-gateway
[Pipeline] sh
+ jq -r .component.key
dhwg
[Pipeline] echo
Component Names and Keys
 | null | null |

感谢任何正确方向的帮助!

您将 true 作为 returnStdout 参数的参数传递给 stageTwoList 的 shell 步骤方法,但随后忘记为 JSON 将 returns 解析为接下来的两个变量赋值:

def lines = stageOneList.readLines()
lines.each { line -> println line
                    
  stageTwoList = sh(script: "curl -u $apptoken" + " -X GET --url " + '"' + "$appurl" + "components/tree?component=" + line + '"', returnStdout: true)                                
  pfName = sh(script: "jq -r '.component.name' <<< '${stageTwoList}' ", returnStdout: true)
  pfKey = sh(script: "jq -r '.component.key' <<< '${stageTwoList}' ", returnStdout: true)
  echo "Component Names and Keys\n | $pfName | $pfKey |"
}

请注意,您还可以通过在 Groovy 中进行 JSON 本地解析并使用 Jenkins Pipeline 步骤方法让自己更轻松:

String stageTwoList = sh(script: "curl -u $apptoken" + " -X GET --url " + '"' + "$appurl" + "components/tree?component=" + line + '"', returnStdout: true)
Map stageTwoListData = readJSON(text: stageTwoList)
pfName = stageTwoListData['component']['name']
pfKey = stageTwoListData['component']['key']