Jenkins脚本化管道:无法在shell内打印变量并在shell中设置变量值 [英] Jenkins scripted pipeline: Unable to print variables inside shell and set variable values in shell

查看:459
本文介绍了Jenkins脚本化管道:无法在shell内打印变量并在shell中设置变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Jenkins脚本化管道.两个问题:

Jenkins scripted pipeline. Two issues:

  1. 我有一个全局变量var,我正在尝试访问其值 壳.但是它什么也没打印
  2. var的值是我在其中一个阶段使用a设置的 shell脚本,可以在下一个阶段访问,但是它在shell中什么也不打印.
  1. I have a global variable var whose value i am trying to access inside shell. But it prints nothing
  2. The value of var i am setting in one of the stages using a shell-script, to access in next stage but it prints nothing in shell.

我想念什么? (请参见下面的脚本)

What am i missing? (See script below)

node {    
    var=10
    stage('HelloWorld') {
        sh '''
              echo "Hello World. Var=$var"  ===> Prints nothing for var
              var=20'''

    }
    stage('git clone') {
        echo "Cloning git. Var = $var"  ==> Prints 20, and not 10
        sh '''
          echo "Var in second stage is = $var"   ===> Doesnt print anything here. I need 20.
        '''
    }
}

推荐答案

1.将常规变量传递给shell

您的示例不起作用,因为您使用带单引号的字符串文字.摘自 Groovy手册(重点是我的):

1. Pass a groovy variable to shell

Your sample doesn't work because you are using a string literal with single quotation marks. From Groovy manual (emphasis mine):

任何Groovy表达式都可以插入所有字符串文字中, 除了三重单引号字符串.

Any Groovy expression can be interpolated in all string literals, apart from single and triple-single-quoted strings.

尝试一下:

sh "echo 'Hello World. Var=$var'"

或者这个:

sh """
    echo 'Hello World. Var=$var'
    echo 'More stuff'
"""        

2.从shell设置Groovy变量

您不能直接在Shell步骤中设置Groovy变量.从Groovy到Shell,这仅在一个方向上起作用.相反,您可以设置退出代码或将数据写入Groovy可以读取的stdout.

2. Set a Groovy variable from shell

You can't directly set a Groovy variable from a shell step. This only works in one direction from Groovy to shell. Instead you can set an exit code or write data to stdout which Groovy can read.

为参数returnStatus传递true并从shell脚本中设置退出代码,该代码将作为sh步骤的返回值.

Pass true for parameter returnStatus and set an exit code from the shell script which will be the return value of the sh step.

var = sh script: 'exit 42', returnStatus: true
echo "$var"   // prints 42

返回单个字符串

为参数returnStdout传递true并在shell脚本中使用echo来输出字符串数据.

Return a single string

Pass true for parameter returnStdout and use echo from shell script to output string data.

var = sh script: "echo 'the answer is 42'", returnStdout: true
echo "$var"   // prints "the answer is 42"  

返回结构化数据

为参数returnStdout传递true并从shell脚本中使用echo以JSON格式输出字符串数据.

Return structured data

Pass true for parameter returnStdout and use echo from shell script to output string data in JSON format.

使用在Groovy代码中解析JSON数据JsonSlurper .现在,您有了可以查询的常规Groovy对象.

Parse JSON data in Groovy code using JsonSlurper. Now you have a regular Groovy object that you can query.

def jsonStr = sh returnStdout: true, script: """
    echo '{
        "answer": 42,
        "question": "what is 6 times 7"
    }'
"""

def jsonData = new groovy.json.JsonSlurper().parseText( jsonStr ) 
echo "answer: $jsonData.answer"
echo "question: $jsonData.question"

这篇关于Jenkins脚本化管道:无法在shell内打印变量并在shell中设置变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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