我如何从Jenkins获取最新构建的构建时间戳? 我想在后续构建操作中将该值插入电子邮件主题中。
我如何从Jenkins获取最新构建的构建时间戳? 我想在后续构建操作中将该值插入电子邮件主题中。
构建时间戳插件将是获取构建过程中的时间戳
的最佳方法。
按照以下简单步骤启用"BUILD_TIMESTAMP"
变量。
步骤1:
Manage Jenkins > Plugin Manager > Available plugins (or Installed plugins)...
Search for "Build Timestamp".
Install with or without Restart.
第二步:
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..
您可以通过导航到https://<your-jenkins>/env-vars.html
来查看构建期间可用的所有全局环境变量。
将 https://<your-jenkins>/
替换为您用于访问Jenkins网页的URL(例如,它可能是 http://localhost:8080/env-vars.html
)。
其中一个环境变量是:
BUILD_ID
The current build id, such as "2005-08-22_23-59-59" (YYYY-MM-DD_hh-mm-ss)
如果您使用Jenkins可编辑的电子邮件通知,您应该能够在电子邮件主题行中使用${ENV, var="BUILD_ID"}
。env-vars.html
中可以看到:“_BUILD_ID当前的构建ID与1.597+中创建的构建的BUILD_NUMBER相同,但对于旧的构建,则为YYYY-MM-DD_hh-mm-ss时间戳_”。 - FrontierPsychoenv-vars.html
输出?我的仍然显示YYYY-MM-DD_hh-mm-ss格式(在德语中,Jenkins版本为2.7)。关于env-vars及其文档似乎存在一些混淆... - OK.env-vars.html
页面的翻译已经过时了。如果我将浏览器默认语言设置为英语,我会得到与您相同的输出结果。 - OK.一种方法是在全局环境部分使用shell脚本,这里,我使用UNIX时间戳,但您可以使用任何与shell脚本语法兼容的时间格式:
pipeline {
agent any
environment {
def BUILDVERSION = sh(script: "echo `date +%s`", returnStdout: true).trim()
}
stages {
stage("Awesome Stage") {
steps {
echo "Current build version :: $BUILDVERSION"
}
}
}
}
Build Timestamp Plugin
并使用 BUILD_TIMESTAMP
变量。从脚本(Unix脚本)生成环境变量:
echo "BUILD_DATE=$(date +%F-%T)"
环境变量名称:BUILD_DATE
版本号格式化字符串:${BUILD_DATE_FORMATTED}
Jenkins.getInstance().getItemByFullName(<your_job_name>).getBuildByNumber(<your_build_number>).getTime()
这里也回答了与it技术有关的问题: https://stackoverflow.com/a/63074829/1968948
BUILD_ID
用于提供此信息,但自 Jenkins 1.597 起,它已更改为提供构建编号。有关更多信息,请参见 this。
您可以使用其他答案中指出的 Build Time Stamp 插件来实现此目的。
但是,如果您不允许或不愿意使用插件,请按照以下方法操作:
def BUILD_TIMESTAMP = null
withCredentials([usernamePassword(credentialsId: 'JenkinsCredentials', passwordVariable: 'JENKINS_PASSWORD', usernameVariable: 'JENKINS_USERNAME')]) {
sh(script: "curl https://${JENKINS_USERNAME}:${JENKINS_PASSWORD}@<JENKINS_URL>/job/<JOB_NAME>/lastBuild/buildTimestamp", returnStdout: true).trim();
}
println BUILD_TIMESTAMP
withCredentials
语句中传递,代替'JenkinsCredentials'
。如果你的Jenkins不使用身份验证,可以随意省略此步骤。 /**
* Puts a hidden input field to the form so that the form submission will have the crumb value
*/
appendToForm : function(form) {
// add here. ..... you code
if(this.fieldName==null) return; // noop
var div = document.createElement("div");
div.innerHTML = "<input type=hidden name='"+this.fieldName+"' value='"+this.value+"'>";
form.appendChild(div);
}