问题
I would like to remotely change a Jenkins build description. I have my script all set and ready except for one tiny problem: Multiple line descriptions.
I am using the REST API and JSON in Jenkins to download the old description:
old_description=$(curl -s --user "$USER:$PASSWORD" --data-urlencode "tree=description" \
"$jenkins_url/job/$job_name/$build_number/api/json")
old_description=${old_description#*:\"} #Remove JSON garbage
old_description=${old_description%\"\}} #Remove JSON garbage
The `curl command pulls out:
<font color=blue><b>At first you don't succeed. Try again</b></font><br/>
\r\n<font color=gold><b>At first you don't succeed. Try again</b></font><br/>
\r\n<font color=green><b>At first you don't succeed. Try again</b></font>
(Note: I added line breaks to make the above easier to read. This is pulled out as a single line).
The \r\n
are separate lines, so I do this:
old_description=$(sed 's/\\r\\n/\
/g' <<<$old_description)
And that changes $old_description
to:
font color=blue><b>At first you don't succeed. Try again</b></font><br/>
<font color=gold><b>At first you don't succeed. Try again</b></font><br/>
<font color=green><b>At first you don't succeed. Try again</b></font>
(NOTE: The new lines are part of the value. This is a three line description.)
My program (depending upon the command line parameters) can replace, append, or prepend a new description to the build:
if [ "$prepend_flag" -a -n "$old_description" ] #Prepend new description to old description
then
new_description="$new_description<br/>
$old_description"
elif [ "$append_flag" -a -n "$old_description" ] #Append new description to old description
then
new_description="$old_description<br/>
$new_description"
fi
Now, I'll redo the description:
if curl -u $USER:$PASSWORD --data-urlencode "description=$new_description" \
--data-urlencode "Submit=Submit" \
"$jenkins_url/job/$job_name/$build_number/submitDescription"
then
echo "Description successfully changed on Build #$build_number in Jenkins job $job_name"
else
echo "WARNING: Description was not set. Manually change the descripiton of the build"
echo " for Build #$build_number in Jenkins job $job_name"
fi
If I am prepending or appending the new description the first time, I get this in Jenkins:
<font color=blue><b>At first you don't succeed. Try again</b></font><br/>
<font color=gold><b>At first you don't succeed. Try again</b></font><br/>
<font color=green><b>At first you don't succeed. Try again</b></font><br/>
<font color=red><b>My new description</b></font><br/>
Looks nice. The next time, it doesn't work. I get this:
<font color=blue><b>At first you don't succeed. Try again</b></font><br/>\n<font color=gold><b>At first you don't succeed. Try again</b></font><br/>\n<font color=green><b>At first you don't succeed. Try again</b></font><br/>\n<font color=red><b>My new description</b></font><br/>
<font color=blue><b>My new new description</b></font>
Note the \n
showing up.
How can I fix this issue?
I've put the whole program in pastebin.
回答1:
I played around with this for a long while...
First, instead of doing this:
new_description="$new_description<br/>
$old_description"
to append or prepend the line, I used printf
:
new_description="$(printf "$new_description\r\n$old_description")"
By using printf
, I put a <CR><LF>
instead of just a <LF>
in my description line separator. This way, I don't have a jumble of <NL>
and <CR><NL>
and I'm no longer dependent upon the operating system's definition of the line break.
The sed
command took me a long, long time to figure out. I tried all sorts of things:
old_description=$(sed 's/\\r\\n/\r\n/g' <<<$old_description)
But, nothing seemed to work... I tried the -E
flag which allows me to use the extended regular expressions, but it kept interpreting \r\n
as replacing \\r\\n
with literal 'rn
.
After several hours of this, I finally tried double quotation marks instead of single quotation marks:
old_description=$(sed "s/\\r\\n/\r\n/g" <<<$old_description)
That worked! You normally use single quotation marks with sed to protect the regular expression from interpolation. However, the single quotes were also killing the interpolation of \r\n
as <CR><LF>
. Changing them with double quotes solved the problem.
来源:https://stackoverflow.com/questions/17839258/modifying-jenkins-description-for-a-build