Jenkins日志可读化修改

Jenkins

Posted by WS on July 15, 2021

背景

公司jenkins smoke test 每天日常运行中,但是在出错查看流程时日志繁琐,不易查找步骤,故简单优化pipeline使Jenkins日志更可读.

Pipeline脚本修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def echoBanner(def ... msgs) {
   echo createBanner(msgs)
}
def errorBanner(def ... msgs) {
   error(createBanner(msgs))
}
def createBanner(def ... msgs) {
   return """
       ===========================================
       ${msgFlatten(null, msgs).join("\n        ")}
       ===========================================
   """
}
// flatten function hack included in case Jenkins security
// is set to preclude calling Groovy flatten() static method
// NOTE: works well on all nested collections except a Map
def msgFlatten(def list, def msgs) {
   list = list ?: []
   if (!(msgs instanceof String) && !(msgs instanceof GString)) {
       msgs.each { msg ->
           list = msgFlatten(list, msg)
       }
   }
   else {
       list += msgs
   }
   return  list
}

在每个阶段起始处(或者在阶段中的特定位置),只需调用 echoBanner

1
echoBanner("Stage Flow", ["Checkout GitLab!"])

显示如下

===========================================
 Stage Flow
 Checkout GitLab!
===========================================

%