运行 每 1 秒执行一次 cURL 命令,直到满足条件
Running cURL command every 1 second until condition is met
创建票证后,我需要连续检查状态(每 1 秒一次),直到返回 approved
或 rejected
。然后,我想退出循环。谁知道一个干净的方法来做到这一点?
stages {
stage('Open and Check ticket status’) {
steps {
script {
dockerImage.inside(" -u root ") {
// Open ticket
withCredentials([string(credentialsId: JENKINS_API_KEY, variable: 'API_KEY')]) {
cmd = "curl -X POST -H 'x-api-key:${API_KEY}' '${URL}/requests' | jq -r '.requestId'"
REQUEST_ID = sh(script: cmd, returnStdout: true).trim()
print(REQUEST_ID)
}
// Check ticket Status
withCredentials([string(credentialsId: JENKINS_API_KEY, variable: 'API_KEY')]) {
cmd = "curl -X GET -H 'x-api-key:${API_KEY}' '${URL}/requests/${REQUEST_ID}' | jq -r '.requestStatus'"
REQUEST_STATUS = sh(script:cmd, returnStdout: true).trim()
print(REQUEST_STATUS)
}
}
}
}
}
}
您可以使用 while 循环尝试这个。我相信它有效..
// 检查工单状态
withCredentials([string(credentialsId: JENKINS_API_KEY, variable: 'API_KEY')])
{
while ({
cmd = "curl -X GET -H 'x-api-key:${API_KEY}' '${URL}/requests/${REQUEST_ID}' | jq -r '.requestStatus'"
REQUEST_STATUS = sh(script:cmd, returnStdout: true).trim()
print(REQUEST_STATUS)
sleep('time': 1,'unit':"SECONDS")
!["approved","rejected"].contains(REQUEST_STATUS)
}()) continue
}
创建票证后,我需要连续检查状态(每 1 秒一次),直到返回 approved
或 rejected
。然后,我想退出循环。谁知道一个干净的方法来做到这一点?
stages {
stage('Open and Check ticket status’) {
steps {
script {
dockerImage.inside(" -u root ") {
// Open ticket
withCredentials([string(credentialsId: JENKINS_API_KEY, variable: 'API_KEY')]) {
cmd = "curl -X POST -H 'x-api-key:${API_KEY}' '${URL}/requests' | jq -r '.requestId'"
REQUEST_ID = sh(script: cmd, returnStdout: true).trim()
print(REQUEST_ID)
}
// Check ticket Status
withCredentials([string(credentialsId: JENKINS_API_KEY, variable: 'API_KEY')]) {
cmd = "curl -X GET -H 'x-api-key:${API_KEY}' '${URL}/requests/${REQUEST_ID}' | jq -r '.requestStatus'"
REQUEST_STATUS = sh(script:cmd, returnStdout: true).trim()
print(REQUEST_STATUS)
}
}
}
}
}
}
您可以使用 while 循环尝试这个。我相信它有效..
// 检查工单状态
withCredentials([string(credentialsId: JENKINS_API_KEY, variable: 'API_KEY')])
{
while ({
cmd = "curl -X GET -H 'x-api-key:${API_KEY}' '${URL}/requests/${REQUEST_ID}' | jq -r '.requestStatus'"
REQUEST_STATUS = sh(script:cmd, returnStdout: true).trim()
print(REQUEST_STATUS)
sleep('time': 1,'unit':"SECONDS")
!["approved","rejected"].contains(REQUEST_STATUS)
}()) continue
}