I have installed Version Number Plugin as I want to use the BUILDS_TODAY. I was hoping it would give me the number of builds for a particular pipeline today. I haven't been able to get it to work. Using:
echo "BUILDS_TODAY=${env.BUILDS_TODAY}"
yields:
BUILDS_TODAY=null
I'm new to Jenkins and concerned that perhaps I have to do more than just install the plugin? How do I use the plugin and get BUILDS_TODAY? I've tried to research this more. I used the following to try to print all variables. [But perhaps this is Windows variables and not Jenkins variables]:
if (isUnix()) {
sh 'printenv'
} else {
bat 'set'
}
this did not yield any BUILDS_TODAY. It did yield:
BUILD_DISPLAY_NAME=#1
BUILD_ID=1
BUILD_NUMBER=1
BUILD_TAG=jenkins-StackOverFlowQuestion-1
BUILD_URL=http://localhost:8080/job/StackOverFlowQuestion/1/
I'm not wedded to using Version Number Plugin. Below, I show my entire script and the console output. As you can see, I tried a few ways to get BUILDS_TODAY. None of them seemed to work. Can someone show me how to get number of builds for the day?
What am I doing wrong?
Here is the complete script:
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 }
}
Here is the Console output:
[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
The plugin doesn't populate environment variables, it provides a VersionNumber
step that returns a version string.
echo "Version ${VersionNumber('foo-${BUILDS_TODAY}')}"