Jenkins中使用时间变量

1,054次阅读
没有评论

共计 3016 个字符,预计需要花费 8 分钟才能阅读完成。

Jenkins中使用时间变量

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

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

Build Timestamp

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

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输出如下

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中:

Jenkins中使用时间变量
Pattern Syntax
Symbol	Meaning	Presentation	Example
G	era designator	Text	AD
y	year	Number	2009
M	month in year	Text & Number	July & 07
d	day in month	Number	10
h	hour in am/pm (1-12)	Number	12
H	hour in day (0-23)	Number	0
m	minute in hour	Number	30
s	second in minute	Number	55
S	millisecond	Number	978
E	day in week	Text	Tuesday
D	day in year	Number	189
F	day of week in month	Number	2 (2nd Wed in July)
w	week in year	Number	27
W	week in month	Number	2
a	am/pm marker	Text	PM
k	hour in day (1-24)	Number	24
K	hour in am/pm (0-11)	Number	0
z	General time zone	Text	Pacific Standard Time; PST; GMT-08:00
Z	RFC 822 time zone	Text	-0800
'	escape for text	Delimiter	(none)
'	single quote	Literal	' Samples
Pattern	Output
dd.MM.yy	30.06.09
yyyy.MM.dd G 'at' hh:mm:ss z	2009.06.30 AD at 08:29:36 PDT
EEE, MMM d, ''yy	Tue, Jun 30, '09
h:mm a	8:29 PM
H:mm	8:29
H:mm:ss:SSS	8:28:36:249
K:mm a,z	8:29 AM,PDT
yyyy.MMMMM.dd GGG hh:mm aaa	2009.June.30 AD 08:29 AM

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

Jenkins中使用时间变量
        stage('Print Build Timestamp') {
            steps {
                echo "build start time: ${BUILD_TIMESTAMP}"
                echo "build start time: ${BUILD_TIMESTAMP_YESTERDAY}"
            }
        }

console输出

[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

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

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

def createTimestamp() {
    return new Date().format('yyyyMMddHHmmss')
}
pipeline {
    agent any
    // environment中引入上边定义的函数,以便于全局调用
    environment {
        MY_BUILD_TIMESTAMP = createTimestamp()
    }
    stages {
        stage ("test") {
            steps {
                echo "${MY_BUILD_TIMESTAMP}"
            }
        }
    }
}

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

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输出

[Pipeline] stage
[Pipeline] { (test)
[Pipeline] echo
20190623204937
[Pipeline] sleep
Sleeping for 20 sec
[Pipeline] script
[Pipeline] {
[Pipeline] echo
20190623204957
[Pipeline] }
[Pipeline] // script

正文完
 
xadocker
版权声明:本站原创文章,由 xadocker 2019-06-23发表,共计3016字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)