You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Jenkinsfile中使用Bash日期替换并替代已废弃的BUILD_ID变量

Hey there! Let's tackle your two Jenkins-related issues step by step:

1. Fixing the Bat Command for Timestamp Output

First off, the $(date +%Y%m%d-%H%M) syntax you're using is bash/shell-specific, which won't work directly in Windows Batch (bat) scripts. Bat uses its own variable syntax for dates and times, plus you have to account for Jenkins escaping special characters like %.

Here's how you can get the YYYYMMDD-HHMM format in a Jenkins bat step:

bat """
echo %%date:~0,4%%%%date:~5,2%%%%date:~8,2%%-%%time:~0,2%%%%time:~3,2%%
"""

Why this works:

  • Bat uses %date% and %time% to access system date/time, but we slice the strings to get the exact format:
    • %date:~0,4% grabs the 4-digit year
    • %date:~5,2% grabs the 2-digit month
    • %date:~8,2% grabs the 2-digit day
    • %time:~0,2% grabs the 2-digit hour
    • %time:~3,2% grabs the 2-digit minute
  • In Jenkins Pipeline, % characters need to be escaped by doubling them (%%) so Jenkins doesn't try to interpret them as environment variables before passing them to the bat script.

If you're open to using PowerShell instead (which has more flexible date formatting), this is even cleaner:

powershell """
echo (Get-Date -Format "yyyyMMdd-HHmm")
"""
2. Replacing the Deprecated BUILD_ID Variable Without EnvInject

You don't need the EnvInject plugin to recreate the old BUILD_ID format! Jenkins Pipeline gives you direct access to the currentBuild object, which you can use to generate the timestamp in the same YYYY-MM-DD_hh-mm-ss format.

Declarative Pipeline Example:

If you're using Declarative Pipeline, define the timestamp in the environment block:

pipeline {
    agent any
    environment {
        // Recreate the old BUILD_ID format
        BUILD_TIMESTAMP = currentBuild.getTime().format("yyyy-MM-dd_HH-mm-ss")
    }
    stages {
        stage('Use Timestamp') {
            steps {
                echo "Old BUILD_ID style timestamp: ${BUILD_TIMESTAMP}"
                // You can use it in bat steps too (remember to double % if referencing env vars directly in bat)
                bat "echo %%BUILD_TIMESTAMP%%"
            }
        }
    }
}

Scripted Pipeline Example:

For Scripted Pipeline, you can define it as a variable or set it as an environment variable:

node {
    // Generate the timestamp
    def buildTimestamp = currentBuild.getTime().format("yyyy-MM-dd_HH-mm-ss")
    echo "Old BUILD_ID style timestamp: ${buildTimestamp}"
    
    // If you need it as an environment variable for subsequent steps
    env.BUILD_TIMESTAMP = buildTimestamp
    bat "echo %%BUILD_TIMESTAMP%%"
}

This approach uses Jenkins' built-in functionality, so no extra plugins are required. The currentBuild.getTime() method gives you the build's start time, and format() lets you match the exact deprecated BUILD_ID format.

内容的提问来源于stack exchange,提问作者Nicolas Baumann

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 15:49:08