Jenkins

Use Timestamps in Jenkins

In Jenkins, timestamps record the date and time at which various events occur, such as when a build starts when it finishes, or when a user logs in. Timestamps can help track the progress of builds, identify when problems occurred, and etc.

Jenkins will typically record timestamps in ISO 8601, a standardized format for representing dates and times. They can be used in various ways, such as to generate reports or trigger other actions based on the event’s time.

In Jenkins, timestamps are governed by the timestampper plugin. This plugin allows you to add timestamps to the console output of a given Jenkins job.

In this tutorial, we will learn how to manage various aspects of the timestamper plugin to add flexibility to the timestamps of your Jenkins output.

Jenkins Timestamper Plugin

By default, you will find the Timestamper plugin installed on your Jenkins controller. However, it is good to ensure that the plugin is installed.

Navigate to the Jenkins Dashboard -> Manage Jenkins -> Manage Plugins -> Installed Plugins.

Configuring the Jenkins Timestamper Plugin

You can customize various parameters of the Timestamper plugin from the Jenkins Dashboard.

Navigate to Manage Jenkins -> Configure System -> Timestamper.

In this section, you can customize the system clock and elapsed time formats.

You can customize formats using the DurationFormats, as shown in the documentation below.

https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/time/DurationFormatUtils.html

To enable the Timestamper on all Jenkins build, check the “Enable for all pipeline builds” checkbox and click save.

Once enabled, a timestamp will be added to every line in the console output of a Jenkins build.

An example is shown below:

Enabling Timestamps for a Job

Sometimes, you may not want to enable the timestamps for specific jobs rather than globally.

Start by disabling the Global timestamps option as shown in the previous step.

Next, you can add timestamps in a given job by adding the timestamps option in a Jenkins pipeline.

An example is as shown:

pipeline {
    agent any
    options {
        timestamps()
    }
    stages {
        stage('Start') {
            steps {
                sh 'date +%s'
            }
        }
        stage('Pausing') {
            steps {
                sleep(time: 2, unit: 'MINUTES')
            }
        }
        stage('End') {
            steps {
                sh "date +%s"
            }
        }
    }
}

By adding the timestamp() options in the Jenkinsfile, Jenkins will enable timestamps for the console output of the job.

Conclusion

In this article, you learned how to enable, disable and customize timestamps in Jenkins pipelines.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list