如何在Jenkinsfile中将卷曲响应捕获到变量中 [英] How catch curl response into variable in Jenkinsfile

查看:128
本文介绍了如何在Jenkinsfile中将卷曲响应捕获到变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

下面是我的Jenkinsfile

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
                }
            }
        }
    }
}

能否请您告诉我我需要在Jenkinsfile中进行哪些更改

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

推荐答案

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

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'

收件人:

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

这篇关于如何在Jenkinsfile中将卷曲响应捕获到变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆