jenkins - 如何在 Jenkins 中创建一个脚本循环,该循环根据 shell 命令接收的 json 继续或返回成功/错误到管道?
问题描述
我在我的 Linux Box 中使用最新的 Jenkins。我正在尝试使用如下脚本块创建管道;
pipeline {
agent any
stages {
stage('TestStage') {
steps {
script {
sh "testfile.sh"
}
}
}
}
}
testfile.sh
将返回如下所示的 json 文本;
{
"Worker": [
{
"Status": "running"
}
]
}
Status
可以是running
或success
或failure
。如果是running
,代码必须再次调用testfile.sh
并检查状态。如果是success
,管道必须继续下一步,如果是failure
,管道必须终止。有可能实现这一目标吗?
谢谢。
解决方案
您可以通过创建一个执行脚本的while循环来实现,读取输出并检查其值直到给定的超时,当状态不再是running
您可以检查结果并根据最终状态使构建失败。
就像是:
pipeline {
agent any
stages {
stage('TestStage') {
steps {
script {
timeout(time: 1, unit: 'HOURS') { // timeout for the 'running' period
def status = sh script: 'testfile.sh', returnStdout: true
while (status == 'running') {
sleep time: 10, unit: 'SECONDS' // sleep between iterations
def output = sh script: 'testfile.sh', returnStdout: true
def dict = readJSON text: output
status = dict.Worker.Status
}
}
if(status == 'failure'){
error "Operation has ended with status 'failure'"
}
}
}
}
}
}
推荐阅读
- c# - 如何在 Unity 中更改其他玩家的相机?
- java - 黄瓜,在所有场景中重复登录步骤
- mongodb - 使用 $let 的聚合 $lookup 不起作用
- python - 尝试在 python 中绘制图像(使用 scipy 和 numpy)
- image - 从二值图像中去除噪声(小白块)
- react-native - Xcode 10 Build Location -> Per-configuration Build Products Path
- flutter - StreamBuilder 和 AsyncSnapshot + rxdart 颤振问题
- python - 如何在 Django 中使用 OrientDB?
- windows - 如何通过 MSVS 2017 x64 命令行将链接器选项传递给“cl”?
- c - C程序提前结束