在我的jenkins管道中,我有一个阶段应该用curl触发一个gitlab ci管道,它工作得很好。现在我想发送第二个curl命令来询问gitlab API最后一个管道的状态(我正在寻找状态:成功)以移动到下一个jenkins管道阶段
这是我的curl命令:
stage('Get Pipeline Status') {
steps {
script {
// Run a curl command to get pipeline details from GitLab
def pipelineStatus = sh(
script: "curl --insecure --header 'PRIVATE-TOKEN: my-token' https://mygitlab.domain/api/v4/projects/229/pipelines/latest",
returnStatus: true)//.trim()
def status = sh(script: """
echo "'$pipelineStatus' | jq -r '.status'", returnStatus: true)//.trim()
if (status == 'success') {
echo "Pipeline succeeded"
} else {
echo "Pipeline did not succeed"
}
}
}
}
字符串
当使用.trim()时,我得到这个错误(所以我注解它):
Also: org.jenkinsci.plugins.workflow.actions.ErrorAction$ErrorId: 69e61b29-b180-44e9-a18f-b06b1a84f99a
10:36:18 hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: java.lang.Integer.trim() is applicable for argument types: () values: []
10:36:18 Possible solutions: wait(), grep(), wait(long), print(java.lang.Object), print(java.io.PrintWriter), div(java.lang.Number)
型
我可以使用curl命令从gitlab中检索输出结果,如下所示:
{"id":1664,"iid":19,"project_id":229,"sha":"9e70fd49d14e86ccd6091addf394ded51d22ab50","ref":"main","status":"success","source":"trigger","created_at":"2023-11-06T15:41:36.238Z","updated_at":"2023-11-06T15:41:38.854Z","web_url":"https://mygitlab.domain/test-ci/-/pipelines/1664","before_sha":"0000000000000000000000000000000000000000","tag":false,"yaml_errors":null,"user":{"id":98,"username":"907802","name":"myname","state":"active","locked":false,"avatar_url":"https://mygitlab.domain/uploads/-/system/user/avatar/98/avatar.png","web_url":"https://mygitlab.domain/907802"},"started_at":"2023-11-06T15:41:37.695Z","finished_at":"2023-11-06T15:41:38.841Z","committed_at":null,"duration":1,"queued_duration":1,"coverage":null,"detailed_status":{"icon":"status_success","text":"Passed","label":"passed","group":"success","tooltip":"passed","has_details":false,"details_path":"/907802/test-ci/-/pipelines/1664","illustration":null,"favicon":"/assets/ci_favicons/favicon_status_success-8451333011eee8ce9f2ab25dc487fe24a8758c694827a582f17f42b0a90446a2.png"},"name":null}
型
但是我只想得到“status”:“success”部分来做一个测试,如果status=success,那么我执行下一个jenkins管道阶段。但是我用“jq”做的不工作,我认为“echo“$pipelineStatus”等于“0”!
这就是错误:
10:36:18 [Pipeline] sh
10:36:18 + echo 0
10:36:18 + jq -r status
10:36:18 jq: error: status/0 is not defined at <top-level>, line 1:
10:36:18 status
10:36:18 jq: 1 compile error
型
我如何正确地做到这一点,我采取了所有工作解决方案,目标是为我的第一个curl命令提供一个报告,以了解我的gitlab ci管道是否正确执行以进入下一阶段。
1条答案
按热度按时间wfveoks01#
由于您的响应来自JSON格式的API,最简单的方法是解析响应,并获取
status
键的值。为此,您需要将Pipeline Utility Steps插件安装到Jenkins。字符串