修改Jenkins构建的描述 [英] Modifying Jenkins Description for a build

查看:1249
本文介绍了修改Jenkins构建的描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想远程更改Jenkins构建说明。我有我的脚本所有设置和准备除了一个小问题:多行描述。

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.

我使用REST API和JSON在Jenkins下载旧的描述: / p>

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

\r\\\
是单独的行,因此我这样做:

The \r\n are separate lines, so I do this:

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

更改 $ old_description 到:

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

现在,我将重做描述:

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

如果我第一次在前面添加或附加新的描述,我在Jenkins中得到这个:

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>

注意 \\\

如何解决此问题?

我将整个程序放在 pastebin

推荐答案

这个很长时间...

首先,不要这样做:

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

添加或添加行,我使用 printf

to append or prepend the line, I used printf:

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

printf ,我放置一个< CR>< LF> ,而不是一个 ; LF> 在我的描述行分隔符。这样,我没有杂乱的< NL> < CR>< NL> 并且我不再依赖于操作系统对换行符的定义。

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.

sed 命令带走了我一个漫长,漫长的时间才弄明白。我尝试了各种各样的事情:

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)

似乎工作...我尝试了 -E 标志,它允许我使用扩展正则表达式,但它一直解释 \r\\\
替换为 \\r\\\\
literal'rn

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)

这工作!通常使用带单引号的sed来保护正则表达式免于插值。然而,单引号也杀死了 \r\\\
作为< CR>< LF> 。用双引号更改它们解决了这个问题。

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.

这篇关于修改Jenkins构建的描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆