首页 > 解决方案 > Jenkins Echo 响应(Http 状态和正文)

问题描述

接收错误,script.sh: 1: Syntax error: "(" unexpected就行了def http_status。可能是什么问题?

   stage('alert') {
    withCredentials([string(credentialsId: 'org_user_key', variable: 'userKey'), string(credentialsId: 'org_api_key', variable: 'apiKey')]) {
        def response = sh(script: "curl -X POST \
                                --header 'Content-Type: application/json' \
                                --data '{\"requestType\" : \"getProductAlerts\",\"productToken\" : \"${productToken}\", \"userKey\" : \"${userKey}\"}' \
                                 'https://saas-eu.whitesourcesoftware.com/api/v1.3'", returnStdout: true).trim()

        def http_status = sh(script: "echo \"${response}\" | grep HTTP |  awk '{print \$2}", returnStdout: true)
        sh "echo  \"${http_status}\""
        def body = sh(script: "echo \"${response}\" | grep body", returnStdout: true)
        sh "echo  \"${body}\""
    }
}

标签: bashjenkinscurl

解决方案


我认为您在 awk 命令后缺少单引号:

def http_status = sh(script: "echo \"${response}\" | grep HTTP |  awk '{print \$2}", returnStdout: true)

相对

def http_status = sh(script: "echo \"${response}\" | grep HTTP |  awk '{print \$2}'", returnStdout: true)

推荐阅读