Master Jenkins Pipeline Variables: Build Flexible, Maintainable CI/CD Scripts
This guide explores how to define, assign, and scope variables in Jenkins Pipelines—including environment, params, and direct assignments—through clear explanations, practical code snippets, and a real-world Kubernetes multi-container project, showing how parameterized builds transform static scripts into flexible, maintainable CI/CD workflows.
In CI/CD practice, Jenkins is a key tool, but hard‑coded pipeline scripts lack flexibility. Parameterized builds solve this.
Common Variable Definitions
def variable – local scope, for temporary variables.
environment – global scope, for configuration keys, paths, etc.
params – global scope, for user‑input parameters.
withEnv– temporary inside a block.
VAR direct assignment – global, for dynamic environment variables.
Three Frequently Used Variable Practices
Examples of environment , params , and VAR direct assignment are shown.
environment
<code>pipeline{
agent all
environment{
Test="ac"
}
stages{
stage('Global Var'){
steps{
echo "Global variable value: ${Test}"
}
}
}
}</code>params
<code>pipeline{
agent all
parameters{
string defaultValue:'master', description:'Enter branch name', name:'Branch'
}
stages{
stage('Global Parameters'){
steps{
echo "Global Parameters Branch value: ${Branch}"
}
}
}
}</code>VAR direct assignment
<code>pipeline{
agent all
environment{
BuildTime=""
}
stages{
stage('Global BuildTime Var1'){
steps{
echo "Global variable BuildTime value: ${BuildTime}"
}
}
stage('Set Global BuildTime Var'){
steps{
script{
BuildTime = sh(returnStdout:true, script:'env TZ=Asia/Shanghai date "+%Y-%m-%d"').trim()
}
}
}
stage('Global BuildTime Var2'){
steps{
echo "Global variable BuildTime value: ${BuildTime}"
}
}
}
}</code>Real Project Demonstration
A Kubernetes‑based multi‑container pipeline where the jnlp container communicates with Jenkins, a golang container compiles the project, and a kaniko container builds and pushes the image. The pipeline passes the Git commit ID and build time as global variables.
Key Steps
<code>pipeline{
agent all
environment{
BuildTime = sh(returnStdout:true, script:'env TZ=Asia/Shanghai date "+%Y-%m-%d"').trim()
CommitID = ''
}
stages{
stage('Checkout Code'){
steps{
sh 'git config --global http.sslverify false; git config --global https.sslverify false'
git branch:'$VERSION', credentialsId:'gitlab-jiaxzeng', url:'https://gitlab.jiaxzeng.com/jiaxzeng/simple.git'
}
}
stage('Set CommitID'){
steps{
script{
CommitID = sh(returnStdout:true, script:'git rev-parse HEAD').trim()
}
}
}
stage('Build'){
steps{
container('golang'){
sh """
cd ${WORKSPACE}
go env -w GOPROXY=https://proxy.golang.com.cn,direct
go mod tidy
go build -ldflags \"-X 'simple/app/controllers.Branch=$VERSION' -X 'simple/app/controllers.BuildTime=$BuildTime' -X 'simple/app/controllers.BuildTime=$CommitID'\" .
"""
}
}
}
}
}</code>By using parameters wisely, Jenkins pipelines become dynamic engines rather than static scripts, improving flexibility and maintainability for both manual triggers and automated schedules.
Linux Ops Smart Journey
The operations journey never stops—pursuing excellence endlessly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.