Interview Prep
Interview questions, roadmaps and practice sets
Tutorials
Free tutorials on 30+ topics
Blog
Career guides, skills and interview tips
Free PDFs
Notes, cheat sheets and question banks
Certificates
Free skill tests and course certificates
Tools & Whiteboard
Free tools you can use right in the browser
Practice Tests
Mock tests, MCQs and quick Q&A
Free LMS
Learn on the Uncodemy LMS
Choose the best training program according to your career.
Improve the performance of your business with our innovative Industrial Training Program.
Read More →
Our instructor-led training classes are designed to make you an expert.
Read More →
Real-life case studies are a great way to demonstrate your practical knowledge.
Read More →
Explore blogs, latest technologies and industry updates with practical examples.
Read More →Browse all available training programs and choose the right one for your career.
Read More →Know more about who we are and what we stand for.
Discover our mission to equip individuals with essential IT skills and industry expertise.
Read More →
Free tutorials, interview questions, tools and calculators to help you learn.
Explore →Learn, practice and plan your career — bilkul free.
Book a free video counselling session and pick the right course for you.
Read More →In a declarative pipeline, a global variable is declared in the environment block at the top of the pipeline and is visible in every stage. A local variable is declared in an environment block inside one stage; it exists only in that stage and, if it has the same name as a global one, it wins there. Both kinds are environment variables, so they are always strings.
pipeline {
agent any
environment { // global: every stage sees these
APP_NAME = 'student-portal'
DEPLOY_ENV = 'qa'
}
stages {
stage('Build') {
steps {
echo "Building ${APP_NAME} for ${DEPLOY_ENV}"
sh 'echo "Shell sees $APP_NAME too"'
}
}
stage('Smoke test on staging') {
environment { // local: only this stage
DEPLOY_ENV = 'staging'
TEST_URL = 'https://staging.example.com'
}
steps {
echo "Testing ${APP_NAME} on ${DEPLOY_ENV} at ${TEST_URL}"
}
}
stage('Report') {
steps {
echo "Back to ${DEPLOY_ENV}" // qa again; TEST_URL does not exist here
}
}
}
}
Console output, in order:
Building student-portal for qa
Shell sees student-portal too
Testing student-portal on staging at https://staging.example.com
Back to qa
| Variable | Holds |
|---|---|
BUILD_NUMBER | The build number, e.g. 42 |
JOB_NAME | The job's name (with folder path) |
WORKSPACE | The workspace folder on the agent |
BUILD_URL | Link to this build's page |
NODE_NAME | The agent running the stage |
BRANCH_NAME | The branch, in multibranch pipelines only |
The full list for your server is under any pipeline job → Pipeline Syntax → Global Variables Reference. Use them as ${env.BUILD_NUMBER} in Groovy strings or $BUILD_NUMBER in shell commands.
The environment block holds fixed values. For something calculated at run time, like a version tag from Git, set env.NAME inside a script block. It is then visible in later stages:
stage('Version') {
steps {
script {
def shortSha = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
env.IMAGE_TAG = "${env.BUILD_NUMBER}-${shortSha}"
}
echo "Tag: ${env.IMAGE_TAG}"
}
}
def shortSha is a plain Groovy variable: it lives only inside that script { } block. env.IMAGE_TAG is an environment variable and survives into later stages.
environment {
SONAR_TOKEN = credentials('sonar-token') // ID of a "Secret text" credential
}
steps {
sh 'sonar-scanner -Dsonar.token=$SONAR_TOKEN' // single quotes: the shell expands it
}
Jenkins masks the value in the log. For a "Username with password" credential, it also creates NAME_USR and NAME_PSW.
${APP_NAME}: you used single quotes in Groovy (echo '${APP_NAME}'). Groovy only interpolates double-quoted strings. Inside sh '...', though, single quotes are fine because the shell does the expanding.sh "... ${SONAR_TOKEN}". Use single quotes so the secret never appears in the generated script.if (env.RUN_TESTS) is always true: environment values are strings, and the string 'false' is truthy in Groovy. Compare with == 'true', or use a boolean pipeline parameter.env.DEPLOY_ENV = ... has no effect: values declared in an environment block cannot be overwritten that way. Use a stage-level environment block or withEnv(['DEPLOY_ENV=prod']) { ... }.No such property: shortSha: a def variable was used outside the script block where it was declared.Before this topic, read pipeline basics. Variables also flow between jobs; see upstream and downstream jobs.
Hands-on live training, real projects and Dedicated Placement Support — become job-ready in DevOps.
Enroll in DevOps Training →