简体   繁体   English

在jenkinsfile中转义sed语句

[英]escaping sed-statement in jenkinsfile

I'm trying to script a pipeline in jenkins to build a docker image and deploy it via helm to kubernetes. 我正在尝试在jenkins中编写管道脚本以构建docker映像并将其通过掌舵部署到kubernetes。 the version number of the chart and docker-image is a SemVer with a shortform of the git commit-hash appended. 图表和docker-image的版本号是SemVer,并附加了git commit-hash的缩写。 the version is stored in the env VERSION and the commit-hash in the env GIT_COMMIT_SHORT . 版本存储在env VERSION ,提交哈希存储在env GIT_COMMIT_SHORT

I want to edit the Chart.yaml and values.yaml of the helm-chart with sed to match the tag of the docker-image and chart version, eg 2.7.1-9fa6sof8 . 我想用sed编辑helm-chart的Chart.yamlvalues.yaml以匹配2.7.1-9fa6sof8 -image和chart版本的标签,例如2.7.1-9fa6sof8

I've tried to put curly braces around the $VERSION and $GIT_COMMIT_SHORT but this didn't worked out. 我试图将花括号放在$VERSION$GIT_COMMIT_SHORT周围,​​但这没有解决。 I tried to escape the $ with \\ but then it won't interprete the envs. 我试图用\\来转义$ ,但是它不会解释envs。

The sed statement for editing the Chart.yaml : 用于编辑Chart.yaml的sed语句:

sh """sed -i 's/^version: .*\$/version: $VERSION-$GIT_COMMIT_SHORT/' $CHART_NAME/Chart.yaml"""

the error I see in jenkins is: 我在詹金斯看到的错误是:

sed -is/^version: .*$/version: 2.7.1-9fa6sof8 / $CHART_NAME/Chart.yaml sed -is / ^ version:。* $ / version:2.7.1-9fa6sof8 / $ CHART_NAME / Chart.yaml

sed: -e expression #1, char 39: unterminated `s' command sed:-e表达式#1,字符39:未终止的s命令

I need the statement with the correct escapes. 我需要带有正确转义符的语句。

You may use the sed command in triple single quotation mark like this: 您可以在三重单引号中使用sed命令,如下所示:

'''sed -i "s/^version: .*/version: $VERSION-$GIT_COMMIT_SHORT/" $CHART_NAME/Chart.yaml'''

Usually, to interpolate variables in a sed command, double quotation marks are used. 通常,要在sed命令中插值变量,请使用双引号。 When no quotes are used, all spaces should be quoted, and other changes might need adding (depending on the options, context, etc). 当不使用引号时,所有空格都应加引号,并且可能需要添加其他更改(取决于选项,上下文等)。

The .* matches all the rest of the line and the $ is redundant here. .*匹配行的所有其余部分,而$在这里是多余的。

try gnu sed 尝试gnu sed

 sed -Ei 's!^(version: ).*$!\12.7.1-9fa6sof8!' $CHART_NAME/Chart.yaml

i have no idea the real problem, may put up -i for test before use it in sh ".... 我不知道真正的问题,可能会在sh“ ....中使用它之前将-i进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM