Operations 6 min read

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.

Linux Ops Smart Journey
Linux Ops Smart Journey
Linux Ops Smart Journey
Master Jenkins Pipeline Variables: Build Flexible, Maintainable CI/CD Scripts

In CI/CD practice, Jenkins is a key tool, but hard‑coded pipeline scripts lack flexibility. Parameterized builds solve this.

Jenkins pipeline overview
Jenkins pipeline overview

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.

Kubernetes multi‑container pipeline
Kubernetes multi‑container pipeline

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.

ci/cdDevOpspipelineJenkinsParameters
Linux Ops Smart Journey
Written by

Linux Ops Smart Journey

The operations journey never stops—pursuing excellence endlessly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.