[jenkins] How to get build time stamp from Jenkins build variables?

How can I get build time stamp of the latest build from Jenkins? I want to insert this value in the Email subject in post build actions.

This question is related to jenkins jenkins-plugins

The answer is


NOTE: This changed in Jenkins 1.597, Please see here for more info regarding the migration

You should be able to view all the global environment variables that are available during the build by navigating to https://<your-jenkins>/env-vars.html.

Replace https://<your-jenkins>/ with the URL you use to get to Jenkins webpage (for example, it could be http://localhost:8080/env-vars.html).

One of the environment variables is :

BUILD_ID
    The current build id, such as "2005-08-22_23-59-59" (YYYY-MM-DD_hh-mm-ss)

If you use jenkins editable email notification, you should be able to use ${ENV, var="BUILD_ID"} in the subject line of your email.


I know its late replying to this question, but I have recently found a better solution to this problem without installing any plugin. We can create a formatted version number and can then use the variable created to display the build date/time. Steps to create: Build Environment --> Create a formatted version number:

  • Environment Variable Name: BUILD_DATE

  • Version Number Format String: ${BUILD_DATE_FORMATTED}

thats it. Just use the variable created above in the email subject line as ${ENV, var="BUILD_DATE"} and you will get the date/time of the current build.


If you want add a timestamp to every request from browser to jenkins server. You can refer to the jenkins crumb issuer mechanism, and you can hack the /scripts/hudson-behavior.js add modify here. so it will transform a timestamp to server.

    /**
     * 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);
    }

BUILD_ID used to provide this information but they changed it to provide the Build Number since Jenkins 1.597. Refer this for more information.

You can achieve this using the Build Time Stamp plugin as pointed out in the other answers.

However, if you are not allowed or not willing to use a plugin, follow the below method:

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

This might seem a bit of overkill but manages to get the job done.

The credentials for accessing your Jenkins should be added and the id needs to be passed in the withCredentials statement, in place of 'JenkinsCredentials'. Feel free to omit that step if your Jenkins doesn't use authentication.


Try use Build Timestamp Plugin and use BUILD_TIMESTAMP variable.


One way this can be done is using shell script in global environment section, here, I am using UNIX timestamp but you can use any shell script syntax compatible time format:

pipeline {

    agent any

    environment {
        def BUILDVERSION = sh(script: "echo `date +%s`", returnStdout: true).trim()
    }

    stages {
        stage("Awesome Stage") {
            steps {
                echo "Current build version :: $BUILDVERSION"
            }
        }
    }
}

This answer below shows another method using "regexp feature of the Description Setter Plugin" which solved my problem as I could not install new plugins on Jenkins due to permission issues:

Use build timestamp in setting build description Jenkins


Build Timestamp Plugin will be the Best Answer to get the TIMESTAMPS in the Build process.

Follow the below Simple steps to get the "BUILD_TIMESTAMP" variable enabled.

STEP 1:

Manage Jenkins -> Plugin Manager -> Installed...
Search for "Build Timestamp Plugin".
Install with or without Restart.

STEP 2:

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.

USAGE:

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


Generate environment variables from script (Unix script) :

echo "BUILD_DATE=$(date +%F-%T)"

You can use the Jenkins object to fetch the start time directly

Jenkins.getInstance().getItemByFullName(<your_job_name>).getBuildByNumber(<your_build_number>).getTime()

also answered it here: https://stackoverflow.com/a/63074829/1968948