In a Declarative Jenkins Pipeline, set variables with an environment block: put it inside pipeline for Pipeline-wide scope or inside a stage for stage-only scope. In a Scripted Pipeline, wrap the relevant steps in withEnv(['NAME=value']). Reference a variable in Pipeline Groovy as env.NAME; in shell commands, expand it using that shell’s syntax.
Set a variable in a Declarative Pipeline
Use environment in a Declarative Jenkinsfile. A block directly inside pipeline makes its values available across the Pipeline; a block inside a stage limits them to that stage.
As an Amazon Associate I earn from qualifying purchases.
Pipeline-wide variable
pipeline {
agent any
environment {
BUILD_MODE = 'release'
}
stages {
stage('Build') {
steps {
echo "Mode: ${env.BUILD_MODE}"
sh 'make BUILD_MODE=$BUILD_MODE'
}
}
}
}
Use env.BUILD_MODE when reading the value in Groovy. The single-quoted sh argument leaves $BUILD_MODE for the shell to expand.
The Tool Desk
Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Stage-only variable
pipeline {
agent any
stages {
stage('Test') {
environment {
TEST_FLAGS = '--verbose'
}
steps {
sh 'make test TEST_FLAGS="$TEST_FLAGS"'
}
}
}
}
Here, TEST_FLAGS is defined for the Test stage rather than at the Pipeline level.
#1 Best Overall
Set a variable in a Scripted Pipeline
Use withEnv to provide an environment value to the steps inside its block:
node {
withEnv(['BUILD_MODE=release']) {
sh 'make BUILD_MODE=$BUILD_MODE'
}
}
The value is available to external processes started within the wrapper’s scope. withEnv can also unset a variable with an entry such as NAME=, or prepend a directory to PATH with the special form PATH+LABEL=/directory.
Rank #2
Choose the right source and scope
- Ordinary configuration: Use an
environmentblock orwithEnv, choosing the location that matches the required scope. - Build input: Pipeline parameters are exported as environment variables when a build starts. In Pipeline Groovy, read parameter values through the read-only
paramsmap. - Secret: Do not put the secret value directly in the Jenkinsfile. Bind a configured Jenkins credential by ID with
credentials('credential-id')in a Declarativeenvironmentblock, or usewithCredentialsfor supported credential bindings and scoped access.
Use credentials without exposing them unnecessarily
A username/password credential binding can provide a combined value and separate _USR and _PSW variables. Jenkins masking can reduce accidental disclosure in logs, but it does not prevent a Pipeline from disclosing a credential. Do not provide trusted credentials to untrusted Pipeline jobs. Jenkins warns: “A Pipeline that uses credentials can also disclose those credentials.” (Jenkins: Using credentials.)
When a shell step needs a secret, pass a literal command string so the shell reads the value from its environment; avoid Groovy-interpolated command strings, which can expose secret material in process arguments. Follow the Jenkinsfile documentation for credential and Pipeline details.
Expand variables in Groovy and shell steps
- Pipeline Groovy: Read a Pipeline environment variable as
env.NAME, for exampleecho "Mode: ${env.BUILD_MODE}". - POSIX shell via
sh: Use$NAMEor${NAME}in the shell command. Keep commands single-quoted in Groovy when shell expansion is intended. - Other command shells: Use the expansion syntax for the shell step and operating system actually running the command. POSIX
$NAMEsyntax is not universal.
Troubleshoot common problems
- The variable is unavailable in a step: Check that it is defined at the correct level. A stage-level
environmentvalue is limited to that stage, and awithEnvvalue applies within its wrapped block. - Groovy prints an empty or unexpected value: Read the Pipeline variable using
env.NAME. For a build parameter, checkparams.NAMEin Groovy or its exported environment-variable form in a process. - The shell receives a literal
$NAME: Check the command’s shell and quoting. A single-quoted Groovy string passes the dollar expression to the shell, but the shell must support that expansion syntax. - A credential appears in command arguments or logs: Avoid Groovy interpolation for commands that use secrets; pass a literal command to the shell. Review which jobs and users can access the credential, since masking is not a security boundary.
Or skip the browser setup
For a different task—capturing a website screenshot—ScreenshotNeo accepts one GET request with a URL and returns an image or PDF. Example cURL request, using the supplied Stripe target:
Quick Recap
Best Value
Rank #4
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
See the ScreenshotNeo API documentation for options and response details. It removes cookie banners, popups and chat widgets before capture; bot checks, blank pages and failed loads are never billed; its MCP server lets AI agents take screenshots. The free plan includes 1,000 screenshots a month with no card, and paid plans start at $5 for 3,000. Sign up for free.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
Free tools Windows power users keep installed
One-click scans. No signup required.




