在Jenkins中设置构建描述中使用构建时间戳

3

我已经安装了Description Setter插件,但是我不知道如何使用BUILD_ID。在jenkins/env-vars.html/中,它以“2005-08-22_23-59-59”(YYYY-MM-DD_hh-mm-ss)的格式显示。

有人知道我是否可以使用它以及如何使用吗?

如果无法显示BUILD_ID,则将其添加到构建描述中的最简单方法是什么?

谢谢!

4个回答

6
他们将${BUILD_ID}变量替换为构建编号,而不是时间戳(自1.597+以来)。请参见https://issues.jenkins-ci.org/browse/JENKINS-26520 还有一些其他插件的解决方法,比如EnvInject,或者你可以使用Description Setter插件的正则表达式功能,像这样:
  • 添加execute shell博客(适用于Linux)
  • 插入命令echo“日期:”$(date +'%Y-%m-%d_%H-%M-%S')
  • 将Description Setter插件设置为正则表达式date:(.*)
  • 将Description Setter插件设置为描述\ 1

是的,它可以这样工作,只需使用适用于Windows的批处理命令代替shell:@echo off for /F "usebackq tokens=1,2 delims==" %%i in (wmic os get LocalDateTime /VALUE 2^>NUL) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j set ldt=%ldt:0,4%-%ldt:4,2%-%ldt:6,2% %ldt:8,2%:%ldt:10,2%:%ldt:12,6% echo date: [%ldt%] - Maria

4
如果您使用的是最新版本的Jenkins(1.6xx),您需要安装ZenTimestamp插件并使用BUILD_TIMESTAMP变量:

enter image description here

enter image description here

您可以在全局 Jenkins 设置中自定义格式:

enter image description here

我的解决方案还演示了如何使用自定义链接作为描述。


你使用的是哪个Jenkins版本?我测试过1.643版本,变量$BUILD_ID包含的是构建编号而不再是日期。 - mszalbach
是的,我有1.602,对我来说它也显示构建号而不是日期... :( - Maria
我用了旧版本进行测试,我会更新我的帖子并提供新的解决方案 :) - Bruno Lavit
1
构建时间戳插件看起来比Zen时间戳插件更新鲜、更具特色: https://wiki.jenkins-ci.org/display/JENKINS/Build+Timestamp+Plugin - skiphoppy

2
"构建时间戳插件"是获取构建过程中的时间戳的最佳方法。按照以下简单步骤启用“BUILD_TIMESTAMP”变量。
步骤1:
    Manage Jenkins -> Plugin Manager -> Installed...
    Search for "Build Timestamp Plugin".
    Install with or without Restart.

STEP2:
第二步:
    Manage Jenkins -> Configure System.
    Search for 'Build Timestamp' section, then Enable the CHECKBOX.
    Select the TIMEZONE, TIME format you want to setup with..Save the Page.

使用方法:

    When Configuring the Build with ANT or MAVEN, 
    Please declare a Global variable as, 
    E.G.  btime=${BUILD_TIMESTAMP}
    (use this in your Properties box in ANT or MAVEN Build Section)

    use 'btime' in your Code to any String Variables etc..

enter image description here

enter image description here


0

您可以使用Groovy令牌,例如:

${GROOVY,script = "String.format('%tF %<tH:%<tM', java.time.LocalDateTime.now())"}

它将在构建描述中添加时间戳,例如:2021-12-05 13:29

请注意,build.getTimestampString2()也会打印时间戳,但是根据UTC(在我的时区中早两个小时):2021-12-05T11:29:09Z


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接