我已经安装了版本号插件,因为我想使用 BUILDS_TODAY。我希望它能给我今天特定管道的构建数量。我还没能让它发挥作用。使用:
echo "BUILDS_TODAY=${env.BUILDS_TODAY}"
产量:
BUILDS_TODAY=null
我是 Jenkins 新手,担心我可能需要做的不仅仅是安装插件? 如何使用该插件并获得 BUILDS_TODAY? 我尝试对此进行更多研究。我使用以下方法尝试打印所有变量。 [但也许这是 Windows 变量而不是 Jenkins 变量]:
if (isUnix()) {
sh 'printenv'
} else {
bat 'set'
}
这没有产生任何 BUILDS_TODAY。它确实产生了:
BUILD_DISPLAY_NAME=#1
BUILD_ID=1
BUILD_NUMBER=1
BUILD_TAG=jenkins-StackOverFlowQuestion-1
BUILD_URL=http://localhost:8080/job/StackOverFlowQuestion/1/
我并不热衷于使用版本号插件。下面,我展示了整个脚本和控制台输出。如您所见,我尝试了几种方法来获得 BUILDS_TODAY。它们似乎都不起作用。 有人可以告诉我如何获取当天的构建数量吗?
我做错了什么?
这是完整的脚本:
import java.text.SimpleDateFormat
import java.util.Date
pipeline {
agent { label 'ThisMachineAgent' }//any
stages {
stage('Print Environment Variables') {
steps {
script {
echo "BUILD_NUMBER=${env.BUILD_NUMBER}"
echo "BUILD_DATE=${env.BUILD_DATE}"
echo "BUILDS_TODAY=${env.BUILDS_TODAY}"
// Print Jenkins environment variables using shell command
if (isUnix()) {
sh 'printenv'
} else {
bat 'set'
}
}
}
}
stage('Count Builds for Today') {
steps {
script {
def jobName = env.JOB_NAME
def currentDate = new Date()
def formatter = new SimpleDateFormat("yyyy-MM-dd")
def today = formatter.format(currentDate)
def job = Jenkins.instance.getItemByFullName(jobName)
def buildCount = job.getBuilds().findAll { build ->
def buildDate = formatter.format(new Date(build.getTimeInMillis()))
buildDate == today && build.result == hudson.model.Result.SUCCESS
}.size()
def allBuildCount = job.getBuilds().findAll { build ->
def buildDate = formatter.format(new Date(build.getTimeInMillis()))
}.size()
println "Total builds for ${jobName} today: ${buildCount}"
println "All Total builds for ${jobName} alltime: ${allBuildCount}"
}
}
}
stage('Print Environment')
{
steps
{
script
{
def buildsToday = countBuildsToday()
echo "Builds done today: ${buildsToday ?: 'unknown'}"
}
}
}
}
post {
success {
echo 'Build and deployment successful!'
}
failure {
echo 'Build or deployment failed!'
}
}
}
def countBuildsToday() {
def today = new Date().clearTime()
def builds = Jenkins.instance.getAllItems(hudson.model.AbstractProject.class)
return builds.count { it.getLastBuild()?.getTimeInMillis()?.clearTime() == today }
}
这是控制台输出:
[Pipeline] Start of Pipeline
[Pipeline] node
Running on ThisMachineAgent in C:\Jenkins\workspace\StackOverFlowQuestion
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Print Environment Variables)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
BUILD_NUMBER=1
[Pipeline] echo
BUILD_DATE=null
[Pipeline] echo
BUILDS_TODAY=null
[Pipeline] isUnix
[Pipeline] bat
C:\Jenkins\workspace\StackOverFlowQuestion>set
ALLUSERSPROFILE=C:\ProgramData
APPDATA=C:\Users\awalker\AppData\Roaming
BUILD_DISPLAY_NAME=#1
BUILD_ID=1
BUILD_NUMBER=1
BUILD_TAG=jenkins-StackOverFlowQuestion-1
BUILD_URL=http://localhost:8080/job/StackOverFlowQuestion/1/
CI=true
CommonProgramFiles=C:\Program Files\Common Files
CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files
CommonProgramW6432=C:\Program Files\Common Files
COMPUTERNAME=LAWLL035
ComSpec=C:\WINDOWS\system32\cmd.exe
DriverData=C:\Windows\System32\Drivers\DriverData
DXSDK_DIR=C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\
EXECUTOR_NUMBER=0
GIT_LFS_PATH=C:\Users\awalker\AppData\Local\Programs\Git LFS
HOMEDRIVE=C:
HOMEPATH=\Users\awalker
HUDSON_COOKIE=aafe4ccb-2899-454c-8e2b-4c0ec697dd92
HUDSON_HOME=C:\ProgramData\Jenkins\.jenkins
HUDSON_SERVER_COOKIE=0fe9b9afa844463f
HUDSON_URL=http://localhost:8080/
JENKINS_HOME=C:\ProgramData\Jenkins\.jenkins
JENKINS_NODE_COOKIE=de1f5fb1-1b41-4768-9371-3fc00d1fd0cd
JENKINS_SERVER_COOKIE=durable-1af16b5c6d111e71bd5c095c598c9d94549ee45c05dc1f3b03ea7ab69f077537
JENKINS_URL=http://localhost:8080/
JOB_BASE_NAME=StackOverFlowQuestion
JOB_DISPLAY_URL=http://localhost:8080/job/StackOverFlowQuestion/display/redirect
JOB_NAME=StackOverFlowQuestion
JOB_URL=http://localhost:8080/job/StackOverFlowQuestion/
LOCALAPPDATA=C:\Users\awalker\AppData\Local
LOGONSERVER=\\LAWLL035
LUMENERA_SDK=C:\Program Files (x86)\Lumenera Corporation\LuCam Capture Software\SDK
MIL_Path=C:\Program Files\Matrox Imaging\Mil\DLL
MIL_Path64=C:\Program Files\Matrox Imaging\Mil\DLL
NODE_LABELS=ThisMachineAgent
NODE_NAME=ThisMachineAgent
NUMBER_OF_PROCESSORS=16
OneDrive=C:\Users\awalker\OneDrive - Revvity
OneDriveCommercial=C:\Users\awalker\OneDrive - Revvity
OS=Windows_NT
Path=C:\Program Files\Common Files\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\dotnet\;C:\Program Files\Matrox Imaging\Mil\DLL;C:\Program Files\Matrox Imaging\Tools;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\;C:\Program Files\MATLAB\MATLAB Runtime\v93\runtime\win64;C:\Program Files\MATLAB\MATLAB Runtime\v912\runtime\win64;C:\Program Files (x86)\Microsoft SQL Server\150\Tools\Binn\;C:\Program Files\Microsoft SQL Server\150\Tools\Binn\;C:\Program Files\Microsoft SQL Server\150\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\150\DTS\Binn\;C:\Program Files\Azure Data Studio\bin;C:\Program Files\MATLAB\MATLAB Compiler Runtime\v79\runtime\win64;C:\Program Files\gs\gs10.02.1\bin;C:\Program Files\Git\cmd;C:\Users\awalker\AppData\Local\Microsoft\WindowsApps;C:\Users\awalker\.dotnet\tools;C:\Users\awalker\AppData\Local\Programs\Git LFS
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
PROCESSOR_ARCHITECTURE=AMD64
PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 141 Stepping 1, GenuineIntel
PROCESSOR_LEVEL=6
PROCESSOR_REVISION=8d01
ProgramData=C:\ProgramData
ProgramFiles=C:\Program Files
ProgramFiles(x86)=C:\Program Files (x86)
ProgramW6432=C:\Program Files
PROMPT=$P$G
PSModulePath=%ProgramFiles%\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules;C:\Program Files (x86)\Microsoft SQL Server\150\Tools\PowerShell\Modules\
PUBLIC=C:\Users\Public
RUN_ARTIFACTS_DISPLAY_URL=http://localhost:8080/job/StackOverFlowQuestion/1/display/redirect?page=artifacts
RUN_CHANGES_DISPLAY_URL=http://localhost:8080/job/StackOverFlowQuestion/1/display/redirect?page=changes
RUN_DISPLAY_URL=http://localhost:8080/job/StackOverFlowQuestion/1/display/redirect
RUN_TESTS_DISPLAY_URL=http://localhost:8080/job/StackOverFlowQuestion/1/display/redirect?page=tests
STAGE_NAME=Print Environment Variables
SystemDrive=C:
SystemRoot=C:\WINDOWS
TEMP=C:\Users\awalker\AppData\Local\Temp
TMP=C:\Users\awalker\AppData\Local\Temp
USERDNSDOMAIN=revvity.com
USERDOMAIN=REVVITY
USERDOMAIN_ROAMINGPROFILE=REVVITY
USERNAME=WalkeA31496
USERPROFILE=C:\Users\awalker
windir=C:\WINDOWS
WORKSPACE=C:\Jenkins\workspace\StackOverFlowQuestion
WORKSPACE_TMP=C:\Jenkins\workspace\StackOverFlowQuestion@tmp
ZES_ENABLE_SYSMAN=1
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Count Builds for Today)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
Total builds for StackOverFlowQuestion today: 0
[Pipeline] echo
All Total builds for StackOverFlowQuestion alltime: 1
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Print Environment)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
Builds done today: unknown
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] echo
Build and deployment successful!
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS