【问题标题】:Pass groovy variable to shell script将 groovy 变量传递给 shell 脚本
【发布时间】:2017-05-24 00:37:33
【问题描述】:

我刚开始学习 groovy。我想在 svn copy 命令中将 svnSourcePath 和 svnDestPath 传递给 shell 脚本。但 URL 未呈现。

node {
 stage 'Copy Svn code'

def svnSourcePath = "${svnBaseURL}${svnAppCode}${svnEnvDev}${SVN_DEV_PACKAGE}"
def svnDestPath = "${svnBaseURL}${svnAppCode}${svnEnvTest}${SVN_DEV_PACKAGE}"

print "DEBUG: svnSourcePath = ${svnSourcePath}"
print "DEBUG: svnDestPath = ${svnDestPath}"

withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: crendentialsIdSVN, passwordVariable: 'SVN_PWD', usernameVariable: 'SVN_USER']]) {
    sh '''  
    svn copy $svnSourcePath $svnDestPath -m 'promote dev to test' --username $SVN_USER --password $SVN_PWD '''
}  
}

输出

+ svn copy -m 'promote dev to test' --username techuser --password 'xxxyyy' 
     svn: E205001: Try 'svn help' for more info
     svn: E205001: Not enough arguments provided

【问题讨论】:

标签: shell jenkins groovy cloudbees


【解决方案1】:

在变量周围添加了单引号和加号 operator('+ variable +')。现在它正在工作

svn copy '''+svnSourcePath+' '+svnDestPath+''' -m 'promote dev to test' --username $SVN_USER --password $SVN_PWD '''

【讨论】:

    【解决方案2】:

    您可以使用""" content $var """""" 允许在此处的文档中进行字符串插值; ''' 没有。

    【讨论】:

      【解决方案3】:

      +1 对 Selvam 的回答

      以下是我使用参数插件的用例

      字符串参数名称:pipelineParameter

      默认值:4

      node {
        stage('test') {
              withCredentials([[...]]) {
                def pipelineValue = "${pipelineParameter}"  //declare the parameter in groovy and use it in shellscript
                sh '''
                   echo '''+pipelineValue+' abcd''''
                   '''
              }
      }}
      

      上面打印 4 abcd

      【讨论】:

        【解决方案4】:

        双引号也只能用一次

        stage('test') {  
          steps {  
            script {  
              for(job in env.JOB_NAMES.split(',')) {  
                println(job);  
                sh "bash jenkins/script.sh $job"  
                sh "echo $job"  
              }  
            }//end of script  
          }//end of steps  
        }//end of stage test
        

        【讨论】:

          【解决方案5】:

          我在寻找一种在sh 命令中插入变量值的方法时遇到了这个问题。

          单引号 'string' 和三单引号 '''string''' 字符串都不支持插值。

          According to Groovy documentation:

          单引号字符串是纯 java.lang.String 并且不支持 插值。

          三单引号字符串是纯 java.lang.String 并且不 支持插值。

          所以要在 groovy (GString) 中使用嵌入的字符串值,必须使用双引号,其中的任何 GString 都将被计算,即使它位于单引号字符串中。

              sh "git commit -m  'Build-Server: ${server}', during main build."
          

          【讨论】:

            【解决方案6】:

            如果需要 bash 脚本,您需要执行以下操作:

            在全局或本地(函数)级别设置此变量,其中 sh 脚本可以访问这些变量:

            def stageOneWorkSpace = "/path/test1"
            def stageTwoWorkSpace = "/path/test2"
            

            在 shell 脚本中像下面这样调用它们

            sh '''
            echo ''' +stageOneWorkSpace+ '''
            echo ''' +stageTwoWorkSpace+ '''
            cd ''' +stageOneWorkSpace+  '''
            rm -r ''' +stageOneWorkSpace+ '''/AllResults
            mkdir -p AllResults
            mkdir -p AllResults/test1
            mkdir -p AllResults/test2
            cp -r ''' +stageOneWorkSpace+'''/qa/results/* ''' +stageOneWorkSpace+'''/AllResults/test1
            cp -r ''' +stageTwoWorkSpace+'''/qa/results/* ''' +stageOneWorkSpace+'''/AllResults/test2
            '''
            

            【讨论】:

              【解决方案7】:
              def my_var = "hai"
              sh (
                  script:  "echo " + my_var,
                  returnStdout: true
              )
              

              【讨论】: