How to get build time stamp from Jenkins build variables?

前端 未结 10 1011
余生分开走
余生分开走 2020-12-24 11:25

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.

10条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-24 11:51

    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}@/job//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.

提交回复
热议问题