一尘不染

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

jenkins

我想卷曲一个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中进行哪些更改


阅读 243

收藏
2020-07-25

共1个答案

一尘不染

如果要从shstep 返回输出并将其捕获到变量中,则必须更改:

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

2020-07-25