I am a Jenkins beginner.
Why does this command work?
sed -i -E s/'image: '(.*)${stack_name}-${service_name}:.*\$/'image: '\1${stack_name}-${service_name}:${version}/g
And why does the same command not work when it is included in a Jenkinsfile?
sh "sed -i -E s/'image: '(.*)${stack_name}-${service_name}:.*\$/'image: '\1${stack_name}-${service_name}:${version}/g"
The error is:
/opt/jenkins_data/workspace/secuview-front_master-Z2ADTSIGTSEJOG3UYRU4FPDUF5VZMB3SMQLEOUD46TUZG4POWKYQ#tmp/durable-a484faaf/script.sh: line 2: syntax error near unexpected token `('
Under the hood Jenkinsfiles are essentially Apache Groovy scripts, therefore string escaping rules for Groovy apply. When you have slashes they need to be escaped (e.g. \ -> \\) and when you're using double quotes using ${} literals actually get interpreted by the script instead of being passed to the shell step.
Try this instead:
sh 'sed -i -E s/\'image: \'\\(.*\\)${stack_name}-${service_name}:.*\\$/\'image: \'\\1${stack_name}-${service_name}:${version}/g'
Related
OK, this might be a silly question. I've got the test.json file:
{
"timestamp": 1234567890,
"report": "AgeReport"
}
What I want to do is to extract timestamp and report values and store them in some env variables:
export $(cat test.json | jq -r '#sh "TIMESTAMP=\(.timestamp) REPORT=\(.report)"')
and the result is:
echo $TIMESTAMP $REPORT
1234567890 'AgeReport'
The problem is that those single quotes break other commands.
How can I get rid of those single quotes?
NOTE: I'm gonna leave the accepted answer as is, but see #Inian's answer for a better solution.
Why make it convoluted with using eval and have a quoting mess? Rather simply emit the variables by joining them with NULL (\u0000) and read it back in the shell environment
{
IFS= read -r -d '' TIMESTAMP
IFS= read -r -d '' REPORT
} < <(jq -r '(.timestamp|tostring) + "\u0000" + .report + "\u0000"' test.json)
This makes your parsing more robust by making the fields joined by NULL delimiter, which can't be part of your string sequence.
From the jq man-page, the #sh command converts its input to be
escaped suitable for use in a command-line for a POSIX shell.
So, rather than attempting to splice the output of jq into the shell's export command which would require carefully removing some quoting, you can generate the entire commandline inside jq, and then execute it with eval:
eval "$(
cat test.json |\
jq -r '#sh "export TIMESTAMP=\(.timestamp) REPORT=\(.report)"'
)"
I have a Jenkins pipeline that I need to run a sed on a file but I am getting an error of line 2: syntax error: unexpected ")"
My file is this:-
name=""
age=""
My Jenkins sh line is:
"""sed -i -e 's|(name *= *")"|\1${params.NAME}"|g' -e 's|(age *= *")"|\1${params.AGE}"|g' vars.txt"""
I can run the sed on my shell fine and it works, but Jenkins doesn't like it for some reason.
If I run it through the Jenkins Pipeline Syntax Generator I get the same error.
You are using BRE POSIX pattern, and to create a capturing group there, you need to use escaped parentheses, \(...\). However, in the triple-quoted string literal, you need to escape the backslash to get a literal backslash in the resulting string.
You need to fix the line you have like this:
'''sed -i' ' -e 's|\\(name *= *"\\)"|\\1'"${params.NAME}"'"|g' -e 's|\\(age *= *"\\)"|\\1'"${params.AGE}"'"|g' vars.txt'''
When i do a sed in my jenkinsfile as
"sh "sed -i 's|HEAP=.*|HEAP="\-Xms1024m \-Xmx1024m"|' $DIR/bin/myfile"
i get the error as unexpected char :'\'.
How to specify special characters in a jenkinsfile??
Try using
"""sh sed -i 's|HEAP=.*|HEAP="\-Xms1024m \-Xmx1024m"|' $DIR/bin/myfile"""
Use the \ character to escape a character.
I'm doing a simple substitution, and this works fine at the command line:
sed "s/pub Url =.*/pub Url = 'https:\/\/example.com:3207';/g" myfile.ts
I'm trying to run it within a Jenkinsfile, and like 40 builds later I cannot get the escape quoting right.
Pretty sure it will look something like this:
sh 'sed \\"s/pub Url =.*/pub Url = \\'https:\\\/\\\/example.com:3207\\';/g\\" myfile.ts'
Yet that results in the following error:
WorkflowScript: 4: unexpected char: '\' # line 4, column 49.
ub Url =.*/pub Url = \\'https:\\\/\\\/ex
I feel like I've tried dozens of variants but nothing is working.
Here is among the most common errors I'm getting: which points to escaping issue
sed: -e expression #1, char 1: unknown command: `"'
I really just need a pipeline expert that can likely see exactly what I'm doing wrong and know where to quote it.
As noted here: https://gist.github.com/Faheetah/e11bd0315c34ed32e681616e41279ef4 this is not uncommon to fight this type of stuff in the pipeline files and it seems like it's just trial and error.
Ok after much trial and error, this is working.
Looks like I had to use triple single quotes around the command. Good thing I don't need to interpolate!
sh '''sed \"s/pub Url =.*/pub Url = \\'https:\\/\\/example.com:3207\\';/g\" afile.txt'''
Hope this is helpful to someone in the future that's fighting this!
To add more on this... I had an issue with sed -i 's/\_/\//g' abc.txt command running fine in CLI but resulting in an unxpected char \ error in jenkins, so, i had to replace this:
sed -i 's/\_/\//g' abc.txt
with
sed -i "s/\\_/\\//g" abc.txt
To remove this error from jenkins.
Can someone escape this sed shell command in Jenkins groovy script for me?
So hard.
sh ("""
sed "s/(AssemblyInformationalVersion\(\")(.*)(\")/\1${productVersion}\3/g"
AssemblyInfoGlobal/AssemblyInfoGlobal.cs -r
""")
The triple-double-quote (""") string literal syntax allows for variable/expression substitution (interpolation), so the backslash (\) is interpreted as a special character "escape". Since the first open paren is not such a special character, Groovy compilation fails. If your intent is to have literal backslashes in the resulting string, you need to escape the backslashes. That is, use a double-backslash (\\) to substitute for one literal backslash.
Thus:
sh ("""
sed "s/(AssemblyInformationalVersion\\(\\")(.*)(\\")/\\1${productVersion}\\3/g"
AssemblyInfoGlobal/AssemblyInfoGlobal.cs -r
""")
So if you like to replace some chars or word in an String groovy variable, for example replacing "/" with "/" in order to escape an special character, which in our case will be the forward slash you can use the code below.
So afterwards we'll be able to apply the linux sed command without getting an error (for instance using sed to replace a place holder value with the desired value in an .env file).
Below we show a Jenkins Pipeline Groovy code:
String s = "A/B/C"
your_variable = s.replace("/", "\\/")
sh "sed -i -e 's/string_to_replace/${your_variable}/g' path/to/file/.env"
NOTE: ${your_variable} will get the content of your variable.
VALIDATION:
echo "Your variable new content: ${your_variable}"
RESULT:
Your variable new content: A\/B\/C