jenkins构建任务中显示每个步骤的时间可以使用timestamp插件,参考文章:Jenkins关于显示时间

timestamp插件只是在console中打印每个步骤的时间,如何在stage中使用该时间变量呢?此时就可以使用另外一个插件:**Build Timestamp**

Build Timestamp

该插件默认提供一个timestamps到env中,此变量是:BUILD_TIMESTAMP,该变量代表任务build开始时间,在pipeline中可以这样使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
pipeline {
agent any
stages {
stage('Hello') {
steps {
timestamps {
echo 'Hello World'
}
}
}
stage('Print env') {
steps {
echo 'Hello World'
}
}
stage('Print Build Timestamp') {
steps {
echo "build start time: ${BUILD_TIMESTAMP}"
}
}
}
}

对于console输出如下

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
29
30
31
Started by user cicd-admin
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/hello-world
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello)
[Pipeline] timestamps
[Pipeline] {
[Pipeline] echo
19:48:09 Hello World
[Pipeline] }
[Pipeline] // timestamps
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Print env)
[Pipeline] echo
Hello World
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Print Build Timestamp)
[Pipeline] echo
build start time: 2019-06-23 19:48:08 CST
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

相应的时间变量格式也可以修改,在系统管理》configure system中:

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
29
30
31
32
Pattern Syntax
SymbolMeaningPresentationExample
Gera designatorTextAD
yyearNumber2009
Mmonth in yearText & NumberJuly & 07
dday in monthNumber10
hhour in am/pm (1-12)Number12
Hhour in day (0-23)Number0
mminute in hourNumber30
ssecond in minuteNumber55
SmillisecondNumber978
Eday in weekTextTuesday
Dday in yearNumber189
Fday of week in monthNumber2 (2nd Wed in July)
wweek in yearNumber27
Wweek in monthNumber2
aam/pm markerTextPM
khour in day (1-24)Number24
Khour in am/pm (0-11)Number0
zGeneral time zoneTextPacific Standard Time; PST; GMT-08:00
ZRFC 822 time zoneText-0800
'escape for textDelimiter(none)
'single quoteLiteral' Samples
PatternOutput
dd.MM.yy30.06.09
yyyy.MM.dd G 'at' hh:mm:ss z2009.06.30 AD at 08:29:36 PDT
EEE, MMM d, ''yyTue, Jun 30, '09
h:mm a8:29 PM
H:mm8:29
H:mm:ss:SSS8:28:36:249
K:mm a,z8:29 AM,PDT
yyyy.MMMMM.dd GGG hh:mm aaa2009.June.30 AD 08:29 AM

同时可以新增时间变量,比如前24小时的时间:

1
2
3
4
5
6
stage('Print Build Timestamp') {
steps {
echo "build start time: ${BUILD_TIMESTAMP}"
echo "build start time: ${BUILD_TIMESTAMP_YESTERDAY}"
}
}

console输出

1
2
3
4
5
6
7
[Pipeline] { (Print Build Timestamp)
[Pipeline] echo
build start time: 2019-06-23 20:01:04 CST
[Pipeline] echo
build start time: 2019-06-22 20:01:04 CST
[Pipeline] }
[Pipeline] // stage

最后提供下不用插件的方式获取时间变量

1.使用shell

1
2
3
environment {
SHELL_BUILD_TIMESTAMP = """${sh(returnStdout: true,script: 'date +%Y%m%d%H%M%S').trim()}"""
}

2.使用groovy内部对象构造一个方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def createTimestamp() {
return new Date().format('yyyyMMddHHmmss')
}
pipeline {
agent any
// environment中引入上边定义的函数,以便于全局调用
environment {
MY_BUILD_TIMESTAMP = createTimestamp()
}
stages {
stage ("test") {
steps {
echo "${MY_BUILD_TIMESTAMP}"
}
}
}
}

也不一定要在全局变量中才用,只需要调用该方法就可以创建一个时间变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def createTimestamp() {
return new Date().format('yyyyMMddHHmmss')
}
pipeline {
agent any
// environment中引入上边定义的函数,则该任务全局可用
environment {
MY_BUILD_TIMESTAMP = createTimestamp()
}
stages {
stage ("test") {
steps {
echo "${MY_BUILD_TIMESTAMP}"
sleep time: 20, unit: 'SECONDS'
script {
SCRIPT_TIMESTAMP2 = createTimestamp()
echo "${SCRIPT_TIMESTAMP2}"
}
}
}
}
}

相应的console输出

1
2
3
4
5
6
7
8
9
10
11
12
[Pipeline] stage
[Pipeline] { (test)
[Pipeline] echo
20190623204937
[Pipeline] sleep
Sleeping for 20 sec
[Pipeline] script
[Pipeline] {
[Pipeline] echo
20190623204957
[Pipeline] }
[Pipeline] // script