Home > Blockchain >  How can I use bash substitution for date inside a Jenkinsfile?
How can I use bash substitution for date inside a Jenkinsfile?

Time:10-02

How can I do something like :

bat """
    echo $(date  %Y%m%d-%H%M)
"""

What I tried :

  • escape the $ with backslash
  • escape the % with backslash
  • double the %

Also Jenkins BUILD_ID variable has been removed so that's why I'm trying other options.

As mentioned in the docs BUILD_ID is defunct and I'm looking for a drop-in replacement for this variable.

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

Source https://wiki.jenkins-ci.org/display/JENKINS/Building a software project#Buildingasoftwareproject-JenkinsSetEnvironmentVariables

See also https://issues.jenkins-ci.org/browse/JENKINS-26520

In this last link, I found that piece of code to add to "prepare env" but don't know how to use it without plugin EnvInject (no plugin can be installed by the way)

return [BUILD_TIMESTAMP:currentBuild.getTime().format("yyyy-MM-dd_HH-mm-ss")]

CodePudding user response:

Several issues here:

  • date.exe from Windows clashes with date.exe from MINGW64
  • bash substitution with $(...) will not work in bat

A solution using Powershell:

bat """
for /f %%i in ('powershell get-date -format "{yyyyMMdd_HHmmss}"') do set datetime=%%i
echo            
  • Related