Parameters and Variables in Jenkins¶
Both parameters and variables are used in Jenkins to handle dynamic and reusable values during builds. However, they serve distinct purposes, have different scopes, and are used in specific scenarios.
1. Parameters in Jenkins¶
Definition:¶
Parameters allow you to provide inputs to a build at runtime. These inputs can control how the build behaves and enable customization without altering the pipeline code.
Key Features:¶
- Defined at the job level in Jenkins.
- Available for users to input when triggering a build manually.
- Examples include string, choice, boolean, password, and file parameters.
Use Case:¶
- Customizing builds, such as deploying to different environments (
dev,staging,prod). - Selecting specific configurations or features for a build.
- Passing credentials securely as parameters (e.g.,
passwordorfile).
When to Use:¶
- When you need user input or control over how the build should execute.
- If a build process needs flexibility for different conditions or environments.
Types:¶
- String Parameter: Allows users to input a string value.
- Choice Parameter: Presents a list of predefined choices to the user.
- Boolean Parameter: Offers a yes/no or true/false option.
- Password Parameter: Hides the input for security, typically used for credentials.
- Credentials Parameter: Allows users to select from a list of stored credentials.
- File Parameter: Allows users to upload a file.

Example:¶
At a time, multiple types of parameters can be used in a Jenkins job. For instance, a job might ask for a username, a password, and a choice of environment (dev, staging, prod). The configuration is of the type String Parameter, Password Parameter, and Choice Parameter respectively.
- Cofigure in Jenkins Job:

- Configure in Jenkinsfile:
A parameterized build with a string parameter Environment:
parameters { // Use this directive when the pipeline is not parameterized.
string(name: 'Environment', defaultValue: 'dev', description: 'Target environment for deployment')
}
echo "Deploying to environment: ${params.Environment}"
2. Variables in Jenkins¶
Definition:¶
Variables are used to store and reference dynamic values within a Jenkins build. These can include:
- Environment Variables: Predefined by Jenkins or the build environment (e.g.,
BUILD_NUMBER,WORKSPACE). - Pipeline Variables: Custom variables defined within the pipeline code.
Key Features:¶
- Available within the scope of the job or script.
- Not exposed to users directly.
- Can be static or dynamic based on the build process.
Use Case:¶
- Storing and reusing intermediate values during the build process.
- Managing values such as file paths, URLs, or build-specific data.
When to Use:¶
- For internal calculations, intermediate values, or managing runtime configurations.
- When you do not require user input and the value can be derived within the build.
Example:¶
Using environment and pipeline variables:
def artifactPath = "${WORKSPACE}/build/artifact.jar"
sh "cp ${artifactPath} /deployments"
Key Differences¶
| Feature | Parameters | Variables |
|---|---|---|
| Definition | Inputs provided by users at build time. | Values used within the build or pipeline. |
| Scope | Exposed to users triggering the build. | Internal to the build or pipeline. |
| Customization | Allows user-specific build behavior. | Enables dynamic behavior without user input. |
| Examples | Environment, Version | BUILD_NUMBER, WORKSPACE |
Choosing Between Parameters and Variables¶
| Scenario | Use Parameters | Use Variables |
|---|---|---|
| User needs to control build behavior | ✅ | ❌ |
| Build process depends on dynamic user input | ✅ | ❌ |
| Values are derived during the build | ❌ | ✅ |
| Intermediate values needed internally | ❌ | ✅ |
Conclusion¶
- Use parameters when you need user input or flexibility for different builds.
- Use variables for internal build logic and runtime data manipulation.
Both play complementary roles in making Jenkins pipelines more dynamic and reusable.