如何将文本附加到 jenkinsfile 中的文件 [英] How to append a text to a file in jenkinsfile
问题描述
如何在 Jenkinsfile
注入 Jenkins BUILD_ID
How to append a text to a file in Jenkinsfile
injecting Jenkins BUILD_ID
我想看看
version := "1.0.25"
其中 25 是 BUILD_ID
这是我的尝试
import hudson.EnvVars
node {
stage('versioning'){
echo 'retrieve build version'
sh 'echo version := 1.0.${env.BUILD_ID} >> build.sbt'
}
}
错误:
版本:=1.0.${env.BUILD_ID}:替换错误
version:=1.0.${env.BUILD_ID}: bad substitution
注意文件在当前目录
推荐答案
env.BUILD_ID
是一个 groovy 变量,而不是 shell 变量.由于您使用单引号 ('
),groovy 将 not 替换字符串中的变量,并且 shell 不知道 ${env.BUILD_ID}代码>.您需要使用双引号
"
并让 groovy 进行替换
env.BUILD_ID
is a groovy variable, not a shell variable. Since you used single-quotes ('
) groovy will not substitute the variables in your string and the shell doesn't know about ${env.BUILD_ID}
. You need to either use double-quotes "
and let groovy do the substitution
sh "echo version := 1.0.${env.BUILD_ID} >> build.sbt"
或者使用shell知道的变量
or use the variable the shell knows
sh 'echo version := 1.0.$BUILD_ID >> build.sbt'
因为你需要用双引号括起来的版本,你需要这样的东西:
and since you need the version surrounded with doublequotes, you'd need something like this:
sh "echo version := \"1.0.${env.BUILD_ID}\" >> build.sbt"
这篇关于如何将文本附加到 jenkinsfile 中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!