Effective Methods to Obtain BUILD_USER in Jenkins for Timer-Triggered Jobs

Dec 05, 2025 · Programming · 10 views · 7.8

Keywords: Jenkins | BUILD_USER | Timer Trigger | Conditional Plugin | API Script | Automation

Abstract: This article addresses the challenge of retrieving the BUILD_USER variable in Jenkins when jobs are triggered by timers, offering comprehensive solutions that include plugin-based and script-based approaches. It analyzes the root cause, details the core method using Conditional Build Step Plugin, supplements with shell and Groovy scripts, and concludes with implementation steps and best practices to enhance automation flexibility.

Introduction

In Jenkins automation environments, the BUILD_USER environment variable, provided by the Build User Vars Plugin, is commonly used to identify the user who triggered a build, such as in post-job email notifications. However, when a build is triggered by a timer or scheduler, this variable is not automatically initialized, leading to an inability to capture user information correctly. This issue affects many automation scenarios that rely on user identification.

Problem Analysis

Based on the best answer discussion, the BUILD_USER variable is only initialized by the plugin for manual triggers and is missing for timer triggers. This occurs because timer triggers are treated as system actions rather than specific user operations. Therefore, additional mechanisms are needed to simulate or extract user information.

Core Solution: Using Conditional Plugins

The recommended approach is to use the Conditional Build Step Plugin or Run Condition Plugin to resolve this issue. In the job configuration, you can add a conditional step that sets the BUILD_USER_ID variable only when the build is triggered by a timer. For example, use a regular expression to match build causes (e.g., TimerTrigger) to trigger the condition.

Implementation steps: First, install the plugin, then configure the condition in the job's "Build Environment" or "Build Steps." For instance, set a condition such as "when build cause contains 'timer'" and execute a script step to assign BUILD_USER_ID a specific value (e.g., 'system' or extracted from other sources).

Supplementary Methods: Script-Based Solutions

Other answers provide script-based solutions without plugins, suitable for simple or temporary needs.

Practical Guide and Code Examples

To combine core and supplementary methods, it is advisable to integrate conditional logic in Pipelines. The following example demonstrates a comprehensive approach:

node('master') {
    def userInfo = ''
    // Check build causes
    def causes = currentBuild.getBuildCauses()
    causes.each { cause ->
        if (cause instanceof hudson.triggers.TimerTrigger) {
            userInfo = "Timer Triggered / system"
        } else if (cause instanceof hudson.model.Cause$UserIdCause) {
            userInfo = "${cause.userName} / ${cause.userId}"
        }
    }
    if (userInfo.isEmpty()) {
        // Fallback API call
        userInfo = sh(script: "curl -k --silent ${BUILD_URL}/api/json | jq -r '.actions[] | select(.causes) | .causes[] | select(.userId) | \"\(.userName) / \(.userId)\"'", returnStdout: true).trim()
    }
    env.BUILD_USER = userInfo
    echo "BUILD_USER set to: ${env.BUILD_USER}"
}

This code prioritizes build causes and falls back to API queries if needed, ensuring robustness.

Conclusion and Recommendations

Retrieving the BUILD_USER variable for timer-triggered jobs requires a combination of plugin and script strategies. The core solution using conditional plugins offers structured handling, while script-based methods are suitable for rapid prototyping or restricted environments. Recommendations: for complex workflows, use plugins to ensure maintainability; for lightweight scenarios, adopt scripts to reduce dependencies. Regardless of the method, test behavior under different trigger scenarios to avoid unexpected errors in production environments.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.