如何将卷曲响应捕获到 Jenkinsfile 中的变量中

How catch curl response into variable in Jenkinsfile

我想卷曲一个 URL 并将响应捕获到一个变量中。

当我卷曲一个命令并回显它的输出时,我得到如下正确的响应

sh 'output=`curl https://some-host/some-service/getApi?apikey=someKey`;echo $output;'

我想将相同的响应捕获到一个变量中并使用该响应进行进一步的操作

下面是我的 Jenkinsfile

pipeline {
    agent {
          label "build_2"
       }
    stages {
        stage('Build') {
            steps {
                checkout scm
                sh 'npm install'

            }
        }
        stage('Build-Image') {
            steps {
                echo '..........................Building Image..........................'

                //In below line I am getting Output
                //sh 'output=`curl https://some-host/some-service/getApi?apikey=someKey`;echo $output;'

                script {
                    //I want to get the same response here
                    def response = sh 'curl https://some-host/some-service/getApi?apikey=someKey'
                    echo '=========================Response===================' + response
                }
            }
        }
    }
}

你能告诉我我需要在我的 Jenkinsfile 中做哪些更改吗

如果您想要 return 来自 sh 步骤的输出并将其捕获到您必须更改的变量中:

def response = sh 'curl https://some-host/some-service/getApi?apikey=someKey'

至:

def response = sh(script: 'curl https://some-host/some-service/getApi?apikey=someKey', returnStdout: true)

参考:https://jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#sh-shell-script