【问题标题】:How to pass a groovy variable to a shell block jenkins如何将 groovy 变量传递给 shell 块詹金斯
【发布时间】:2019-01-14 23:28:38
【问题描述】:

我有一个 groovy 变量,我想将它传递给 shell 块进行进一步处理,但我不断收到错误消息:

stages {      
    stage('First Stage - echo out available variables'){
        steps{

            script {
                def string_var = "im a groovy variable"
                echo "${string_var}"   // This will print "im a groovy variable" just fine
                sh """
                    echo """ + string_var + """
                """  // This will error

                sh """
                    echo ${string_var} 
                """  // This will error

                sh ''' echo '''+ string_var +''' '''

                sh "echo ${string_var}" // This will error
            }
        }
    }
}

我的错误:

an exception which occurred:
    in field com.cloudbees.groovy.cps.impl.BlockScopeEnv.locals
    in object com.cloudbees.groovy.cps.impl.BlockScopeEnv@1ecb37ba
    in field com.cloudbees.groovy.cps.impl.CallEnv.caller
    in object com.cloudbees.groovy.cps.impl.FunctionCallEnv@32070c3b
    in field com.cloudbees.groovy.cps.Continuable.e
    in object org.jenkinsci.plugins.workflow.cps.SandboxContinuable@641113f0

【问题讨论】:

  • 需要定义为sh """ echo string_var: ${string_var} """
  • sh方法的参数类型有哪些?您可能需要这样做才能立即评估 GString: sh "echo $string_var".toString()
  • @RohitMahajan 我试过了,同样的错误,在你发布的链接上,我尝试了额外的三倍“但仍然得到同样的错误。我将编辑上面的代码以显示我刚刚尝试过的内容.
  • 三双引号对我有用。你能试试sh ''' echo '''+ string_var +''' '''

标签: jenkins groovy


【解决方案1】:

我尝试使用您的管道(刚刚关闭 ''' 范围)并且一切正常:

pipeline {
    agent any
    stages {      
        stage('First Stage - echo out available variables'){
            steps{
                script {
                    def string_var = "im a groovy variable"
                    echo "${string_var}"

                    sh """
                        echo """ + string_var + """
                    """

                    sh """
                        echo ${string_var} 
                    """ 

                    sh ''' echo '''+ string_var +''' ''' // added '''

                    sh "echo ${string_var}"
                }
            }
        }
    }
}

此管道的输出:

[Pipeline] {
[Pipeline] stage
[Pipeline] { (First Stage - echo out available variables)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
im a groovy variable
[Pipeline] sh
+ echo im a groovy variable
im a groovy variable
[Pipeline] sh
+ echo im a groovy variable
im a groovy variable
[Pipeline] sh
+ echo im a groovy variable
im a groovy variable
[Pipeline] sh
+ echo im a groovy variable
im a groovy variable
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

所以,问题可能出在脚本的其他地方,或者您需要更新 Pipeline Plugin(我使用 2.6 版本)/Jenkins(我使用 2.150.1 版本)。

【讨论】:

  • 我使用管道插件:2.5 和 jenkins 2.107.1 知道您的版本有什么错误修复吗?
  • 目前还没有发现任何可能修复的错误(尝试在changelogs 中找到),但是您可以安装新版本的本地 Jenkins 并检查问题是否已解决。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-08-03
  • 1970-01-01
  • 1970-01-01
  • 2021-06-17
  • 2019-05-16
  • 1970-01-01
  • 2017-05-24
相关资源
最近更新 更多