i found this commande in internet, to update image tag inside a file, can someone please explain this command please ?
sh """
sed -i 's|image: .*|image: ${ECR_URL}/${ECR_REPO_NAME}:${env.BUILD_NUMBER}|' ${env}/values.yaml
"""
Let's start decomposing this:
sh """
...
"""
That's for Groovy scripts to invoke multiline shells. If you're not writing a Groovy script, remove that.
The rest exhibits the beauty of sed:
sed -i ... ${env}/values.yaml
means the operation between single quotes will be case insensitive and will apply to the file ${env}/values.yaml
; -i
is short for --in-place
, which means it applies the changes to the file. ${env}
is defined somewhere else in the script and is likely the path where the file values.yaml
resides.
Now, for what it does on this file: the s
command is for "substitute" and the next character is the separator. It's most often a '/', sometimes a '%', rarely a '|' or a '@'. \7f
is a valid separator only used to explain how separators work in sed (but the sed formula must then be in double quotes).
The syntax of s
is s|searched_string|replaced_string|options
(replace searched_string
by replaced_string
in the entire file, once per line at most - using the pipe as the separator). You have no option in your snippet.
In image: .*
, .*
means any number of occurrences of any character. Basically, you're searching for a line containing "image: " followed by anything, including nothing: replace everything starting by image:
up to the end of the line. Note that the occurrence of image:
can be anywhere in a line, including after something else, it will match splash-image: splash.png
image: ${ECR_URL}/${ECR_REPO_NAME}:${env.BUILD_NUMBER}|'
is the replaced_string, as per above. ${ECR_URL}
, ${ECR_REPO_NAME}
and ${env.BUILDNUMBER}
are environment variables probably defined elsewhere in the Groovy script or by the Groovy engine. No witchcraft here, it doesn't even retain the original value of image:
(but that you could do with regular expressions and/or sed-scripts).
For more information, just type man sed
.