In this lesson, we explore environment variables in Jenkins and their importance in modern application deployments. Environment variables are key-value pairs that configure the behavior of processes across different environments. They allow your application to dynamically adjust settings without modifying the source code, which is especially valuable when moving between development, staging, and production environments. Imagine an application that connects to a database. During development, the application might connect to a development database; however, in production, it needs to communicate with a production database. Hardcoding the development database’s IP address is not a viable solution because the connection details must change in production. Instead, storing these details as environment variables ensures that the correct settings are applied based on the deployment target. The operating system supplies the appropriate configuration, enabling seamless transitions between environments.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.



Types of Environment Variables in Jenkins
There are two main categories of environment variables in Jenkins:- System Environment Variables: Set on the operating system where Jenkins is running. Examples include
PATHand other system-specific settings that influence Jenkins operations. - Jenkins-Specific Environment Variables: Defined within Jenkins and can be scoped globally, per job, or even to a particular stage within a pipeline.
Scope of Environment Variables
When working with environment variables in Jenkins, they are available at different levels of visibility:- Global: Accessible to all jobs and pipelines.
- Job-Specific: Available only within a particular job.
- Pipeline Stage-Specific: Limited to a specific stage within the pipeline, reducing variable exposure and enhancing security.
Configuring Environment Variables in a Jenkins Pipeline
The following examples demonstrate how to configure environment variables in Jenkins pipelines.Global Environment Variable Example
The example below shows how to define an environment variable globally in a Jenkinsfile:MY_VAR is set at the pipeline level, making it available in all stages. Referencing ${MY_VAR} will return the value 'value'.
Stage-Specific Environment Variable Example
You can also scope environment variables to a specific pipeline stage. Consider the following example:HOSTNAME and PORT environment variables are only accessible within the Build stage, demonstrating how scoping minimizes potential security risks.
Built-in Environment Variables in Jenkins
Jenkins provides several built-in environment variables that offer useful information about the build process. Some examples include:BUILD_IDBUILD_NUMBERBUILD_TAGBUILD_URLEXECUTORJAVA_HOMEJENKINS_URLJOB_NAMENODE_NAMEWORKSPACE
- Use the Jenkins Credentials Plugin to inject secrets into environment variables instead of hardcoding them.
- Maintain consistent naming conventions across all projects to improve clarity and maintenance.
- Document the usage and purpose of each environment variable within your Jenkins jobs and pipelines.
