Variables are used to store values/data inside a pipeline. Jenkins declarative pipelines support two kinds:
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"
}
}
}
}
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"
}
}
}
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.
Hands-on live training, real projects, and placement support — become job-ready in DevOps.
Enroll in DevOps Training →