Keywords: Jenkins | Environment Variables | System Information | EnvInject Plugin | Configuration Management
Abstract: This article provides an in-depth analysis of the origin mechanisms of environment variables in Jenkins system information, exploring the inheritance principles and distinguishing between system environment variables, shell configuration files, and Jenkins-specific variables. Through practical code examples, it demonstrates how to view and configure environment variables, and offers methods for custom variable configuration using the EnvInject plugin. The paper comprehensively examines the Jenkins environment variable management system from fundamental principles to practical applications.
Environment Variable Inheritance Mechanism
The environment variables displayed on the Jenkins system information page primarily originate from the host system environment where Jenkins is running. When the Jenkins service starts, it inherits all environment variables from the current system shell. These variables include both system-level and user-level environment variables, forming the foundational environment configuration for Jenkins runtime operations.
To verify this, execute the env command on the server running Jenkins:
# Execute env command in terminal
$ env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
HOME=/var/lib/jenkins
# More environment variables...The execution results should largely match the environment variable list displayed on the Jenkins system information page. This inheritance mechanism ensures Jenkins can access various system-configured paths, library files, and runtime parameters.
Analysis of Environment Variable Sources
Environment variables are set through several primary channels:
System-level Configuration Files: Including global configuration files such as /etc/profile, /etc/bash.bashrc. These files are read during system startup or user login, with set environment variables生效 for all users and processes.
User-level Configuration Files: Such as user-specific configuration files like ~/.bashrc, ~/.bash_profile. These files load during user login or new shell startup, with set environment variables生效 only for the current user.
Process Inheritance: When Jenkins starts as a service, it inherits environment variables from its parent process (typically the init system or service manager). For example, Jenkins started via systemd inherits systemd's environment configuration.
Below is a code example for setting environment variables:
# Add environment variables in ~/.bashrc
export ORACLE_HOME=/usr/lib/oracle/19.6/client64
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH
export PATH=$ORACLE_HOME/bin:$PATHAfter addition, the configuration file needs reloading or shell restart to take effect.
Special Handling of Oracle-related Variables
Regarding the Oracle environment variable issue mentioned by the user, special attention is needed for environment variable loading sequence and scope. Even if Oracle variables in Tomcat's bashrc are commented out, these variables might still be set through other channels.
Possible solutions include:
Check All Possible Configuration Files: Beyond the files checked by the user, also examine configuration files like /etc/environment, /etc/default/jenkins that might contain environment variable settings.
Examine Service Startup Scripts: Jenkins service startup scripts might directly set environment variables. For example, in systemd service files:
# /etc/systemd/system/jenkins.service
[Service]
Environment="ORACLE_HOME=/opt/oracle"
Environment="LD_LIBRARY_PATH=/opt/oracle/lib"Utilize Environment Variable Viewing Tools: Real-time viewing of all available environment variables is possible through Jenkins' environment variable viewing page http://<jenkins-host>:8080/env-vars.html/.
Jenkins-specific Environment Variables
Beyond inherited system environment variables, Jenkins sets specific environment variables during job execution. These variables include build-related information, version control information, system parameters, etc., but are not displayed on the system information page.
Job-level environment variable example:
# Access environment variables in Jenkins Pipeline
pipeline {
agent any
stages {
stage('Build') {
steps {
script {
echo "Build URL: ${env.BUILD_URL}"
echo "Job Name: ${env.JOB_NAME}"
echo "Workspace: ${env.WORKSPACE}"
}
}
}
}
}These variables are available only during job execution, providing build context information.
Custom Environment Variable Configuration
Using the EnvInject plugin enables flexible configuration of custom environment variables. Below is a detailed explanation of configuration steps:
Install EnvInject Plugin: Install the Environment Injector Plugin through Jenkins plugin management interface.
Configure Project Environment Variables: In the project configuration page's "Build Environment" section, check the "Inject environment variables to the build process" option.
Set Variable Content: Input environment variable definitions in the "Properties Content" text box, supporting multiple formats:
# Key-value pair format
TZ=America/New_York
CUSTOM_VAR=value123
ORACLE_HOME=/custom/path/to/oracle
# Or load from file
VARIABLE_FILE=/path/to/env.propertiesScript-based Injection: Environment variables can also be dynamically set via Groovy scripts:
// Dynamically set environment variables in Pipeline
pipeline {
agent any
environment {
CUSTOM_VAR = 'dynamic_value'
TIMESTAMP = sh(script: 'date +%s', returnStdout: true).trim()
}
stages {
stage('Example') {
steps {
echo "Custom variable: ${env.CUSTOM_VAR}"
}
}
}
}This method is particularly suitable for scenarios requiring dynamic variable setting based on build conditions.
Best Practices for Environment Variable Management
To effectively manage Jenkins environment variables, follow these best practices:
Unified Configuration Management: Centralize important environment variables in few configuration files, avoiding dispersion across multiple locations.
Sensitive Information Protection: For sensitive information like passwords and keys, use Jenkins Credentials functionality instead of plain text environment variables.
Environment Isolation: Set different environment variables for different environments (development, testing, production) to ensure environment consistency.
Documentation: Maintain environment variable documentation, recording each variable's purpose, setting location, and dependencies.
Through systematic environment variable management, stability and maintainability of Jenkins build environments can be ensured, providing reliable foundational support for continuous integration and continuous delivery processes.