14

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
Selvam Rajendran
  • 1,366
  • 5
  • 20
  • 38

7 Answers7

14

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 '''
Selvam Rajendran
  • 1,366
  • 5
  • 20
  • 38
6

+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

5

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

ablarg
  • 2,400
  • 1
  • 24
  • 32
1

You need to do something like below if a bash script is required :

Set this variable at global or local(function) level where from these can be accessible to sh script:

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

In shell script call them like below

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
'''
Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
0

only one time double quotes would also work

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
Sushil Kumar Sah
  • 1,042
  • 10
  • 13
0

I ran across this question when I was looking for a way to interpolate a variable value in an sh command.

Neither Single-quoted 'string' nor Triple-single-quoted '''string''' strings support interpolation.

According to Groovy documentation:

Single-quoted strings are plain java.lang.String and don’t support interpolation.

Triple-single-quoted strings are plain java.lang.String and don’t support interpolation.

So to use the embedded string values in groovy (GString) the double quotes must be used, any GString within it will be evaluated, even if it was inside a single-quoted string.

    sh "git commit -m  'Build-Server: ${server}', during main build."
Bakri Bitar
  • 1,543
  • 18
  • 29
-1
def my_var = "hai"
sh (
    script:  "echo " + my_var,
    returnStdout: true
)
storm_m2138
  • 2,281
  • 2
  • 20
  • 18