简体   繁体   中英

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. the version number of the chart and docker-image is a SemVer with a shortform of the git commit-hash appended. the version is stored in the env VERSION and the commit-hash in the 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 .

I've tried to put curly braces around the $VERSION and $GIT_COMMIT_SHORT but this didn't worked out. I tried to escape the $ with \\ but then it won't interprete the envs.

The sed statement for editing the Chart.yaml :

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: -e expression #1, char 39: unterminated `s' command

I need the statement with the correct escapes.

You may use the sed command in triple single quotation mark like this:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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