#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Jenkins · Topic 12 of 15

Pipeline Variables: Global & Local

By Uncodemy Editorial Team · Updated 16 Nov 2026

The short version

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.

Global and local in one Jenkinsfile

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

Built-in variables you get for free

VariableHolds
BUILD_NUMBERThe build number, e.g. 42
JOB_NAMEThe job's name (with folder path)
WORKSPACEThe workspace folder on the agent
BUILD_URLLink to this build's page
NODE_NAMEThe agent running the stage
BRANCH_NAMEThe 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.

Values you work out during the build

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.

Secrets as variables

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.

Common errors

Before this topic, read pipeline basics. Variables also flow between jobs; see upstream and downstream jobs.

Master Jenkins & DevOps with Uncodemy

Hands-on live training, real projects and Dedicated Placement Support — become job-ready in DevOps.

Enroll in DevOps Training →