Modify Jenkins description for assembly

I would like to remotely modify the Jenkins build description. I have my script whole set and ready, with the exception of one tiny problem: a few line descriptions.

I use the REST API and JSON in Jenkins to load 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 extends:

 <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 it easier to read above. It pulls out as one line).

\r\n are single lines, so I do this:

 old_description=$(sed 's/\\r\\n/\ /g' <<<$old_description) 

And that will change $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: Newlines are part of the value. This is a three-line description.)

My program (depending on the command line parameters) can replace, add or add a new description to the assembly:

 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 will 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 add or add a new description for 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 good. Next time it will not work. I get the following:

 <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 mapping \n .

How can I fix this problem?

I put the whole program in pastebin .

+1
source share
1 answer

I played with this for a long time ...

First, instead:

  new_description="$new_description<br/> $old_description" 

add or add a line, I used printf :

 new_description="$(printf "$new_description\r\n$old_description")" 

Using printf , I put <CR><LF> instead of just <LF> in my description line separator. Thus, I have no mess <NL> and <CR><NL> , and I am no longer dependent on the definition of the operating system line break.

The sed team took a long time to understand. I tried all kinds of things:

 old_description=$(sed 's/\\r\\n/\r\n/g' <<<$old_description) 

But nothing worked ... I tried the -E flag, which allows me to use extended regular expressions, but it continued to interpret \r\n as replacing \\r\\n with literal 'rn .

After a few hours, I finally tried double quotes instead of single quotes:

 old_description=$(sed "s/\\r\\n/\r\n/g" <<<$old_description) 

It worked! Usually you use single quotes with sed to protect the regular expression from interpolation. However, single quotes also killed the \r\n interpolation as <CR><LF> . Changing them with double quotes solved the problem.

+2
source

Source: https://habr.com/ru/post/982454/


All Articles