Uncodemy Logo ← Back to Course
Jenkins · Topic 12 of 15

Pipeline Variables: Global & Local

Two Types of Variables

Variables are used to store values/data inside a pipeline. Jenkins declarative pipelines support two kinds:

  1. Global variable
  2. Local variable

Global Variable

Declared in an environment block right after agent. Referenced anywhere in the stages using $variableName.

pipeline {
    agent any
    environment {
        name = "Sandeep Chikkala"
    }
    stages {
        stage('Hello') {
            steps {
                echo "hi, my name is $name"
            }
        }
    }
}

Multiple Global Variables

environment {
    name = "Sandeep Chikkala"
    topic = 'DevOps'
    platform = 'linkedin'
    duration = 90
}
stages {
    stage('Multiple Global Variables') {
        steps {
            echo "hi, my name is $name, I'm teaching $topic through $platform."
            echo "And the course duration is $duration days"
        }
    }
}

Local Variable

A local variable is declared inside a specific stage and overrides the global variable for that stage only.

pipeline {
    agent any
    environment {
        name = "Sandeep"
    }
    stages {
        stage('Hello') {
            steps { echo "hi, my name is $name" }
        }
        stage("local variable") {
            environment {
                name = "SandeepChikkala"
            }
            steps { echo "hii, I'm $name" }
        }
    }
}

Use a local variable whenever one particular stage needs a different value than the rest of the pipeline.

Master Jenkins & DevOps with Uncodemy

Hands-on live training, real projects, and placement support — become job-ready in DevOps.

Enroll in DevOps Training →