Pass groovy variable to shell script

2019-02-14 12:22发布

I just started learning groovy.I want to pass the svnSourcePath and svnDestPath to shell script in the svn copy command. But URL not rendered.

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

Output

+ 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

4条回答
时光不老,我们不散
2楼-- · 2019-02-14 12:48

added the single quotes and plus operater('+ variable +') around the variable. Now it is working

svn copy '''+svnSourcePath+' '+svnDestPath+''' -m 'promote dev to test' --username $SVN_USER --password $SVN_PWD '''
查看更多
狗以群分
3楼-- · 2019-02-14 12:49
def my_var = "hai"
sh (
    script:  "echo " + my_var,
    returnStdout: true
)
查看更多
别忘想泡老子
4楼-- · 2019-02-14 12:50

+1 to Selvam answer

following is my use case with parameter plugin

String parameter name: pipelineParameter

Default value: 4

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

The above prints 4 abcd

查看更多
forever°为你锁心
5楼-- · 2019-02-14 12:53

You can use """ content $var """. """ allows string interpolation in the here doc; ''' does not.

查看更多
登录 后发表回答