将 groovy 变量传递给 shell 脚本
Pass groovy variable to shell script
刚开始学习groovy.I想在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
在变量周围添加了单引号和加号运算符('+变量+')。现在正在运行
svn copy '''+svnSourcePath+' '+svnDestPath+''' -m 'promote dev to test' --username $SVN_USER --password $SVN_PWD '''
您可以使用 """ content $var """
。 """
允许在此处文档中进行字符串插值; '''
没有。
+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
def my_var = "hai"
sh (
script: "echo " + my_var,
returnStdout: true
)
只有一次双引号也可以工作
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
我 运行 在寻找在 sh
命令中插入变量值的方法时遇到了这个问题。
单引号 'string'
和三重单引号 '''string'''
字符串都不支持插值。
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.
所以要在 groovy (GString) 中使用嵌入的字符串值,必须使用双引号,其中的任何 GString 都将被计算,即使它在单引号字符串中.
sh "git commit -m 'Build-Server: ${server}', during main build."
如果需要 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
'''
刚开始学习groovy.I想在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
在变量周围添加了单引号和加号运算符('+变量+')。现在正在运行
svn copy '''+svnSourcePath+' '+svnDestPath+''' -m 'promote dev to test' --username $SVN_USER --password $SVN_PWD '''
您可以使用 """ content $var """
。 """
允许在此处文档中进行字符串插值; '''
没有。
+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
def my_var = "hai"
sh (
script: "echo " + my_var,
returnStdout: true
)
只有一次双引号也可以工作
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
我 运行 在寻找在 sh
命令中插入变量值的方法时遇到了这个问题。
单引号 'string'
和三重单引号 '''string'''
字符串都不支持插值。
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.
所以要在 groovy (GString) 中使用嵌入的字符串值,必须使用双引号,其中的任何 GString 都将被计算,即使它在单引号字符串中.
sh "git commit -m 'Build-Server: ${server}', during main build."
如果需要 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
'''