Pass groovy variable to shell script
Asked Answered
C

7

15

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
Congratulation answered 9/1, 2017 at 17:11 Comment(2)
To the person who downvoted this question - what's the point of downvoting without giving an explanation?Discussion
#39982914Donatist
C
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 '''
Congratulation answered 11/1, 2017 at 17:43 Comment(0)
T
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

Taskwork answered 2/10, 2017 at 1:57 Comment(0)
I
5

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

Ilona answered 15/5, 2017 at 17:22 Comment(0)
D
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
'''
Donatist answered 16/3, 2021 at 15:38 Comment(0)
C
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
Compendious answered 21/5, 2020 at 11:0 Comment(0)
C
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."
Clemmieclemmons answered 30/12, 2020 at 14:43 Comment(0)
N
-1
def my_var = "hai"
sh (
    script:  "echo " + my_var,
    returnStdout: true
)
Nyasaland answered 18/12, 2018 at 21:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.