詹金斯管道是糟糕的替代品 [英] Jenkins Pipeline sh bad substitution
问题描述
我的管道中的一个步骤将.tar上传到artifactory服务器。传入env.BUILD_NUMBER时,我得到一个错误的替换错误,但是当硬编码时,相同的命令有效。
A step in my pipeline uploads a .tar to an artifactory server. I am getting a Bad substitution error when passing in env.BUILD_NUMBER, but the same commands works when the number is hard coded. The script is written in groovy through jenkins and is running in the jenkins workspace.
sh 'curl -v --user user:password --data-binary ${buildDir}package${env.BUILD_NUMBER}.tar -X PUT "http://artifactory.mydomain.com/artifactory/release-packages/package${env.BUILD_NUMBER}.tar"'
返回错误:
returns the errors:
[Pipeline] sh
[Package_Deploy_Pipeline] Running shell script
/var/lib/jenkins/workspace/Package_Deploy_Pipeline@tmp/durable-4c8b7958/script.sh: 2:
/var/lib/jenkins/workspace/Package_Deploy_Pipeline@tmp/durable-4c8b7958/script.sh: Bad substitution
[Pipeline] } //node
[Pipeline] Allocate node : End
[Pipeline] End of Pipeline
ERROR: script returned exit code 2
如果硬编码在内部版本号中换出 $ {env.BUILD_NUMBER}
我没有遇到任何错误,代码也能成功运行。
If hard code in a build number and swap out ${env.BUILD_NUMBER}
I get no errors and the code runs successfully.
sh 'curl -v --user user:password --data-binary ${buildDir}package113.tar -X PUT "http://artifactory.mydomain.com/artifactory/release-packages/package113.tar"'
我在同一个脚本的其他sh命令中使用$ {env.BUILD_NUMBER},并且在任何其他地方都没有问题。
I use ${env.BUILD_NUMBER} within other sh commands within the same script and have no issues in any other places.
推荐答案
原来这是一个语法问题。包装'
中的命令导致 $ {env.BUILD_NUMBER
被传递,而不是其值。我在 s中包装了整个命令,并且逃脱了嵌套。现在可以正常工作了。
This turned out to be a syntax issue. Wrapping the command in '
's caused ${env.BUILD_NUMBER
to be passed instead of its value. I wrapped the whole command in "
s and escaped the nested. Works fine now.
sh "curl -v --user user:password --data-binary ${buildDir}package${env.BUILD_NUMBER}.tar -X PUT \"http://artifactory.mydomain.com/artifactory/release-packages/package${env.BUILD_NUMBER}.tar\""
这篇关于詹金斯管道是糟糕的替代品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!