jenkins-pipeline - jq 在 Jenkins 管道中没有将输出保存到变量
问题描述
因此,在我的 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 |"
}
最终返回 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
参数传递给 shell step 方法stageTwoList
,但后来忘记使用相同的参数来解析 JSON 并返回到接下来的两个变量赋值:
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 流水线步骤方法来简化此操作:
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']
推荐阅读
- differential-equations - 使用 Matlab BVP4C 进行时间演化
- linux - 在 Yocto 中如何进行成像和恢复
- qt - 使用来自 QtLocation 的卫星视图作为 3D 场景中的地面纹理
- sql-server - 当它是主键时,如何将数据类型Varchar的列加载到sql中列数据类型Date的其他表中
- node.js - 将多个项目发送到 Node JS 以进行批量删除
- ios - 无法下载适用于 IOS 的 Tello SDK
- docker - 当 jenkins 作业中止时,Docker 容器崩溃
- react-native - 复选框未显示选中(反应原生)
- laravel - 插入第三条记录时不显示任何内容 laravel
- php - 使用 Guzzle 读取 curl 请求进度标头