How catch curl response into variable in Jenkinsfile

↘锁芯ラ 提交于 2020-01-23 05:55:34

问题


I want to curl an URL and capture the response into a variable.

when I curl a command and echo its output I get the correct response as below

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

I want to catch the same response into a variable and use that response for further operation

Below is my 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
                }
            }
        }
    }
}

Can you please tell me what changes I need to do in my Jenkinsfile


回答1:


If you want to return an output from sh step and capture it in the variable you have to change:

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

to:

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

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



来源:https://stackoverflow.com/questions/51492967/how-catch-curl-response-into-variable-in-jenkinsfile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!