如何将文本追加到jenkinsfile中的文件 [英] How to append a text to a file in jenkinsfile

查看:284
本文介绍了如何将文本追加到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

请注意文件位于当前目录

Note the file is in the current directory

推荐答案

env.BUILD_ID是一个常规变量,而不是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"

或使用外壳程序知道的变量

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屋!

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