在jenkins管道阶段使用curl获取gitlab-ci管道执行状态

问题描述 投票:0回答:1

在我的詹金斯管道中,我有一个阶段应该使用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 那么我执行下一个詹金斯管道阶段。但我用“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管道是否正确执行以进入下一阶段。 提前非常感谢您

jenkins curl jq gitlab-ci-runner
1个回答
0
投票

当您从 api 获得 JSON 格式的响应时,最简单的方法是解析响应,并获取

status
键的值。为此,您需要将 Pipeline Utility Steps 插件安装到 Jenkins。

stage('Get Pipeline Status') {
    steps {
        script {
            // Run a curl command to get pipeline details from GitLab
            def pipelineResponse = sh(
                script: "curl --insecure --header 'PRIVATE-TOKEN: my-token' https://mygitlab.domain/api/v4/projects/229/pipelines/latest",
                returnStatus: true)

            def jsonResponse = readJSON text: pipelineResponse
            def status = jsonResponse['status']

            if (status == 'success') {
                echo "Pipeline succeeded"
            } else {
                echo "Pipeline did not succeed"
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.