在Jenkins Groovy中执行'curl'时出错

时间:2019-02-14 15:50:55

标签: jenkins jenkins-groovy

我正在尝试使用Groovy和curl通过其REST API创建ServiceNow更改票证。每次运行Jenkins管道时,都会出现以下错误

{“错误”:{“消息”:“读取请求时发生异常”,“详细信息”:“无法解码:java.io.StringReader@90e4d8”},“状态”:“失败”}

我在这里做错了什么? Jenkins版本2.150.2

    {
    node(){
        stage ('Create Change Request') {
            echo("Creating Change Request")

            sh(script: """curl  ${SERVICENOW_URL}/table/change_request \
              --request POST \
              --header 'Accept:application/json' \
              --header 'Content-Type:application/json' \
              --data '{"requested_by": "${params.requested_by}",
                        "u_verifier":"${params.u_verifier}",
                        "assigned_to":"${params.assigned_to}",
                        "reason":"${params.reason}",
                        "type":"${params.type}",
                        "start_date":"${params.start_date}",
                        "end_date":"${params.end_date}",
                        "change_plan":"${params.change_plan}",
                        "short_description":"${SHORT_DESCRIPTION}",
                        "description":"${DESCRIPTION}",
                        "backout_plan":"${BACKOUT_PLAN}",
                        "u_verification_plan":"${U_VERIFICATION_PLAN}",
                        "u_department_subsidiary":"${U_DEPARTMENT_SUBSIDIARY}",
                        "u_tested":"${U_TESTED}",
                        "u_have_verification_plan":"${U_HAVE_VERIFICATION_PLAN}",
                        "u_have_implementation_plan":"${U_HAVE_IMPLEMENTATION_PLAN}",
                        "u_have_backout_plan":"${U_HAVE_BACKOUT_PLAN}",
                        "assignment_group":"${U_ASSIGNMENT_GROUP}",
                        "category":"${CATEGORY}",
                        "cmdb_ci":"${CMDB_CI}",
                        "u_approval_group":"${U_APPROVAL_GROUP}",
                        "approval":"requested",
                        "state":"${_STATE}"
                        }' \
              --user 'xxxx':'password' > CREATE_CHG_REQUEST_OUTPUT
            """)
        }

        stage ('Parsing Change Result') {
            def REQUEST_OUTPUT = ""
            REQUEST_OUTPUT = readFile ('CREATE_CHG_REQUEST_OUTPUT').trim()
            //var jsonStr = JSON.stringify(REQUEST_OUTPUT);

            echo "REQUEST_OUTPUT:"
            echo REQUEST_OUTPUT

            def jsonSlurper = new JsonSlurper()
            def object = jsonSlurper.parseText(REQUEST_OUTPUT)

            NEW_CHANGE_NUMBER = object.result.number
            NEW_SYS_ID = object.result.sys_id

            echo("New Change Number is : " + NEW_CHANGE_NUMBER )
            echo("New sys_id for Change Number : " + NEW_SYS_ID )
        }

    }
} catch(e) {
    echo e.message
} finally {

}

1 个答案:

答案 0 :(得分:0)

数据具有使用声明的某些变量。在变量名中。 Groovy不支持替换双引号“”内声明的变量。用替换所有的变量名。用单引号引起来''。

希望如此有帮助