Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk3 min

How to Set Environment Variables in a Jenkins Pipeline

Use Declarative environment blocks for Pipeline-wide or stage-only values, and Scripted withEnv for scoped variables. Learn how to reference values safely in Groovy and shell steps.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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.

Choose the right source and scope

  • Ordinary configuration: Use an environment block or withEnv, 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 params map.
  • Secret: Do not put the secret value directly in the Jenkinsfile. Bind a configured Jenkins credential by ID with credentials('credential-id') in a Declarative environment block, or use withCredentials for 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.)

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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 example echo "Mode: ${env.BUILD_MODE}".
  • POSIX shell via sh: Use $NAME or ${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 $NAME syntax 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 environment value is limited to that stage, and a withEnv value applies within its wrapped block.
  • Groovy prints an empty or unexpected value: Read the Pipeline variable using env.NAME. For a build parameter, check params.NAME in 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.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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:

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.