An environment variable in Jenkins is a key-value pair that stores information accessible to Jenkins jobs during their execution. It helps configure jobs without hardcoding values.
Click to reveal answer
beginner
How do you access an environment variable in a Jenkins Pipeline script?
You access an environment variable in a Jenkins Pipeline using the syntax: env.VARIABLE_NAME. For example, env.BUILD_NUMBER gives the current build number.
Click to reveal answer
intermediate
How can you set a custom environment variable in a Jenkins Pipeline?
You can set a custom environment variable inside the environment block in a Jenkins Pipeline like this:
environment {
MY_VAR = 'Hello'
}
Click to reveal answer
intermediate
What is the difference between global and job-specific environment variables in Jenkins?
Global environment variables are set for all Jenkins jobs and agents, while job-specific variables are defined only within a particular job or pipeline.
Click to reveal answer
intermediate
How do you print all environment variables in a Jenkins Pipeline?
You can print all environment variables using a shell step:
sh 'printenv'
or in a scripted pipeline:
env.each { k, v -> println "${k} = ${v}" }
Click to reveal answer
How do you access the environment variable named 'PATH' in a Jenkins Pipeline script?
AgetEnv('PATH')
BPATH.env
Cenv.PATH
Denvironment.PATH
✗ Incorrect
In Jenkins Pipeline, environment variables are accessed using the 'env' object followed by the variable name, like env.PATH.
Where do you define custom environment variables for a Jenkins Pipeline?
AInside the environment block
BIn the Jenkins global config only
CInside the steps block
DIn the Jenkinsfile header comment
✗ Incorrect
Custom environment variables are defined inside the 'environment' block in a Jenkins Pipeline.
Which command prints all environment variables in a Jenkins Pipeline shell step?
Alistenv
Bprintenv
Cshowenv
Denvlist
✗ Incorrect
The 'printenv' command lists all environment variables in a shell environment.
What is the scope of environment variables defined in the Jenkins global configuration?
AOnly available during build steps
BOnly available to one job
COnly available in post-build actions
DAvailable to all jobs and agents
✗ Incorrect
Global environment variables are accessible to all Jenkins jobs and agents.
How do you access the current build number in a Jenkins Pipeline?
Aenv.BUILD_NUMBER
Bbuild.number
CcurrentBuild.number
Denv.BUILDNUM
✗ Incorrect
The current build number is available as the environment variable 'BUILD_NUMBER', accessed via env.BUILD_NUMBER.
Explain how to set and access environment variables in a Jenkins Pipeline.
Think about the environment block and the env object.
You got /3 concepts.
Describe the difference between global and job-specific environment variables in Jenkins.
Consider scope and configuration location.
You got /3 concepts.
Practice
(1/5)
1. In a Jenkins Pipeline script, how do you access an environment variable named BUILD_NUMBER?
Jenkins Pipeline exposes environment variables via the env object.
Step 2: Access the variable using the correct syntax
You access BUILD_NUMBER as env.BUILD_NUMBER in the script.
Final Answer:
Using env.BUILD_NUMBER -> Option B
Quick Check:
env.VARIABLE_NAME = correct access [OK]
Hint: Always prefix environment variables with env. in Jenkins [OK]
Common Mistakes:
Trying to use variable name without env prefix
Using shell-style $VARIABLE inside Groovy script
Calling non-existent getEnv() method
2. Which of the following is the correct syntax to print the environment variable JOB_NAME in a Jenkins Pipeline script block?
easy
A. println("JOB_NAME")
B. echo $JOB_NAME
C. println(env.JOB_NAME)
D. print(JOB_NAME)
Solution
Step 1: Identify the correct way to print env variables in Groovy script
In Jenkins Pipeline Groovy scripts, use println(env.VARIABLE) to print environment variables.
Step 2: Check each option
println("JOB_NAME") prints the string "JOB_NAME" literally. echo $JOB_NAME uses shell syntax, invalid in Groovy. print(JOB_NAME) attempts to print JOB_NAME directly, but it's not available in Groovy scope without env prefix.
Final Answer:
println(env.JOB_NAME) -> Option C
Quick Check:
Use println(env.VARIABLE) to print env vars in script [OK]
Hint: Use println(env.VARIABLE) inside script blocks [OK]
But the output is empty. What is the likely cause?
medium
A. The environment block syntax is incorrect; use MY_VAR='hello' with quotes and no spaces around =
B. Environment variables must be accessed as env['MY_VAR'] not env.MY_VAR
C. The environment variable was not set correctly; use MY_VAR='hello' without spaces
D. You cannot access environment variables inside script blocks
Solution
Step 1: Check environment block syntax
In Jenkins Pipeline, environment variables must be assigned with no spaces around = and values in quotes, like MY_VAR='hello'.
Step 2: Identify the syntax error in the snippet
The snippet uses MY_VAR = 'hello' with spaces around = which is invalid and causes the variable not to be set.
Final Answer:
The environment block syntax is incorrect; use MY_VAR='hello' with quotes and no spaces around = -> Option A
Quick Check:
Use no spaces around = in environment block [OK]
Hint: No spaces around = in environment block assignments [OK]
Common Mistakes:
Adding spaces around = in environment variable assignment
Trying to access env vars incorrectly
Assuming script block can't access env vars
5. You want to pass a secret API key stored as an environment variable API_KEY to a shell command in Jenkins Pipeline. Which is the best way to do this securely?
hard
A. Use sh "curl -H 'Authorization: Bearer $API_KEY' https://api.example.com"
B. Use sh(script: "curl -H 'Authorization: Bearer ${env.API_KEY}' https://api.example.com")
C. Use sh(script: "curl -H 'Authorization: Bearer $API_KEY' https://api.example.com", environment: [API_KEY: env.API_KEY])
D. Use Jenkins credentials binding plugin to inject API_KEY and access it as env.API_KEY inside sh
Solution
Step 1: Understand secure handling of secrets in Jenkins
Secrets like API keys should be stored in Jenkins credentials and injected securely, not hardcoded or exposed in logs.
Step 2: Use credentials binding plugin
This plugin injects secrets as environment variables during the build, accessible as env.API_KEY inside sh without exposing them in logs.
Final Answer:
Use Jenkins credentials binding plugin to inject API_KEY and access it as env.API_KEY inside sh -> Option D
Quick Check:
Use credentials plugin for secrets, not plain env vars [OK]
Hint: Use Jenkins credentials plugin for secrets, not plain env vars [OK]
Common Mistakes:
Passing secrets directly in shell commands risking exposure
Using string interpolation exposing secrets in logs
Not using Jenkins credentials plugin for sensitive data
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.