Loading

Quipoin Menu

Learn • Practice • Grow

jenkins / Environment Variables
tutorial

Environment Variables

Environment variables are key‑value pairs that store configuration and build metadata. Jenkins provides built‑in variables, and you can define your own. They are useful for passing information between stages or to external tools.

Built‑in Environment Variables

Jenkins exposes many environment variables. Common ones include:
  • BUILD_NUMBER: Current build number.
  • JOB_NAME: Name of the job.
  • WORKSPACE: Absolute path of the build workspace.
  • JENKINS_URL: Jenkins server URL.
  • GIT_COMMIT: Git commit hash (if using Git plugin).
You can view all available variables by adding a step: sh 'printenv' (Linux) or bat 'set' (Windows).

Defining Custom Variables in Declarative

Use the environment block:
pipeline {
environment {
APP_VERSION = '1.0.0'
BUILD_DIR = "${WORKSPACE}/build"
}
stages {
stage('Build') {
steps {
sh 'echo "Building version ${APP_VERSION}"'
}
}
}
}

Using Credentials

Never hard‑code secrets. Use Jenkins Credentials and bind them to environment variables:
pipeline {
environment {
DOCKER_PASSWORD = credentials('docker-hub-password')
}
stages {
stage('Login') {
steps {
sh 'docker login -u myuser -p ${DOCKER_PASSWORD}'
}
}
}
}

Using Variables in Scripted Pipeline

In Scripted, you can use Groovy variables and environment variables:
node {
def myVar = 'hello'
withEnv(['MY_VAR=value']) {
sh 'echo $MY_VAR'
}
}

Accessing Variables in Shell Steps

In sh steps, use double quotes for string interpolation: sh "echo ${WORKSPACE}". In single quotes, variables are not expanded.


Two Minute Drill
  • Jenkins has built‑in variables like BUILD_NUMBER, WORKSPACE.
  • Define custom variables in environment block.
  • Use credentials helper to inject secrets safely.
  • Access variables with ${VAR} in double‑quoted strings.
  • Print all variables with sh 'printenv'.

Need more clarification?

Drop us an email at career@quipoinfotech.com