Jenkins:管道 sh 错误替换错误 [英] Jenkins: Pipeline sh bad substitution error

查看:9
本文介绍了Jenkins:管道 sh 错误替换错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的管道中的一个步骤将 .tar 上传到工件服务器.传入 env.BUILD_NUMBER 时出现错误替换错误,但是当数字被硬编码时,相同的命令有效.该脚本是通过jenkins 用groovy 编写的,在jenkins 工作区中运行.

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

返回错误:

[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 而不是其值.我将整个命令包装在 " 中并转义了嵌套.现在可以正常工作了.

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""

这篇关于Jenkins:管道 sh 错误替换错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆